This is python.info, produced by makeinfo version 6.5 from python.texi.
Python 3.8.9, September 01, 2021
unknown
Copyright © 2001-2021, Python Software Foundation
INFO-DIR-SECTION Miscellaneous
START-INFO-DIR-ENTRY
* python: (python.info). One line description of project
END-INFO-DIR-ENTRY
Generated by Sphinx 2.4.4.
File: python.info, Node: Top, Next: What’s New in Python, Up: (dir)
Python
******
Python 3.8.9, September 01, 2021
unknown
Copyright © 2001-2021, Python Software Foundation
* Menu:
* What’s New in Python::
* The Python Tutorial::
* Python Setup and Usage::
* The Python Language Reference::
* The Python Standard Library::
* Extending and Embedding the Python Interpreter::
* Python/C API Reference Manual::
* Distributing Python Modules::
* Installing Python Modules::
* Python HOWTOs::
* Python Frequently Asked Questions::
* Glossary::
* About these documents::
* Dealing with Bugs::
* Copyright::
* History and License::
* Distributing Python Modules (Legacy version): Distributing Python Modules Legacy version.
* Installing Python Modules (Legacy version): Installing Python Modules Legacy version.
* Python Module Index::
* Index::
— The Detailed Node Listing —
What’s New in Python
* What’s New In Python 3.8: What’s New In Python 3 8.
* What’s New In Python 3.7: What’s New In Python 3 7.
* What’s New In Python 3.6: What’s New In Python 3 6.
* What’s New In Python 3.5: What’s New In Python 3 5.
* What’s New In Python 3.4: What’s New In Python 3 4.
* What’s New In Python 3.3: What’s New In Python 3 3.
* What’s New In Python 3.2: What’s New In Python 3 2.
* What’s New In Python 3.1: What’s New In Python 3 1.
* What’s New In Python 3.0: What’s New In Python 3 0.
* What’s New in Python 2.7: What’s New in Python 2 7.
* What’s New in Python 2.6: What’s New in Python 2 6.
* What’s New in Python 2.5: What’s New in Python 2 5.
* What’s New in Python 2.4: What’s New in Python 2 4.
* What’s New in Python 2.3: What’s New in Python 2 3.
* What’s New in Python 2.2: What’s New in Python 2 2.
* What’s New in Python 2.1: What’s New in Python 2 1.
* What’s New in Python 2.0: What’s New in Python 2 0.
* Changelog::
What’s New In Python 3.8
* Summary – Release highlights::
* New Features::
* Other Language Changes::
* New Modules::
* Improved Modules::
* Optimizations::
* Build and C API Changes::
* Deprecated::
* API and Feature Removals::
* Porting to Python 3.8: Porting to Python 3 8.
* Notable changes in Python 3.8.1: Notable changes in Python 3 8 1.
* Notable changes in Python 3.8.2: Notable changes in Python 3 8 2.
* Notable changes in Python 3.8.3: Notable changes in Python 3 8 3.
* Notable changes in Python 3.8.8: Notable changes in Python 3 8 8.
* Notable changes in Python 3.8.9: Notable changes in Python 3 8 9.
New Features
* Assignment expressions::
* Positional-only parameters::
* Parallel filesystem cache for compiled bytecode files::
* Debug build uses the same ABI as release build::
* f-strings support = for self-documenting expressions and debugging::
* PEP 578; Python Runtime Audit Hooks: PEP 578 Python Runtime Audit Hooks.
* PEP 587; Python Initialization Configuration: PEP 587 Python Initialization Configuration.
* Vectorcall; a fast calling protocol for CPython: Vectorcall a fast calling protocol for CPython.
* Pickle protocol 5 with out-of-band data buffers::
Improved Modules
* ast::
* asyncio::
* builtins::
* collections::
* cProfile::
* csv::
* curses::
* ctypes::
* datetime::
* functools::
* gc::
* gettext::
* gzip::
* IDLE and idlelib::
* inspect::
* io::
* itertools::
* json.tool: json tool.
* logging::
* math::
* mmap::
* multiprocessing::
* os::
* os.path: os path.
* pathlib::
* pickle::
* plistlib::
* pprint::
* py_compile::
* shlex::
* shutil::
* socket::
* ssl::
* statistics::
* sys::
* tarfile::
* threading::
* tokenize::
* tkinter::
* time::
* typing::
* unicodedata::
* unittest::
* venv::
* weakref::
* xml::
* xmlrpc::
Porting to Python 3.8
* Changes in Python behavior::
* Changes in the Python API::
* Changes in the C API::
* CPython bytecode changes::
* Demos and Tools::
What’s New In Python 3.7
* Summary – Release Highlights::
* New Features: New Features<2>.
* Other Language Changes: Other Language Changes<2>.
* New Modules: New Modules<2>.
* Improved Modules: Improved Modules<2>.
* C API Changes::
* Build Changes::
* Optimizations: Optimizations<2>.
* Other CPython Implementation Changes::
* Deprecated Python Behavior::
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods.
* Deprecated functions and types of the C API::
* Platform Support Removals::
* API and Feature Removals: API and Feature Removals<2>.
* Module Removals::
* Windows-only Changes::
* Porting to Python 3.7: Porting to Python 3 7.
* Notable changes in Python 3.7.1: Notable changes in Python 3 7 1.
* Notable changes in Python 3.7.2: Notable changes in Python 3 7 2.
* Notable changes in Python 3.7.6: Notable changes in Python 3 7 6.
* Notable changes in Python 3.7.10: Notable changes in Python 3 7 10.
New Features
* PEP 563; Postponed Evaluation of Annotations: PEP 563 Postponed Evaluation of Annotations.
* PEP 538; Legacy C Locale Coercion: PEP 538 Legacy C Locale Coercion.
* PEP 540; Forced UTF-8 Runtime Mode: PEP 540 Forced UTF-8 Runtime Mode.
* PEP 553; Built-in breakpoint(): PEP 553 Built-in breakpoint.
* PEP 539; New C API for Thread-Local Storage: PEP 539 New C API for Thread-Local Storage.
* PEP 562; Customization of Access to Module Attributes: PEP 562 Customization of Access to Module Attributes.
* PEP 564; New Time Functions With Nanosecond Resolution: PEP 564 New Time Functions With Nanosecond Resolution.
* PEP 565; Show DeprecationWarning in __main__: PEP 565 Show DeprecationWarning in __main__.
* PEP 560; Core Support for typing module and Generic Types: PEP 560 Core Support for typing module and Generic Types.
* PEP 552; Hash-based .pyc Files: PEP 552 Hash-based pyc Files.
* PEP 545; Python Documentation Translations: PEP 545 Python Documentation Translations.
* Development Runtime Mode; -X dev: Development Runtime Mode -X dev.
New Modules
* contextvars::
* dataclasses::
* importlib.resources: importlib resources.
Improved Modules
* argparse::
* asyncio: asyncio<2>.
* binascii::
* calendar::
* collections: collections<2>.
* compileall::
* concurrent.futures: concurrent futures.
* contextlib::
* cProfile: cProfile<2>.
* crypt::
* datetime: datetime<2>.
* dbm::
* decimal::
* dis::
* distutils::
* enum::
* functools: functools<2>.
* gc: gc<2>.
* hmac::
* http.client: http client.
* http.server: http server.
* idlelib and IDLE::
* importlib::
* io: io<2>.
* ipaddress::
* itertools: itertools<2>.
* locale::
* logging: logging<2>.
* math: math<2>.
* mimetypes::
* msilib::
* multiprocessing: multiprocessing<2>.
* os: os<2>.
* pathlib: pathlib<2>.
* pdb::
* py_compile: py_compile<2>.
* pydoc::
* queue::
* re::
* signal::
* socket: socket<2>.
* socketserver::
* sqlite3::
* ssl: ssl<2>.
* string::
* subprocess::
* sys: sys<2>.
* time: time<2>.
* tkinter: tkinter<2>.
* tracemalloc::
* types::
* unicodedata: unicodedata<2>.
* unittest: unittest<2>.
* unittest.mock: unittest mock.
* urllib.parse: urllib parse.
* uu::
* uuid::
* warnings::
* xml.etree: xml etree.
* xmlrpc.server: xmlrpc server.
* zipapp::
* zipfile::
Deprecated Python modules, functions and methods
* aifc::
* asyncio: asyncio<3>.
* collections: collections<3>.
* dbm: dbm<2>.
* enum: enum<2>.
* gettext: gettext<2>.
* importlib: importlib<2>.
* locale: locale<2>.
* macpath::
* threading: threading<2>.
* socket: socket<3>.
* ssl: ssl<3>.
* sunau::
* sys: sys<3>.
* wave::
Porting to Python 3.7
* Changes in Python Behavior::
* Changes in the Python API: Changes in the Python API<2>.
* Changes in the C API: Changes in the C API<2>.
* CPython bytecode changes: CPython bytecode changes<2>.
* Windows-only Changes: Windows-only Changes<2>.
* Other CPython implementation changes::
What’s New In Python 3.6
* Summary – Release highlights: Summary – Release highlights<2>.
* New Features: New Features<3>.
* Other Language Changes: Other Language Changes<3>.
* New Modules: New Modules<3>.
* Improved Modules: Improved Modules<3>.
* Optimizations: Optimizations<3>.
* Build and C API Changes: Build and C API Changes<2>.
* Other Improvements::
* Deprecated: Deprecated<2>.
* Removed::
* Porting to Python 3.6: Porting to Python 3 6.
* Notable changes in Python 3.6.2: Notable changes in Python 3 6 2.
* Notable changes in Python 3.6.4: Notable changes in Python 3 6 4.
* Notable changes in Python 3.6.5: Notable changes in Python 3 6 5.
* Notable changes in Python 3.6.7: Notable changes in Python 3 6 7.
* Notable changes in Python 3.6.10: Notable changes in Python 3 6 10.
* Notable changes in Python 3.6.13: Notable changes in Python 3 6 13.
New Features
* PEP 498; Formatted string literals: PEP 498 Formatted string literals.
* PEP 526; Syntax for variable annotations: PEP 526 Syntax for variable annotations.
* PEP 515; Underscores in Numeric Literals: PEP 515 Underscores in Numeric Literals.
* PEP 525; Asynchronous Generators: PEP 525 Asynchronous Generators.
* PEP 530; Asynchronous Comprehensions: PEP 530 Asynchronous Comprehensions.
* PEP 487; Simpler customization of class creation: PEP 487 Simpler customization of class creation.
* PEP 487; Descriptor Protocol Enhancements: PEP 487 Descriptor Protocol Enhancements.
* PEP 519; Adding a file system path protocol: PEP 519 Adding a file system path protocol.
* PEP 495; Local Time Disambiguation: PEP 495 Local Time Disambiguation.
* PEP 529; Change Windows filesystem encoding to UTF-8: PEP 529 Change Windows filesystem encoding to UTF-8.
* PEP 528; Change Windows console encoding to UTF-8: PEP 528 Change Windows console encoding to UTF-8.
* PEP 520; Preserving Class Attribute Definition Order: PEP 520 Preserving Class Attribute Definition Order.
* PEP 468; Preserving Keyword Argument Order: PEP 468 Preserving Keyword Argument Order.
* New dict implementation::
* PEP 523; Adding a frame evaluation API to CPython: PEP 523 Adding a frame evaluation API to CPython.
* PYTHONMALLOC environment variable::
* DTrace and SystemTap probing support::
New Modules
* secrets::
Improved Modules
* array::
* ast: ast<2>.
* asyncio: asyncio<4>.
* binascii: binascii<2>.
* cmath::
* collections: collections<4>.
* concurrent.futures: concurrent futures<2>.
* contextlib: contextlib<2>.
* datetime: datetime<3>.
* decimal: decimal<2>.
* distutils: distutils<2>.
* email::
* encodings::
* enum: enum<3>.
* faulthandler::
* fileinput::
* hashlib::
* http.client: http client<2>.
* idlelib and IDLE: idlelib and IDLE<2>.
* importlib: importlib<3>.
* inspect: inspect<2>.
* json::
* logging: logging<3>.
* math: math<3>.
* multiprocessing: multiprocessing<3>.
* os: os<3>.
* pathlib: pathlib<3>.
* pdb: pdb<2>.
* pickle: pickle<2>.
* pickletools::
* pydoc: pydoc<2>.
* random::
* re: re<2>.
* readline::
* rlcompleter::
* shlex: shlex<2>.
* site::
* sqlite3: sqlite3<2>.
* socket: socket<4>.
* socketserver: socketserver<2>.
* ssl: ssl<4>.
* statistics: statistics<2>.
* struct::
* subprocess: subprocess<2>.
* sys: sys<4>.
* telnetlib::
* time: time<3>.
* timeit::
* tkinter: tkinter<3>.
* traceback::
* tracemalloc: tracemalloc<2>.
* typing: typing<2>.
* unicodedata: unicodedata<3>.
* unittest.mock: unittest mock<2>.
* urllib.request: urllib request.
* urllib.robotparser: urllib robotparser.
* venv: venv<2>.
* warnings: warnings<2>.
* winreg::
* winsound::
* xmlrpc.client: xmlrpc client.
* zipfile: zipfile<2>.
* zlib::
Deprecated
* New Keywords::
* Deprecated Python behavior::
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods<2>.
* Deprecated functions and types of the C API: Deprecated functions and types of the C API<2>.
* Deprecated Build Options::
Deprecated Python modules, functions and methods
* asynchat::
* asyncore::
* dbm: dbm<3>.
* distutils: distutils<3>.
* grp::
* importlib: importlib<4>.
* os: os<4>.
* re: re<3>.
* ssl: ssl<5>.
* tkinter: tkinter<4>.
* venv: venv<3>.
Removed
* API and Feature Removals: API and Feature Removals<3>.
Porting to Python 3.6
* Changes in ‘python’ Command Behavior::
* Changes in the Python API: Changes in the Python API<3>.
* Changes in the C API: Changes in the C API<3>.
* CPython bytecode changes: CPython bytecode changes<3>.
Notable changes in Python 3.6.2
* New make regen-all build target::
* Removal of make touch build target::
What’s New In Python 3.5
* Summary – Release highlights: Summary – Release highlights<3>.
* New Features: New Features<4>.
* Other Language Changes: Other Language Changes<4>.
* New Modules: New Modules<4>.
* Improved Modules: Improved Modules<4>.
* Other module-level changes::
* Optimizations: Optimizations<4>.
* Build and C API Changes: Build and C API Changes<3>.
* Deprecated: Deprecated<3>.
* Removed: Removed<2>.
* Porting to Python 3.5: Porting to Python 3 5.
* Notable changes in Python 3.5.4: Notable changes in Python 3 5 4.
New Features
* PEP 492 - Coroutines with async and await syntax::
* PEP 465 - A dedicated infix operator for matrix multiplication::
* PEP 448 - Additional Unpacking Generalizations::
* PEP 461 - percent formatting support for bytes and bytearray::
* PEP 484 - Type Hints::
* PEP 471 - os.scandir() function – a better and faster directory iterator: PEP 471 - os scandir function – a better and faster directory iterator.
* PEP 475; Retry system calls failing with EINTR: PEP 475 Retry system calls failing with EINTR.
* PEP 479; Change StopIteration handling inside generators: PEP 479 Change StopIteration handling inside generators.
* PEP 485; A function for testing approximate equality: PEP 485 A function for testing approximate equality.
* PEP 486; Make the Python Launcher aware of virtual environments: PEP 486 Make the Python Launcher aware of virtual environments.
* PEP 488; Elimination of PYO files: PEP 488 Elimination of PYO files.
* PEP 489; Multi-phase extension module initialization: PEP 489 Multi-phase extension module initialization.
New Modules
* typing: typing<3>.
* zipapp: zipapp<2>.
Improved Modules
* argparse: argparse<2>.
* asyncio: asyncio<5>.
* bz2::
* cgi::
* cmath: cmath<2>.
* code::
* collections: collections<5>.
* collections.abc: collections abc.
* compileall: compileall<2>.
* concurrent.futures: concurrent futures<3>.
* configparser::
* contextlib: contextlib<3>.
* csv: csv<2>.
* curses: curses<2>.
* dbm: dbm<4>.
* difflib::
* distutils: distutils<4>.
* doctest::
* email: email<2>.
* enum: enum<4>.
* faulthandler: faulthandler<2>.
* functools: functools<3>.
* glob::
* gzip: gzip<2>.
* heapq::
* http::
* http.client: http client<3>.
* idlelib and IDLE: idlelib and IDLE<3>.
* imaplib::
* imghdr::
* importlib: importlib<5>.
* inspect: inspect<3>.
* io: io<3>.
* ipaddress: ipaddress<2>.
* json: json<2>.
* linecache::
* locale: locale<3>.
* logging: logging<4>.
* lzma::
* math: math<4>.
* multiprocessing: multiprocessing<4>.
* operator::
* os: os<5>.
* pathlib: pathlib<4>.
* pickle: pickle<3>.
* poplib::
* re: re<4>.
* readline: readline<2>.
* selectors::
* shutil: shutil<2>.
* signal: signal<2>.
* smtpd::
* smtplib::
* sndhdr::
* socket: socket<5>.
* ssl: ssl<6>.
* sqlite3: sqlite3<3>.
* subprocess: subprocess<3>.
* sys: sys<5>.
* sysconfig::
* tarfile: tarfile<2>.
* threading: threading<3>.
* time: time<4>.
* timeit: timeit<2>.
* tkinter: tkinter<5>.
* traceback: traceback<2>.
* types: types<2>.
* unicodedata: unicodedata<4>.
* unittest: unittest<3>.
* unittest.mock: unittest mock<3>.
* urllib::
* wsgiref::
* xmlrpc: xmlrpc<2>.
* xml.sax: xml sax.
* zipfile: zipfile<3>.
ssl
* Memory BIO Support::
* Application-Layer Protocol Negotiation Support::
* Other Changes::
Deprecated
* New Keywords: New Keywords<2>.
* Deprecated Python Behavior: Deprecated Python Behavior<2>.
* Unsupported Operating Systems::
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods<3>.
Removed
* API and Feature Removals: API and Feature Removals<4>.
Porting to Python 3.5
* Changes in Python behavior: Changes in Python behavior<2>.
* Changes in the Python API: Changes in the Python API<4>.
* Changes in the C API: Changes in the C API<4>.
Notable changes in Python 3.5.4
* New make regen-all build target: New make regen-all build target<2>.
* Removal of make touch build target: Removal of make touch build target<2>.
What’s New In Python 3.4
* Summary – Release Highlights: Summary – Release Highlights<2>.
* New Features: New Features<5>.
* New Modules: New Modules<5>.
* Improved Modules: Improved Modules<5>.
* CPython Implementation Changes::
* Deprecated: Deprecated<4>.
* Removed: Removed<3>.
* Porting to Python 3.4: Porting to Python 3 4.
* Changed in 3.4.3: Changed in 3 4 3.
New Features
* PEP 453; Explicit Bootstrapping of PIP in Python Installations: PEP 453 Explicit Bootstrapping of PIP in Python Installations.
* PEP 446; Newly Created File Descriptors Are Non-Inheritable: PEP 446 Newly Created File Descriptors Are Non-Inheritable.
* Improvements to Codec Handling::
* PEP 451; A ModuleSpec Type for the Import System: PEP 451 A ModuleSpec Type for the Import System.
* Other Language Changes: Other Language Changes<5>.
PEP 453: Explicit Bootstrapping of PIP in Python Installations
* Bootstrapping pip By Default::
* Documentation Changes::
New Modules
* asyncio: asyncio<6>.
* ensurepip::
* enum: enum<5>.
* pathlib: pathlib<5>.
* selectors: selectors<2>.
* statistics: statistics<3>.
* tracemalloc: tracemalloc<3>.
Improved Modules
* abc::
* aifc: aifc<2>.
* argparse: argparse<3>.
* audioop::
* base64::
* collections: collections<6>.
* colorsys::
* contextlib: contextlib<4>.
* dbm: dbm<5>.
* dis: dis<2>.
* doctest: doctest<2>.
* email: email<3>.
* filecmp::
* functools: functools<4>.
* gc: gc<3>.
* glob: glob<2>.
* hashlib: hashlib<2>.
* hmac: hmac<2>.
* html::
* http: http<2>.
* idlelib and IDLE: idlelib and IDLE<4>.
* importlib: importlib<6>.
* inspect: inspect<4>.
* ipaddress: ipaddress<3>.
* logging: logging<5>.
* marshal::
* mmap: mmap<2>.
* multiprocessing: multiprocessing<5>.
* operator: operator<2>.
* os: os<6>.
* pdb: pdb<3>.
* pickle: pickle<4>.
* plistlib: plistlib<2>.
* poplib: poplib<2>.
* pprint: pprint<2>.
* pty::
* pydoc: pydoc<3>.
* re: re<5>.
* resource::
* select::
* shelve::
* shutil: shutil<3>.
* smtpd: smtpd<2>.
* smtplib: smtplib<2>.
* socket: socket<6>.
* sqlite3: sqlite3<4>.
* ssl: ssl<7>.
* stat::
* struct: struct<2>.
* subprocess: subprocess<4>.
* sunau: sunau<2>.
* sys: sys<6>.
* tarfile: tarfile<3>.
* textwrap::
* threading: threading<4>.
* traceback: traceback<3>.
* types: types<3>.
* urllib: urllib<2>.
* unittest: unittest<4>.
* venv: venv<4>.
* wave: wave<2>.
* weakref: weakref<2>.
* xml.etree: xml etree<2>.
* zipfile: zipfile<4>.
CPython Implementation Changes
* PEP 445; Customization of CPython Memory Allocators: PEP 445 Customization of CPython Memory Allocators.
* PEP 442; Safe Object Finalization: PEP 442 Safe Object Finalization.
* PEP 456; Secure and Interchangeable Hash Algorithm: PEP 456 Secure and Interchangeable Hash Algorithm.
* PEP 436; Argument Clinic: PEP 436 Argument Clinic.
* Other Build and C API Changes::
* Other Improvements: Other Improvements<2>.
* Significant Optimizations::
Deprecated
* Deprecations in the Python API::
* Deprecated Features::
Removed
* Operating Systems No Longer Supported::
* API and Feature Removals: API and Feature Removals<5>.
* Code Cleanups::
Porting to Python 3.4
* Changes in ‘python’ Command Behavior: Changes in ‘python’ Command Behavior<2>.
* Changes in the Python API: Changes in the Python API<5>.
* Changes in the C API: Changes in the C API<5>.
Changed in 3.4.3
* PEP 476; Enabling certificate verification by default for stdlib http clients: PEP 476 Enabling certificate verification by default for stdlib http clients.
What’s New In Python 3.3
* Summary – Release highlights: Summary – Release highlights<4>.
* PEP 405; Virtual Environments: PEP 405 Virtual Environments.
* PEP 420; Implicit Namespace Packages: PEP 420 Implicit Namespace Packages.
* PEP 3118; New memoryview implementation and buffer protocol documentation: PEP 3118 New memoryview implementation and buffer protocol documentation.
* PEP 393; Flexible String Representation: PEP 393 Flexible String Representation.
* PEP 397; Python Launcher for Windows: PEP 397 Python Launcher for Windows.
* PEP 3151; Reworking the OS and IO exception hierarchy: PEP 3151 Reworking the OS and IO exception hierarchy.
* PEP 380; Syntax for Delegating to a Subgenerator: PEP 380 Syntax for Delegating to a Subgenerator.
* PEP 409; Suppressing exception context: PEP 409 Suppressing exception context.
* PEP 414; Explicit Unicode literals: PEP 414 Explicit Unicode literals.
* PEP 3155; Qualified name for classes and functions: PEP 3155 Qualified name for classes and functions.
* PEP 412; Key-Sharing Dictionary: PEP 412 Key-Sharing Dictionary.
* PEP 362; Function Signature Object: PEP 362 Function Signature Object.
* PEP 421; Adding sys.implementation: PEP 421 Adding sys implementation.
* Using importlib as the Implementation of Import::
* Other Language Changes: Other Language Changes<6>.
* A Finer-Grained Import Lock::
* Builtin functions and types::
* New Modules: New Modules<6>.
* Improved Modules: Improved Modules<6>.
* Optimizations: Optimizations<5>.
* Build and C API Changes: Build and C API Changes<4>.
* Deprecated: Deprecated<5>.
* Porting to Python 3.3: Porting to Python 3 3.
PEP 3118: New memoryview implementation and buffer protocol documentation
* Features::
* API changes::
PEP 393: Flexible String Representation
* Functionality::
* Performance and resource usage::
PEP 421: Adding sys.implementation
* SimpleNamespace::
Using importlib as the Implementation of Import
* New APIs::
* Visible Changes::
New Modules
* faulthandler: faulthandler<3>.
* ipaddress: ipaddress<4>.
* lzma: lzma<2>.
Improved Modules
* abc: abc<2>.
* array: array<2>.
* base64: base64<2>.
* binascii: binascii<3>.
* bz2: bz2<2>.
* codecs::
* collections: collections<7>.
* contextlib: contextlib<5>.
* crypt: crypt<2>.
* curses: curses<3>.
* datetime: datetime<4>.
* decimal: decimal<3>.
* email: email<4>.
* ftplib::
* functools: functools<5>.
* gc: gc<4>.
* hmac: hmac<3>.
* http: http<3>.
* html: html<2>.
* imaplib: imaplib<2>.
* inspect: inspect<5>.
* io: io<4>.
* itertools: itertools<3>.
* logging: logging<6>.
* math: math<5>.
* mmap: mmap<3>.
* multiprocessing: multiprocessing<6>.
* nntplib::
* os: os<7>.
* pdb: pdb<4>.
* pickle: pickle<5>.
* pydoc: pydoc<4>.
* re: re<6>.
* sched::
* select: select<2>.
* shlex: shlex<3>.
* shutil: shutil<4>.
* signal: signal<3>.
* smtpd: smtpd<3>.
* smtplib: smtplib<3>.
* socket: socket<7>.
* socketserver: socketserver<3>.
* sqlite3: sqlite3<5>.
* ssl: ssl<8>.
* stat: stat<2>.
* struct: struct<3>.
* subprocess: subprocess<5>.
* sys: sys<7>.
* tarfile: tarfile<4>.
* tempfile::
* textwrap: textwrap<2>.
* threading: threading<5>.
* time: time<5>.
* types: types<4>.
* unittest: unittest<5>.
* urllib: urllib<3>.
* webbrowser::
* xml.etree.ElementTree: xml etree ElementTree.
* zlib: zlib<2>.
decimal
* Features: Features<2>.
* API changes: API changes<2>.
email
* Policy Framework::
* Provisional Policy with New Header API::
* Other API Changes::
Deprecated
* Unsupported Operating Systems: Unsupported Operating Systems<2>.
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods<4>.
* Deprecated functions and types of the C API: Deprecated functions and types of the C API<3>.
* Deprecated features::
Porting to Python 3.3
* Porting Python code::
* Porting C code::
* Building C extensions::
* Command Line Switch Changes::
What’s New In Python 3.2
* PEP 384; Defining a Stable ABI: PEP 384 Defining a Stable ABI.
* PEP 389; Argparse Command Line Parsing Module: PEP 389 Argparse Command Line Parsing Module.
* PEP 391; Dictionary Based Configuration for Logging: PEP 391 Dictionary Based Configuration for Logging.
* PEP 3148; The concurrent.futures module: PEP 3148 The concurrent futures module.
* PEP 3147; PYC Repository Directories: PEP 3147 PYC Repository Directories.
* PEP 3149; ABI Version Tagged .so Files: PEP 3149 ABI Version Tagged so Files.
* PEP 3333; Python Web Server Gateway Interface v1.0.1: PEP 3333 Python Web Server Gateway Interface v1 0 1.
* Other Language Changes: Other Language Changes<7>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules.
* Multi-threading::
* Optimizations: Optimizations<6>.
* Unicode::
* Codecs::
* Documentation::
* IDLE::
* Code Repository::
* Build and C API Changes: Build and C API Changes<5>.
* Porting to Python 3.2: Porting to Python 3 2.
New, Improved, and Deprecated Modules
* email: email<5>.
* elementtree::
* functools: functools<6>.
* itertools: itertools<4>.
* collections: collections<8>.
* threading: threading<6>.
* datetime and time::
* math: math<6>.
* abc: abc<3>.
* io: io<5>.
* reprlib::
* logging: logging<7>.
* csv: csv<3>.
* contextlib: contextlib<6>.
* decimal and fractions::
* ftp::
* popen::
* select: select<3>.
* gzip and zipfile::
* tarfile: tarfile<5>.
* hashlib: hashlib<3>.
* ast: ast<3>.
* os: os<8>.
* shutil: shutil<5>.
* sqlite3: sqlite3<6>.
* html: html<3>.
* socket: socket<8>.
* ssl: ssl<9>.
* nntp::
* certificates::
* imaplib: imaplib<3>.
* http.client: http client<4>.
* unittest: unittest<6>.
* random: random<2>.
* poplib: poplib<3>.
* asyncore: asyncore<2>.
* tempfile: tempfile<2>.
* inspect: inspect<6>.
* pydoc: pydoc<5>.
* dis: dis<3>.
* dbm: dbm<6>.
* ctypes: ctypes<2>.
* site: site<2>.
* sysconfig: sysconfig<2>.
* pdb: pdb<5>.
* configparser: configparser<2>.
* urllib.parse: urllib parse<2>.
* mailbox::
* turtledemo::
What’s New In Python 3.1
* PEP 372; Ordered Dictionaries: PEP 372 Ordered Dictionaries.
* PEP 378; Format Specifier for Thousands Separator: PEP 378 Format Specifier for Thousands Separator.
* Other Language Changes: Other Language Changes<8>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules<2>.
* Optimizations: Optimizations<7>.
* IDLE: IDLE<2>.
* Build and C API Changes: Build and C API Changes<6>.
* Porting to Python 3.1: Porting to Python 3 1.
What’s New In Python 3.0
* Common Stumbling Blocks::
* Overview Of Syntax Changes::
* Changes Already Present In Python 2.6: Changes Already Present In Python 2 6.
* Library Changes::
* PEP 3101; A New Approach To String Formatting: PEP 3101 A New Approach To String Formatting.
* Changes To Exceptions::
* Miscellaneous Other Changes::
* Build and C API Changes: Build and C API Changes<7>.
* Performance::
* Porting To Python 3.0: Porting To Python 3 0.
Common Stumbling Blocks
* Print Is A Function::
* Views And Iterators Instead Of Lists::
* Ordering Comparisons::
* Integers::
* Text Vs. Data Instead Of Unicode Vs. 8-bit: Text Vs Data Instead Of Unicode Vs 8-bit.
Overview Of Syntax Changes
* New Syntax::
* Changed Syntax::
* Removed Syntax::
Miscellaneous Other Changes
* Operators And Special Methods::
* Builtins::
What’s New in Python 2.7
* The Future for Python 2.x: The Future for Python 2 x.
* Changes to the Handling of Deprecation Warnings::
* Python 3.1 Features: Python 3 1 Features.
* PEP 372; Adding an Ordered Dictionary to collections: PEP 372 Adding an Ordered Dictionary to collections.
* PEP 378; Format Specifier for Thousands Separator: PEP 378 Format Specifier for Thousands Separator<2>.
* PEP 389; The argparse Module for Parsing Command Lines: PEP 389 The argparse Module for Parsing Command Lines.
* PEP 391; Dictionary-Based Configuration For Logging: PEP 391 Dictionary-Based Configuration For Logging.
* PEP 3106; Dictionary Views: PEP 3106 Dictionary Views.
* PEP 3137; The memoryview Object: PEP 3137 The memoryview Object.
* Other Language Changes: Other Language Changes<9>.
* New and Improved Modules::
* Build and C API Changes: Build and C API Changes<8>.
* Other Changes and Fixes::
* Porting to Python 2.7: Porting to Python 2 7.
* New Features Added to Python 2.7 Maintenance Releases: New Features Added to Python 2 7 Maintenance Releases.
* Acknowledgements::
Other Language Changes
* Interpreter Changes::
* Optimizations: Optimizations<8>.
New and Improved Modules
* New module; importlib: New module importlib.
* New module; sysconfig: New module sysconfig.
* ttk; Themed Widgets for Tk: ttk Themed Widgets for Tk.
* Updated module; unittest: Updated module unittest.
* Updated module; ElementTree 1.3: Updated module ElementTree 1 3.
Build and C API Changes
* Capsules::
* Port-Specific Changes; Windows: Port-Specific Changes Windows.
* Port-Specific Changes; Mac OS X: Port-Specific Changes Mac OS X.
* Port-Specific Changes; FreeBSD: Port-Specific Changes FreeBSD.
New Features Added to Python 2.7 Maintenance Releases
* Two new environment variables for debug mode::
* PEP 434; IDLE Enhancement Exception for All Branches: PEP 434 IDLE Enhancement Exception for All Branches.
* PEP 466; Network Security Enhancements for Python 2.7: PEP 466 Network Security Enhancements for Python 2 7.
* PEP 477; Backport ensurepip (PEP 453) to Python 2.7: PEP 477 Backport ensurepip PEP 453 to Python 2 7.
* PEP 476; Enabling certificate verification by default for stdlib http clients: PEP 476 Enabling certificate verification by default for stdlib http clients<2>.
* PEP 493; HTTPS verification migration tools for Python 2.7: PEP 493 HTTPS verification migration tools for Python 2 7.
* New make regen-all build target: New make regen-all build target<3>.
* Removal of make touch build target: Removal of make touch build target<3>.
PEP 477: Backport ensurepip (PEP 453) to Python 2.7
* Bootstrapping pip By Default: Bootstrapping pip By Default<2>.
* Documentation Changes: Documentation Changes<2>.
What’s New in Python 2.6
* Python 3.0: Python 3 0.
* Changes to the Development Process::
* PEP 343; The ‘with’ statement: PEP 343 The ‘with’ statement.
* PEP 366; Explicit Relative Imports From a Main Module: PEP 366 Explicit Relative Imports From a Main Module.
* PEP 370; Per-user site-packages Directory: PEP 370 Per-user site-packages Directory.
* PEP 371; The multiprocessing Package: PEP 371 The multiprocessing Package.
* PEP 3101; Advanced String Formatting: PEP 3101 Advanced String Formatting.
* PEP 3105; print As a Function: PEP 3105 print As a Function.
* PEP 3110; Exception-Handling Changes: PEP 3110 Exception-Handling Changes.
* PEP 3112; Byte Literals: PEP 3112 Byte Literals.
* PEP 3116; New I/O Library: PEP 3116 New I/O Library.
* PEP 3118; Revised Buffer Protocol: PEP 3118 Revised Buffer Protocol.
* PEP 3119; Abstract Base Classes: PEP 3119 Abstract Base Classes.
* PEP 3127; Integer Literal Support and Syntax: PEP 3127 Integer Literal Support and Syntax.
* PEP 3129; Class Decorators: PEP 3129 Class Decorators.
* PEP 3141; A Type Hierarchy for Numbers: PEP 3141 A Type Hierarchy for Numbers.
* Other Language Changes: Other Language Changes<10>.
* New and Improved Modules: New and Improved Modules<2>.
* Deprecations and Removals::
* Build and C API Changes: Build and C API Changes<9>.
* Porting to Python 2.6: Porting to Python 2 6.
* Acknowledgements: Acknowledgements<2>.
Changes to the Development Process
* New Issue Tracker; Roundup: New Issue Tracker Roundup.
* New Documentation Format; reStructuredText Using Sphinx: New Documentation Format reStructuredText Using Sphinx.
PEP 343: The ‘with’ statement
* Writing Context Managers::
* The contextlib module::
PEP 3141: A Type Hierarchy for Numbers
* The fractions Module::
Other Language Changes
* Optimizations: Optimizations<9>.
* Interpreter Changes: Interpreter Changes<2>.
New and Improved Modules
* The ast module::
* The future_builtins module::
* The json module; JavaScript Object Notation: The json module JavaScript Object Notation.
* The plistlib module; A Property-List Parser: The plistlib module A Property-List Parser.
* ctypes Enhancements::
* Improved SSL Support::
Build and C API Changes
* Port-Specific Changes; Windows: Port-Specific Changes Windows<2>.
* Port-Specific Changes; Mac OS X: Port-Specific Changes Mac OS X<2>.
* Port-Specific Changes; IRIX: Port-Specific Changes IRIX.
What’s New in Python 2.5
* PEP 308; Conditional Expressions: PEP 308 Conditional Expressions.
* PEP 309; Partial Function Application: PEP 309 Partial Function Application.
* PEP 314; Metadata for Python Software Packages v1.1: PEP 314 Metadata for Python Software Packages v1 1.
* PEP 328; Absolute and Relative Imports: PEP 328 Absolute and Relative Imports.
* PEP 338; Executing Modules as Scripts: PEP 338 Executing Modules as Scripts.
* PEP 341; Unified try/except/finally: PEP 341 Unified try/except/finally.
* PEP 342; New Generator Features: PEP 342 New Generator Features.
* PEP 343; The ‘with’ statement: PEP 343 The ‘with’ statement<2>.
* PEP 352; Exceptions as New-Style Classes: PEP 352 Exceptions as New-Style Classes.
* PEP 353; Using ssize_t as the index type: PEP 353 Using ssize_t as the index type.
* PEP 357; The ‘__index__’ method: PEP 357 The ‘__index__’ method.
* Other Language Changes: Other Language Changes<11>.
* New, Improved, and Removed Modules: New Improved and Removed Modules.
* Build and C API Changes: Build and C API Changes<10>.
* Porting to Python 2.5: Porting to Python 2 5.
* Acknowledgements: Acknowledgements<3>.
PEP 343: The ‘with’ statement
* Writing Context Managers: Writing Context Managers<2>.
* The contextlib module: The contextlib module<2>.
Other Language Changes
* Interactive Interpreter Changes::
* Optimizations: Optimizations<10>.
New, Improved, and Removed Modules
* The ctypes package::
* The ElementTree package::
* The hashlib package::
* The sqlite3 package::
* The wsgiref package::
Build and C API Changes
* Port-Specific Changes::
What’s New in Python 2.4
* PEP 218; Built-In Set Objects: PEP 218 Built-In Set Objects.
* PEP 237; Unifying Long Integers and Integers: PEP 237 Unifying Long Integers and Integers.
* PEP 289; Generator Expressions: PEP 289 Generator Expressions.
* PEP 292; Simpler String Substitutions: PEP 292 Simpler String Substitutions.
* PEP 318; Decorators for Functions and Methods: PEP 318 Decorators for Functions and Methods.
* PEP 322; Reverse Iteration: PEP 322 Reverse Iteration.
* PEP 324; New subprocess Module: PEP 324 New subprocess Module.
* PEP 327; Decimal Data Type: PEP 327 Decimal Data Type.
* PEP 328; Multi-line Imports: PEP 328 Multi-line Imports.
* PEP 331; Locale-Independent Float/String Conversions: PEP 331 Locale-Independent Float/String Conversions.
* Other Language Changes: Other Language Changes<12>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules<3>.
* Build and C API Changes: Build and C API Changes<11>.
* Porting to Python 2.4: Porting to Python 2 4.
* Acknowledgements: Acknowledgements<4>.
PEP 327: Decimal Data Type
* Why is Decimal needed?::
* The Decimal type::
* The Context type::
Other Language Changes
* Optimizations: Optimizations<11>.
New, Improved, and Deprecated Modules
* cookielib::
* doctest: doctest<3>.
Build and C API Changes
* Port-Specific Changes: Port-Specific Changes<2>.
What’s New in Python 2.3
* PEP 218; A Standard Set Datatype: PEP 218 A Standard Set Datatype.
* PEP 255; Simple Generators: PEP 255 Simple Generators.
* PEP 263; Source Code Encodings: PEP 263 Source Code Encodings.
* PEP 273; Importing Modules from ZIP Archives: PEP 273 Importing Modules from ZIP Archives.
* PEP 277; Unicode file name support for Windows NT: PEP 277 Unicode file name support for Windows NT.
* PEP 278; Universal Newline Support: PEP 278 Universal Newline Support.
* PEP 279; enumerate(): PEP 279 enumerate.
* PEP 282; The logging Package: PEP 282 The logging Package.
* PEP 285; A Boolean Type: PEP 285 A Boolean Type.
* PEP 293; Codec Error Handling Callbacks: PEP 293 Codec Error Handling Callbacks.
* PEP 301; Package Index and Metadata for Distutils: PEP 301 Package Index and Metadata for Distutils.
* PEP 302; New Import Hooks: PEP 302 New Import Hooks.
* PEP 305; Comma-separated Files: PEP 305 Comma-separated Files.
* PEP 307; Pickle Enhancements: PEP 307 Pickle Enhancements.
* Extended Slices::
* Other Language Changes: Other Language Changes<13>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules<4>.
* Pymalloc; A Specialized Object Allocator: Pymalloc A Specialized Object Allocator.
* Build and C API Changes: Build and C API Changes<12>.
* Other Changes and Fixes: Other Changes and Fixes<2>.
* Porting to Python 2.3: Porting to Python 2 3.
* Acknowledgements: Acknowledgements<5>.
Other Language Changes
* String Changes::
* Optimizations: Optimizations<12>.
New, Improved, and Deprecated Modules
* Date/Time Type::
* The optparse Module::
Build and C API Changes
* Port-Specific Changes: Port-Specific Changes<3>.
What’s New in Python 2.2
* Introduction::
* PEPs 252 and 253; Type and Class Changes: PEPs 252 and 253 Type and Class Changes.
* PEP 234; Iterators: PEP 234 Iterators.
* PEP 255; Simple Generators: PEP 255 Simple Generators<2>.
* PEP 237; Unifying Long Integers and Integers: PEP 237 Unifying Long Integers and Integers<2>.
* PEP 238; Changing the Division Operator: PEP 238 Changing the Division Operator.
* Unicode Changes::
* PEP 227; Nested Scopes: PEP 227 Nested Scopes.
* New and Improved Modules: New and Improved Modules<3>.
* Interpreter Changes and Fixes::
* Other Changes and Fixes: Other Changes and Fixes<3>.
* Acknowledgements: Acknowledgements<6>.
PEPs 252 and 253: Type and Class Changes
* Old and New Classes::
* Descriptors::
* Multiple Inheritance; The Diamond Rule: Multiple Inheritance The Diamond Rule.
* Attribute Access::
* Related Links::
What’s New in Python 2.1
* Introduction: Introduction<2>.
* PEP 227; Nested Scopes: PEP 227 Nested Scopes<2>.
* PEP 236; __future__ Directives: PEP 236 __future__ Directives.
* PEP 207; Rich Comparisons: PEP 207 Rich Comparisons.
* PEP 230; Warning Framework: PEP 230 Warning Framework.
* PEP 229; New Build System: PEP 229 New Build System.
* PEP 205; Weak References: PEP 205 Weak References.
* PEP 232; Function Attributes: PEP 232 Function Attributes.
* PEP 235; Importing Modules on Case-Insensitive Platforms: PEP 235 Importing Modules on Case-Insensitive Platforms.
* PEP 217; Interactive Display Hook: PEP 217 Interactive Display Hook.
* PEP 208; New Coercion Model: PEP 208 New Coercion Model.
* PEP 241; Metadata in Python Packages: PEP 241 Metadata in Python Packages.
* New and Improved Modules: New and Improved Modules<4>.
* Other Changes and Fixes: Other Changes and Fixes<4>.
* Acknowledgements: Acknowledgements<7>.
What’s New in Python 2.0
* Introduction: Introduction<3>.
* What About Python 1.6?: What About Python 1 6?.
* New Development Process::
* Unicode: Unicode<2>.
* List Comprehensions::
* Augmented Assignment::
* String Methods::
* Garbage Collection of Cycles::
* Other Core Changes::
* Porting to 2.0: Porting to 2 0.
* Extending/Embedding Changes::
* Distutils; Making Modules Easy to Install: Distutils Making Modules Easy to Install.
* XML Modules::
* Module changes::
* New modules::
* IDLE Improvements::
* Deleted and Deprecated Modules::
* Acknowledgements: Acknowledgements<8>.
Other Core Changes
* Minor Language Changes::
* Changes to Built-in Functions::
XML Modules
* SAX2 Support::
* DOM Support::
* Relationship to PyXML::
The Python Tutorial
* Whetting Your Appetite::
* Using the Python Interpreter::
* An Informal Introduction to Python::
* More Control Flow Tools::
* Data Structures::
* Modules::
* Input and Output::
* Errors and Exceptions::
* Classes::
* Brief Tour of the Standard Library::
* Brief Tour of the Standard Library — Part II::
* Virtual Environments and Packages::
* What Now?::
* Interactive Input Editing and History Substitution::
* Floating Point Arithmetic; Issues and Limitations: Floating Point Arithmetic Issues and Limitations.
* Appendix::
Using the Python Interpreter
* Invoking the Interpreter::
* The Interpreter and Its Environment::
Invoking the Interpreter
* Argument Passing::
* Interactive Mode::
The Interpreter and Its Environment
* Source Code Encoding::
An Informal Introduction to Python
* Using Python as a Calculator::
* First Steps Towards Programming::
Using Python as a Calculator
* Numbers::
* Strings::
* Lists::
More Control Flow Tools
* if Statements::
* for Statements::
* The range() Function: The range Function.
* break and continue Statements, and else Clauses on Loops: break and continue Statements and else Clauses on Loops.
* pass Statements::
* Defining Functions::
* More on Defining Functions::
* Intermezzo; Coding Style: Intermezzo Coding Style.
More on Defining Functions
* Default Argument Values::
* Keyword Arguments::
* Special parameters::
* Arbitrary Argument Lists::
* Unpacking Argument Lists::
* Lambda Expressions::
* Documentation Strings::
* Function Annotations::
Special parameters
* Positional-or-Keyword Arguments::
* Positional-Only Parameters::
* Keyword-Only Arguments::
* Function Examples::
* Recap::
Data Structures
* More on Lists::
* The del statement::
* Tuples and Sequences::
* Sets::
* Dictionaries::
* Looping Techniques::
* More on Conditions::
* Comparing Sequences and Other Types::
More on Lists
* Using Lists as Stacks::
* Using Lists as Queues::
* List Comprehensions: List Comprehensions<2>.
* Nested List Comprehensions::
Modules
* More on Modules::
* Standard Modules::
* The dir() Function: The dir Function.
* Packages::
More on Modules
* Executing modules as scripts::
* The Module Search Path::
* “Compiled” Python files::
Packages
* Importing * From a Package::
* Intra-package References::
* Packages in Multiple Directories::
Input and Output
* Fancier Output Formatting::
* Reading and Writing Files::
Fancier Output Formatting
* Formatted String Literals::
* The String format() Method: The String format Method.
* Manual String Formatting::
* Old string formatting::
Reading and Writing Files
* Methods of File Objects::
* Saving structured data with json::
Errors and Exceptions
* Syntax Errors::
* Exceptions::
* Handling Exceptions::
* Raising Exceptions::
* User-defined Exceptions::
* Defining Clean-up Actions::
* Predefined Clean-up Actions::
Classes
* A Word About Names and Objects::
* Python Scopes and Namespaces::
* A First Look at Classes::
* Random Remarks::
* Inheritance::
* Private Variables::
* Odds and Ends::
* Iterators::
* Generators::
* Generator Expressions::
Python Scopes and Namespaces
* Scopes and Namespaces Example::
A First Look at Classes
* Class Definition Syntax::
* Class Objects::
* Instance Objects::
* Method Objects::
* Class and Instance Variables::
Inheritance
* Multiple Inheritance::
Brief Tour of the Standard Library
* Operating System Interface::
* File Wildcards::
* Command Line Arguments::
* Error Output Redirection and Program Termination::
* String Pattern Matching::
* Mathematics::
* Internet Access::
* Dates and Times::
* Data Compression::
* Performance Measurement::
* Quality Control::
* Batteries Included::
Brief Tour of the Standard Library — Part II
* Output Formatting::
* Templating::
* Working with Binary Data Record Layouts::
* Multi-threading: Multi-threading<2>.
* Logging::
* Weak References::
* Tools for Working with Lists::
* Decimal Floating Point Arithmetic::
Virtual Environments and Packages
* Introduction: Introduction<4>.
* Creating Virtual Environments::
* Managing Packages with pip::
Interactive Input Editing and History Substitution
* Tab Completion and History Editing::
* Alternatives to the Interactive Interpreter::
Floating Point Arithmetic: Issues and Limitations
* Representation Error::
Appendix
* Interactive Mode: Interactive Mode<2>.
Interactive Mode
* Error Handling::
* Executable Python Scripts::
* The Interactive Startup File::
* The Customization Modules::
Python Setup and Usage
* Command line and environment::
* Using Python on Unix platforms::
* Using Python on Windows::
* Using Python on a Macintosh::
* Editors and IDEs::
Command line and environment
* Command line::
* Environment variables::
Command line
* Interface options::
* Generic options::
* Miscellaneous options::
* Options you shouldn’t use::
Environment variables
* Debug-mode variables::
Using Python on Unix platforms
* Getting and installing the latest version of Python::
* Building Python::
* Python-related paths and files::
* Miscellaneous::
Getting and installing the latest version of Python
* On Linux::
* On FreeBSD and OpenBSD::
* On OpenSolaris::
Using Python on Windows
* The full installer::
* The Microsoft Store package::
* The nuget.org packages: The nuget org packages.
* The embeddable package::
* Alternative bundles::
* Configuring Python::
* UTF-8 mode::
* Python Launcher for Windows::
* Finding modules::
* Additional modules::
* Compiling Python on Windows::
* Other Platforms::
The full installer
* Installation steps::
* Removing the MAX_PATH Limitation::
* Installing Without UI::
* Installing Without Downloading::
* Modifying an install::
The Microsoft Store package
* Known Issues::
The embeddable package
* Python Application::
* Embedding Python::
Configuring Python
* Excursus; Setting environment variables: Excursus Setting environment variables.
* Finding the Python executable::
Python Launcher for Windows
* Getting started::
* Shebang Lines::
* Arguments in shebang lines::
* Customization::
* Diagnostics::
Getting started
* From the command-line::
* Virtual environments::
* From a script::
* From file associations::
Customization
* Customization via INI files::
* Customizing default Python versions::
Additional modules
* PyWin32::
* cx_Freeze::
* WConio::
Using Python on a Macintosh
* Getting and Installing MacPython::
* The IDE::
* Installing Additional Python Packages::
* GUI Programming on the Mac::
* Distributing Python Applications on the Mac::
* Other Resources::
Getting and Installing MacPython
* How to run a Python script::
* Running scripts with a GUI::
* Configuration::
The Python Language Reference
* Introduction: Introduction<5>.
* Lexical analysis::
* Data model::
* Execution model::
* The import system::
* Expressions::
* Simple statements::
* Compound statements::
* Top-level components::
* Full Grammar specification::
Introduction
* Alternate Implementations::
* Notation::
Lexical analysis
* Line structure::
* Other tokens::
* Identifiers and keywords::
* Literals::
* Operators::
* Delimiters::
Line structure
* Logical lines::
* Physical lines::
* Comments::
* Encoding declarations::
* Explicit line joining::
* Implicit line joining::
* Blank lines::
* Indentation::
* Whitespace between tokens::
Identifiers and keywords
* Keywords::
* Reserved classes of identifiers::
Literals
* String and Bytes literals::
* String literal concatenation::
* Formatted string literals::
* Numeric literals::
* Integer literals::
* Floating point literals::
* Imaginary literals::
Data model
* Objects, values and types: Objects values and types.
* The standard type hierarchy::
* Special method names::
* Coroutines::
Special method names
* Basic customization::
* Customizing attribute access::
* Customizing class creation::
* Customizing instance and subclass checks::
* Emulating generic types::
* Emulating callable objects::
* Emulating container types::
* Emulating numeric types::
* With Statement Context Managers::
* Special method lookup::
Customizing attribute access
* Customizing module attribute access::
* Implementing Descriptors::
* Invoking Descriptors::
* __slots__::
__slots__
* Notes on using __slots__::
Customizing class creation
* Metaclasses::
* Resolving MRO entries::
* Determining the appropriate metaclass::
* Preparing the class namespace::
* Executing the class body::
* Creating the class object::
* Uses for metaclasses::
Coroutines
* Awaitable Objects::
* Coroutine Objects::
* Asynchronous Iterators::
* Asynchronous Context Managers::
Execution model
* Structure of a program::
* Naming and binding::
* Exceptions: Exceptions<2>.
Naming and binding
* Binding of names::
* Resolution of names::
* Builtins and restricted execution::
* Interaction with dynamic features::
The import system
* importlib: importlib<7>.
* Packages: Packages<2>.
* Searching::
* Loading::
* The Path Based Finder::
* Replacing the standard import system::
* Package Relative Imports::
* Special considerations for __main__::
* Open issues::
* References::
Packages
* Regular packages::
* Namespace packages::
Searching
* The module cache::
* Finders and loaders::
* Import hooks::
* The meta path::
Loading
* Loaders::
* Submodules::
* Module spec::
* Import-related module attributes::
* module.__path__: module __path__.
* Module reprs::
* Cached bytecode invalidation::
The Path Based Finder
* Path entry finders::
* Path entry finder protocol::
Special considerations for __main__
* __main__.__spec__: __main__ __spec__.
Expressions
* Arithmetic conversions::
* Atoms::
* Primaries::
* Await expression::
* The power operator::
* Unary arithmetic and bitwise operations::
* Binary arithmetic operations::
* Shifting operations::
* Binary bitwise operations::
* Comparisons::
* Boolean operations::
* Assignment expressions: Assignment expressions<2>.
* Conditional expressions::
* Lambdas::
* Expression lists::
* Evaluation order::
* Operator precedence::
Atoms
* Identifiers (Names): Identifiers Names.
* Literals: Literals<2>.
* Parenthesized forms::
* Displays for lists, sets and dictionaries: Displays for lists sets and dictionaries.
* List displays::
* Set displays::
* Dictionary displays::
* Generator expressions::
* Yield expressions::
Yield expressions
* Generator-iterator methods::
* Examples::
* Asynchronous generator functions::
* Asynchronous generator-iterator methods::
Primaries
* Attribute references::
* Subscriptions::
* Slicings::
* Calls::
Comparisons
* Value comparisons::
* Membership test operations::
* Identity comparisons::
Simple statements
* Expression statements::
* Assignment statements::
* The assert statement::
* The pass statement::
* The del statement: The del statement<2>.
* The return statement::
* The yield statement::
* The raise statement::
* The break statement::
* The continue statement::
* The import statement::
* The global statement::
* The nonlocal statement::
Assignment statements
* Augmented assignment statements::
* Annotated assignment statements::
The import statement
* Future statements::
Compound statements
* The if statement::
* The while statement::
* The for statement::
* The try statement::
* The with statement::
* Function definitions::
* Class definitions::
* Coroutines: Coroutines<2>.
Coroutines
* Coroutine function definition::
* The async for statement::
* The async with statement::
Top-level components
* Complete Python programs::
* File input::
* Interactive input::
* Expression input::
The Python Standard Library
* Introduction: Introduction<6>.
* Built-in Functions::
* Built-in Constants::
* Built-in Types::
* Built-in Exceptions::
* Text Processing Services::
* Binary Data Services::
* Data Types::
* Numeric and Mathematical Modules::
* Functional Programming Modules::
* File and Directory Access::
* Data Persistence::
* Data Compression and Archiving::
* File Formats::
* Cryptographic Services::
* Generic Operating System Services::
* Concurrent Execution::
* Networking and Interprocess Communication::
* Internet Data Handling::
* Structured Markup Processing Tools::
* Internet Protocols and Support::
* Multimedia Services::
* Internationalization::
* Program Frameworks::
* Graphical User Interfaces with Tk::
* Development Tools::
* Debugging and Profiling::
* Software Packaging and Distribution::
* Python Runtime Services::
* Custom Python Interpreters::
* Importing Modules::
* Python Language Services::
* Miscellaneous Services::
* MS Windows Specific Services::
* Unix Specific Services::
* Superseded Modules::
* Undocumented Modules::
Introduction
* Notes on availability::
Built-in Constants
* Constants added by the site module::
Built-in Types
* Truth Value Testing::
* Boolean Operations — and, or, not: Boolean Operations — and or not.
* Comparisons: Comparisons<2>.
* Numeric Types — int, float, complex: Numeric Types — int float complex.
* Iterator Types::
* Sequence Types — list, tuple, range: Sequence Types — list tuple range.
* Text Sequence Type — str::
* Binary Sequence Types — bytes, bytearray, memoryview: Binary Sequence Types — bytes bytearray memoryview.
* Set Types — set, frozenset: Set Types — set frozenset.
* Mapping Types — dict::
* Context Manager Types::
* Other Built-in Types::
* Special Attributes::
Numeric Types — int, float, complex
* Bitwise Operations on Integer Types::
* Additional Methods on Integer Types::
* Additional Methods on Float::
* Hashing of numeric types::
Iterator Types
* Generator Types::
Sequence Types — list, tuple, range
* Common Sequence Operations::
* Immutable Sequence Types::
* Mutable Sequence Types::
* Lists: Lists<2>.
* Tuples::
* Ranges::
Text Sequence Type — str
* String Methods: String Methods<2>.
* printf-style String Formatting::
Binary Sequence Types — bytes, bytearray, memoryview
* Bytes Objects::
* Bytearray Objects::
* Bytes and Bytearray Operations::
* printf-style Bytes Formatting::
* Memory Views::
Mapping Types — dict
* Dictionary view objects::
Other Built-in Types
* Modules: Modules<2>.
* Classes and Class Instances::
* Functions::
* Methods::
* Code Objects::
* Type Objects::
* The Null Object::
* The Ellipsis Object::
* The NotImplemented Object::
* Boolean Values::
* Internal Objects::
Built-in Exceptions
* Base classes::
* Concrete exceptions::
* Warnings::
* Exception hierarchy::
Concrete exceptions
* OS exceptions::
Text Processing Services
* string — Common string operations::
* re — Regular expression operations::
* difflib — Helpers for computing deltas::
* textwrap — Text wrapping and filling::
* unicodedata — Unicode Database::
* stringprep — Internet String Preparation::
* readline — GNU readline interface::
* rlcompleter — Completion function for GNU readline::
string — Common string operations
* String constants::
* Custom String Formatting::
* Format String Syntax::
* Template strings::
* Helper functions::
Format String Syntax
* Format Specification Mini-Language::
* Format examples::
re — Regular expression operations
* Regular Expression Syntax::
* Module Contents::
* Regular Expression Objects::
* Match Objects::
* Regular Expression Examples::
Regular Expression Examples
* Checking for a Pair::
* Simulating scanf(): Simulating scanf.
* search() vs. match(): search vs match.
* Making a Phonebook::
* Text Munging::
* Finding all Adverbs::
* Finding all Adverbs and their Positions::
* Raw String Notation::
* Writing a Tokenizer::
difflib — Helpers for computing deltas
* SequenceMatcher Objects::
* SequenceMatcher Examples::
* Differ Objects::
* Differ Example::
* A command-line interface to difflib::
readline — GNU readline interface
* Init file::
* Line buffer::
* History file::
* History list::
* Startup hooks::
* Completion::
* Example::
rlcompleter — Completion function for GNU readline
* Completer Objects::
Binary Data Services
* struct — Interpret bytes as packed binary data::
* codecs — Codec registry and base classes::
struct — Interpret bytes as packed binary data
* Functions and Exceptions::
* Format Strings::
* Classes: Classes<2>.
Format Strings
* Byte Order, Size, and Alignment: Byte Order Size and Alignment.
* Format Characters::
* Examples: Examples<2>.
codecs — Codec registry and base classes
* Codec Base Classes::
* Encodings and Unicode::
* Standard Encodings::
* Python Specific Encodings::
* encodings.idna — Internationalized Domain Names in Applications: encodings idna — Internationalized Domain Names in Applications.
* encodings.mbcs — Windows ANSI codepage: encodings mbcs — Windows ANSI codepage.
* encodings.utf_8_sig — UTF-8 codec with BOM signature: encodings utf_8_sig — UTF-8 codec with BOM signature.
Codec Base Classes
* Error Handlers::
* Stateless Encoding and Decoding::
* Incremental Encoding and Decoding::
* Stream Encoding and Decoding::
Incremental Encoding and Decoding
* IncrementalEncoder Objects::
* IncrementalDecoder Objects::
Stream Encoding and Decoding
* StreamWriter Objects::
* StreamReader Objects::
* StreamReaderWriter Objects::
* StreamRecoder Objects::
Python Specific Encodings
* Text Encodings::
* Binary Transforms::
* Text Transforms::
Data Types
* datetime — Basic date and time types::
* calendar — General calendar-related functions::
* collections — Container datatypes::
* collections.abc — Abstract Base Classes for Containers: collections abc — Abstract Base Classes for Containers.
* heapq — Heap queue algorithm::
* bisect — Array bisection algorithm::
* array — Efficient arrays of numeric values::
* weakref — Weak references::
* types — Dynamic type creation and names for built-in types::
* copy — Shallow and deep copy operations::
* pprint — Data pretty printer::
* reprlib — Alternate repr() implementation: reprlib — Alternate repr implementation.
* enum — Support for enumerations::
datetime — Basic date and time types
* Aware and Naive Objects::
* Constants::
* Available Types::
* timedelta Objects::
* date Objects::
* datetime Objects::
* time Objects::
* tzinfo Objects::
* timezone Objects::
* strftime() and strptime() Behavior: strftime and strptime Behavior.
Available Types
* Common Properties::
* Determining if an Object is Aware or Naive::
timedelta Objects
* Examples of usage; timedelta: Examples of usage timedelta.
date Objects
* Examples of Usage; date: Examples of Usage date.
datetime Objects
* Examples of Usage; datetime: Examples of Usage datetime.
time Objects
* Examples of Usage; time: Examples of Usage time.
strftime() and strptime() Behavior
* strftime() and strptime() Format Codes: strftime and strptime Format Codes.
* Technical Detail::
collections — Container datatypes
* ChainMap objects::
* Counter objects::
* deque objects::
* defaultdict objects::
* namedtuple() Factory Function for Tuples with Named Fields: namedtuple Factory Function for Tuples with Named Fields.
* OrderedDict objects::
* UserDict objects::
* UserList objects::
* UserString objects::
ChainMap objects
* ChainMap Examples and Recipes::
deque objects
* deque Recipes::
defaultdict objects
* defaultdict Examples::
OrderedDict objects
* OrderedDict Examples and Recipes::
collections.abc — Abstract Base Classes for Containers
* Collections Abstract Base Classes::
heapq — Heap queue algorithm
* Basic Examples::
* Priority Queue Implementation Notes::
* Theory::
bisect — Array bisection algorithm
* Searching Sorted Lists::
* Other Examples::
weakref — Weak references
* Weak Reference Objects::
* Example: Example<2>.
* Finalizer Objects::
* Comparing finalizers with __del__() methods: Comparing finalizers with __del__ methods.
types — Dynamic type creation and names for built-in types
* Dynamic Type Creation::
* Standard Interpreter Types::
* Additional Utility Classes and Functions::
* Coroutine Utility Functions::
pprint — Data pretty printer
* PrettyPrinter Objects::
* Example: Example<3>.
reprlib — Alternate repr() implementation
* Repr Objects::
* Subclassing Repr Objects::
enum — Support for enumerations
* Module Contents: Module Contents<2>.
* Creating an Enum::
* Programmatic access to enumeration members and their attributes::
* Duplicating enum members and values::
* Ensuring unique enumeration values::
* Using automatic values::
* Iteration::
* Comparisons: Comparisons<3>.
* Allowed members and attributes of enumerations::
* Restricted Enum subclassing::
* Pickling::
* Functional API::
* Derived Enumerations::
* When to use __new__() vs. __init__(): When to use __new__ vs __init__.
* Interesting examples::
* How are Enums different?::
Derived Enumerations
* IntEnum::
* IntFlag::
* Flag::
* Others::
Interesting examples
* Omitting values::
* OrderedEnum::
* DuplicateFreeEnum::
* Planet::
* TimePeriod::
Omitting values
* Using auto::
* Using object::
* Using a descriptive string::
* Using a custom __new__(): Using a custom __new__.
How are Enums different?
* Enum Classes::
* Enum Members (aka instances): Enum Members aka instances.
* Finer Points::
Finer Points
* Supported __dunder__ names::
* Supported _sunder_ names::
* Enum member type::
* Boolean value of Enum classes and members::
* Enum classes with methods::
* Combining members of Flag::
Numeric and Mathematical Modules
* numbers — Numeric abstract base classes::
* math — Mathematical functions::
* cmath — Mathematical functions for complex numbers::
* decimal — Decimal fixed point and floating point arithmetic::
* fractions — Rational numbers::
* random — Generate pseudo-random numbers::
* statistics — Mathematical statistics functions::
numbers — Numeric abstract base classes
* The numeric tower::
* Notes for type implementors::
Notes for type implementors
* Adding More Numeric ABCs::
* Implementing the arithmetic operations::
math — Mathematical functions
* Number-theoretic and representation functions::
* Power and logarithmic functions::
* Trigonometric functions::
* Angular conversion::
* Hyperbolic functions::
* Special functions::
* Constants: Constants<2>.
cmath — Mathematical functions for complex numbers
* Conversions to and from polar coordinates::
* Power and logarithmic functions: Power and logarithmic functions<2>.
* Trigonometric functions: Trigonometric functions<2>.
* Hyperbolic functions: Hyperbolic functions<2>.
* Classification functions::
* Constants: Constants<3>.
decimal — Decimal fixed point and floating point arithmetic
* Quick-start Tutorial::
* Decimal objects::
* Context objects::
* Constants: Constants<4>.
* Rounding modes::
* Signals::
* Floating Point Notes::
* Working with threads::
* Recipes::
* Decimal FAQ::
Decimal objects
* Logical operands::
Floating Point Notes
* Mitigating round-off error with increased precision::
* Special values::
random — Generate pseudo-random numbers
* Bookkeeping functions::
* Functions for integers::
* Functions for sequences::
* Real-valued distributions::
* Alternative Generator::
* Notes on Reproducibility::
* Examples and Recipes::
statistics — Mathematical statistics functions
* Averages and measures of central location::
* Measures of spread::
* Function details::
* Exceptions: Exceptions<3>.
* NormalDist objects::
NormalDist objects
* NormalDist Examples and Recipes::
Functional Programming Modules
* itertools — Functions creating iterators for efficient looping::
* functools — Higher-order functions and operations on callable objects::
* operator — Standard operators as functions::
itertools — Functions creating iterators for efficient looping
* Itertool functions::
* Itertools Recipes::
functools — Higher-order functions and operations on callable objects
* partial Objects::
operator — Standard operators as functions
* Mapping Operators to Functions::
* In-place Operators::
File and Directory Access
* pathlib — Object-oriented filesystem paths::
* os.path — Common pathname manipulations: os path — Common pathname manipulations.
* fileinput — Iterate over lines from multiple input streams::
* stat — Interpreting stat() results: stat — Interpreting stat results.
* filecmp — File and Directory Comparisons::
* tempfile — Generate temporary files and directories::
* glob — Unix style pathname pattern expansion::
* fnmatch — Unix filename pattern matching::
* linecache — Random access to text lines::
* shutil — High-level file operations::
pathlib — Object-oriented filesystem paths
* Basic use::
* Pure paths::
* Concrete paths::
* Correspondence to tools in the os module::
Pure paths
* General properties::
* Operators: Operators<2>.
* Accessing individual parts::
* Methods and properties::
Concrete paths
* Methods: Methods<2>.
filecmp — File and Directory Comparisons
* The dircmp class::
tempfile — Generate temporary files and directories
* Examples: Examples<3>.
* Deprecated functions and variables::
shutil — High-level file operations
* Directory and files operations::
* Archiving operations::
* Querying the size of the output terminal::
Directory and files operations
* Platform-dependent efficient copy operations::
* copytree example::
* rmtree example::
Archiving operations
* Archiving example::
* Archiving example with base_dir::
Data Persistence
* pickle — Python object serialization::
* copyreg — Register pickle support functions::
* shelve — Python object persistence::
* marshal — Internal Python object serialization::
* dbm — Interfaces to Unix “databases”::
* sqlite3 — DB-API 2.0 interface for SQLite databases: sqlite3 — DB-API 2 0 interface for SQLite databases.
pickle — Python object serialization
* Relationship to other Python modules::
* Data stream format::
* Module Interface::
* What can be pickled and unpickled?::
* Pickling Class Instances::
* Custom Reduction for Types, Functions, and Other Objects: Custom Reduction for Types Functions and Other Objects.
* Out-of-band Buffers::
* Restricting Globals::
* Performance: Performance<2>.
* Examples: Examples<4>.
Relationship to other Python modules
* Comparison with marshal::
* Comparison with json::
Pickling Class Instances
* Persistence of External Objects::
* Dispatch Tables::
* Handling Stateful Objects::
Out-of-band Buffers
* Provider API::
* Consumer API::
* Example: Example<4>.
copyreg — Register pickle support functions
* Example: Example<5>.
shelve — Python object persistence
* Restrictions::
* Example: Example<6>.
dbm — Interfaces to Unix “databases”
* dbm.gnu — GNU’s reinterpretation of dbm: dbm gnu — GNU’s reinterpretation of dbm.
* dbm.ndbm — Interface based on ndbm: dbm ndbm — Interface based on ndbm.
* dbm.dumb — Portable DBM implementation: dbm dumb — Portable DBM implementation.
sqlite3 — DB-API 2.0 interface for SQLite databases
* Module functions and constants::
* Connection Objects::
* Cursor Objects::
* Row Objects::
* Exceptions: Exceptions<4>.
* SQLite and Python types::
* Controlling Transactions::
* Using sqlite3 efficiently::
SQLite and Python types
* Introduction: Introduction<7>.
* Using adapters to store additional Python types in SQLite databases::
* Converting SQLite values to custom Python types::
* Default adapters and converters::
Using adapters to store additional Python types in SQLite databases
* Letting your object adapt itself::
* Registering an adapter callable::
Using sqlite3 efficiently
* Using shortcut methods::
* Accessing columns by name instead of by index::
* Using the connection as a context manager::
Data Compression and Archiving
* zlib — Compression compatible with gzip::
* gzip — Support for gzip files::
* bz2 — Support for bzip2 compression::
* lzma — Compression using the LZMA algorithm::
* zipfile — Work with ZIP archives::
* tarfile — Read and write tar archive files::
gzip — Support for gzip files
* Examples of usage::
* Command Line Interface::
Command Line Interface
* Command line options::
bz2 — Support for bzip2 compression
* (De)compression of files: De compression of files.
* Incremental (de)compression: Incremental de compression.
* One-shot (de)compression: One-shot de compression.
* Examples of usage: Examples of usage<2>.
lzma — Compression using the LZMA algorithm
* Reading and writing compressed files::
* Compressing and decompressing data in memory::
* Miscellaneous: Miscellaneous<2>.
* Specifying custom filter chains::
* Examples: Examples<5>.
zipfile — Work with ZIP archives
* ZipFile Objects::
* Path Objects::
* PyZipFile Objects::
* ZipInfo Objects::
* Command-Line Interface::
* Decompression pitfalls::
Command-Line Interface
* Command-line options::
Decompression pitfalls
* From file itself::
* File System limitations::
* Resources limitations::
* Interruption::
* Default behaviors of extraction::
tarfile — Read and write tar archive files
* TarFile Objects::
* TarInfo Objects::
* Command-Line Interface: Command-Line Interface<2>.
* Examples: Examples<6>.
* Supported tar formats::
* Unicode issues::
Command-Line Interface
* Command-line options: Command-line options<2>.
File Formats
* csv — CSV File Reading and Writing::
* configparser — Configuration file parser::
* netrc — netrc file processing::
* xdrlib — Encode and decode XDR data::
* plistlib — Generate and parse Mac OS X .plist files: plistlib — Generate and parse Mac OS X plist files.
csv — CSV File Reading and Writing
* Module Contents: Module Contents<3>.
* Dialects and Formatting Parameters::
* Reader Objects::
* Writer Objects::
* Examples: Examples<7>.
configparser — Configuration file parser
* Quick Start::
* Supported Datatypes::
* Fallback Values::
* Supported INI File Structure::
* Interpolation of values::
* Mapping Protocol Access::
* Customizing Parser Behaviour::
* Legacy API Examples::
* ConfigParser Objects::
* RawConfigParser Objects::
* Exceptions: Exceptions<5>.
netrc — netrc file processing
* netrc Objects::
xdrlib — Encode and decode XDR data
* Packer Objects::
* Unpacker Objects::
* Exceptions: Exceptions<6>.
plistlib — Generate and parse Mac OS X .plist files
* Examples: Examples<8>.
Cryptographic Services
* hashlib — Secure hashes and message digests::
* hmac — Keyed-Hashing for Message Authentication::
* secrets — Generate secure random numbers for managing secrets::
hashlib — Secure hashes and message digests
* Hash algorithms::
* SHAKE variable length digests::
* Key derivation::
* BLAKE2::
BLAKE2
* Creating hash objects::
* Constants: Constants<5>.
* Examples: Examples<9>.
* Credits::
Examples
* Simple hashing::
* Using different digest sizes::
* Keyed hashing::
* Randomized hashing::
* Personalization::
* Tree mode::
secrets — Generate secure random numbers for managing secrets
* Random numbers::
* Generating tokens::
* Other functions::
* Recipes and best practices::
Generating tokens
* How many bytes should tokens use?::
Generic Operating System Services
* os — Miscellaneous operating system interfaces::
* io — Core tools for working with streams::
* time — Time access and conversions::
* argparse — Parser for command-line options, arguments and sub-commands: argparse — Parser for command-line options arguments and sub-commands.
* getopt — C-style parser for command line options::
* logging — Logging facility for Python::
* logging.config — Logging configuration: logging config — Logging configuration.
* logging.handlers — Logging handlers: logging handlers — Logging handlers.
* getpass — Portable password input::
* curses — Terminal handling for character-cell displays::
* curses.textpad — Text input widget for curses programs: curses textpad — Text input widget for curses programs.
* curses.ascii — Utilities for ASCII characters: curses ascii — Utilities for ASCII characters.
* curses.panel — A panel stack extension for curses: curses panel — A panel stack extension for curses.
* platform — Access to underlying platform’s identifying data::
* errno — Standard errno system symbols::
* ctypes — A foreign function library for Python::
os — Miscellaneous operating system interfaces
* File Names, Command Line Arguments, and Environment Variables: File Names Command Line Arguments and Environment Variables.
* Process Parameters::
* File Object Creation::
* File Descriptor Operations::
* Files and Directories::
* Process Management::
* Interface to the scheduler::
* Miscellaneous System Information::
* Random numbers: Random numbers<2>.
File Descriptor Operations
* Querying the size of a terminal::
* Inheritance of File Descriptors::
Files and Directories
* Linux extended attributes::
io — Core tools for working with streams
* Overview::
* High-level Module Interface::
* Class hierarchy::
* Performance: Performance<3>.
Overview
* Text I/O::
* Binary I/O::
* Raw I/O::
Class hierarchy
* I/O Base Classes::
* Raw File I/O::
* Buffered Streams::
* Text I/O: Text I/O<2>.
Performance
* Binary I/O: Binary I/O<2>.
* Text I/O: Text I/O<3>.
* Multi-threading: Multi-threading<3>.
* Reentrancy::
time — Time access and conversions
* Functions: Functions<2>.
* Clock ID Constants::
* Timezone Constants::
argparse — Parser for command-line options, arguments and sub-commands
* Example: Example<7>.
* ArgumentParser objects::
* The add_argument() method: The add_argument method.
* The parse_args() method: The parse_args method.
* Other utilities::
* Upgrading optparse code::
Example
* Creating a parser::
* Adding arguments::
* Parsing arguments::
ArgumentParser objects
* prog::
* usage::
* description::
* epilog::
* parents::
* formatter_class::
* prefix_chars::
* fromfile_prefix_chars::
* argument_default::
* allow_abbrev::
* conflict_handler::
* add_help::
The add_argument() method
* name or flags::
* action::
* nargs::
* const::
* default::
* type::
* choices::
* required::
* help::
* metavar::
* dest::
* Action classes::
The parse_args() method
* Option value syntax::
* Invalid arguments::
* Arguments containing -::
* Argument abbreviations (prefix matching): Argument abbreviations prefix matching.
* Beyond sys.argv: Beyond sys argv.
* The Namespace object::
Other utilities
* Sub-commands::
* FileType objects::
* Argument groups::
* Mutual exclusion::
* Parser defaults::
* Printing help::
* Partial parsing::
* Customizing file parsing::
* Exiting methods::
* Intermixed parsing::
logging — Logging facility for Python
* Logger Objects::
* Logging Levels::
* Handler Objects::
* Formatter Objects::
* Filter Objects::
* LogRecord Objects::
* LogRecord attributes::
* LoggerAdapter Objects::
* Thread Safety::
* Module-Level Functions::
* Module-Level Attributes::
* Integration with the warnings module::
logging.config — Logging configuration
* Configuration functions::
* Configuration dictionary schema::
* Configuration file format::
Configuration dictionary schema
* Dictionary Schema Details::
* Incremental Configuration::
* Object connections::
* User-defined objects::
* Access to external objects::
* Access to internal objects::
* Import resolution and custom importers::
logging.handlers — Logging handlers
* StreamHandler::
* FileHandler::
* NullHandler::
* WatchedFileHandler::
* BaseRotatingHandler::
* RotatingFileHandler::
* TimedRotatingFileHandler::
* SocketHandler::
* DatagramHandler::
* SysLogHandler::
* NTEventLogHandler::
* SMTPHandler::
* MemoryHandler::
* HTTPHandler::
* QueueHandler::
* QueueListener::
curses — Terminal handling for character-cell displays
* Functions: Functions<3>.
* Window Objects::
* Constants: Constants<6>.
curses.textpad — Text input widget for curses programs
* Textbox objects::
curses.panel — A panel stack extension for curses
* Functions: Functions<4>.
* Panel Objects::
platform — Access to underlying platform’s identifying data
* Cross Platform::
* Java Platform::
* Windows Platform::
* Mac OS Platform::
* Unix Platforms::
ctypes — A foreign function library for Python
* ctypes tutorial::
* ctypes reference::
ctypes tutorial
* Loading dynamic link libraries::
* Accessing functions from loaded dlls::
* Calling functions::
* Fundamental data types::
* Calling functions, continued: Calling functions continued.
* Calling functions with your own custom data types::
* Specifying the required argument types (function prototypes): Specifying the required argument types function prototypes.
* Return types::
* Passing pointers (or; passing parameters by reference): Passing pointers or passing parameters by reference.
* Structures and unions::
* Structure/union alignment and byte order::
* Bit fields in structures and unions::
* Arrays::
* Pointers::
* Type conversions::
* Incomplete Types::
* Callback functions::
* Accessing values exported from dlls::
* Surprises::
* Variable-sized data types::
ctypes reference
* Finding shared libraries::
* Loading shared libraries::
* Foreign functions::
* Function prototypes::
* Utility functions::
* Data types::
* Fundamental data types: Fundamental data types<2>.
* Structured data types::
* Arrays and pointers::
Concurrent Execution
* threading — Thread-based parallelism::
* multiprocessing — Process-based parallelism::
* multiprocessing.shared_memory — Provides shared memory for direct access across processes: multiprocessing shared_memory — Provides shared memory for direct access across processes.
* The concurrent package::
* concurrent.futures — Launching parallel tasks: concurrent futures — Launching parallel tasks.
* subprocess — Subprocess management::
* sched — Event scheduler::
* queue — A synchronized queue class::
* contextvars — Context Variables::
* _thread — Low-level threading API::
* _dummy_thread — Drop-in replacement for the _thread module::
* dummy_threading — Drop-in replacement for the threading module::
threading — Thread-based parallelism
* Thread-Local Data::
* Thread Objects::
* Lock Objects::
* RLock Objects::
* Condition Objects::
* Semaphore Objects::
* Event Objects::
* Timer Objects::
* Barrier Objects::
* Using locks, conditions, and semaphores in the with statement: Using locks conditions and semaphores in the with statement.
Semaphore Objects
* Semaphore Example::
multiprocessing — Process-based parallelism
* Introduction: Introduction<8>.
* Reference::
* Programming guidelines::
* Examples: Examples<10>.
Introduction
* The Process class::
* Contexts and start methods::
* Exchanging objects between processes::
* Synchronization between processes::
* Sharing state between processes::
* Using a pool of workers::
Reference
* Process and exceptions::
* Pipes and Queues::
* Miscellaneous: Miscellaneous<3>.
* Connection Objects: Connection Objects<2>.
* Synchronization primitives::
* Shared ctypes Objects::
* Managers::
* Proxy Objects::
* Process Pools::
* Listeners and Clients::
* Authentication keys::
* Logging: Logging<2>.
* The multiprocessing.dummy module: The multiprocessing dummy module.
Shared ctypes Objects
* The multiprocessing.sharedctypes module: The multiprocessing sharedctypes module.
Managers
* Customized managers::
* Using a remote manager::
Proxy Objects
* Cleanup::
Listeners and Clients
* Address Formats::
Programming guidelines
* All start methods::
* The spawn and forkserver start methods::
concurrent.futures — Launching parallel tasks
* Executor Objects::
* ThreadPoolExecutor::
* ProcessPoolExecutor::
* Future Objects::
* Module Functions::
* Exception classes::
ThreadPoolExecutor
* ThreadPoolExecutor Example::
ProcessPoolExecutor
* ProcessPoolExecutor Example::
subprocess — Subprocess management
* Using the subprocess Module::
* Security Considerations::
* Popen Objects::
* Windows Popen Helpers::
* Older high-level API::
* Replacing Older Functions with the subprocess Module::
* Legacy Shell Invocation Functions::
* Notes::
Using the subprocess Module
* Frequently Used Arguments::
* Popen Constructor::
* Exceptions: Exceptions<7>.
Windows Popen Helpers
* Windows Constants::
Replacing Older Functions with the subprocess Module
* Replacing /bin/sh shell command substitution::
* Replacing shell pipeline::
* Replacing os.system(): Replacing os system.
* Replacing the os.spawn family: Replacing the os spawn family.
* Replacing os.popen(), os.popen2(), os.popen3(): Replacing os popen os popen2 os popen3.
* Replacing functions from the popen2 module::
Notes
* Converting an argument sequence to a string on Windows::
sched — Event scheduler
* Scheduler Objects::
queue — A synchronized queue class
* Queue Objects::
* SimpleQueue Objects::
contextvars — Context Variables
* Context Variables::
* Manual Context Management::
* asyncio support::
Networking and Interprocess Communication
* asyncio — Asynchronous I/O::
* socket — Low-level networking interface::
* ssl — TLS/SSL wrapper for socket objects::
* select — Waiting for I/O completion::
* selectors — High-level I/O multiplexing::
* asyncore — Asynchronous socket handler::
* asynchat — Asynchronous socket command/response handler::
* signal — Set handlers for asynchronous events::
* mmap — Memory-mapped file support::
asyncio — Asynchronous I/O
* Coroutines and Tasks::
* Streams::
* Synchronization Primitives::
* Subprocesses::
* Queues::
* Exceptions: Exceptions<9>.
* Event Loop::
* Futures::
* Transports and Protocols::
* Policies::
* Platform Support::
* High-level API Index::
* Low-level API Index::
* Developing with asyncio::
Coroutines and Tasks
* Coroutines: Coroutines<3>.
* Awaitables::
* Running an asyncio Program::
* Creating Tasks::
* Sleeping::
* Running Tasks Concurrently::
* Shielding From Cancellation::
* Timeouts::
* Waiting Primitives::
* Scheduling From Other Threads::
* Introspection::
* Task Object::
* Generator-based Coroutines::
Streams
* StreamReader::
* StreamWriter::
* Examples: Examples<11>.
Examples
* TCP echo client using streams::
* TCP echo server using streams::
* Get HTTP headers::
* Register an open socket to wait for data using streams::
Synchronization Primitives
* Lock::
* Event::
* Condition::
* Semaphore::
* BoundedSemaphore::
Subprocesses
* Creating Subprocesses::
* Constants: Constants<7>.
* Interacting with Subprocesses::
Interacting with Subprocesses
* Subprocess and Threads::
* Examples: Examples<12>.
Queues
* Queue::
* Priority Queue::
* LIFO Queue::
* Exceptions: Exceptions<8>.
* Examples: Examples<13>.
Event Loop
* Event Loop Methods::
* Callback Handles::
* Server Objects::
* Event Loop Implementations::
* Examples: Examples<14>.
Event Loop Methods
* Running and stopping the loop::
* Scheduling callbacks::
* Scheduling delayed callbacks::
* Creating Futures and Tasks::
* Opening network connections::
* Creating network servers::
* Transferring files::
* TLS Upgrade::
* Watching file descriptors::
* Working with socket objects directly::
* DNS::
* Working with pipes::
* Unix signals::
* Executing code in thread or process pools::
* Error Handling API::
* Enabling debug mode::
* Running Subprocesses::
Examples
* Hello World with call_soon(): Hello World with call_soon.
* Display the current date with call_later(): Display the current date with call_later.
* Watch a file descriptor for read events::
* Set signal handlers for SIGINT and SIGTERM::
Futures
* Future Functions::
* Future Object::
Transports and Protocols
* Transports::
* Protocols::
* Examples: Examples<15>.
Transports
* Transports Hierarchy::
* Base Transport::
* Read-only Transports::
* Write-only Transports::
* Datagram Transports::
* Subprocess Transports::
Protocols
* Base Protocols::
* Base Protocol::
* Streaming Protocols::
* Buffered Streaming Protocols::
* Datagram Protocols::
* Subprocess Protocols::
Examples
* TCP Echo Server::
* TCP Echo Client::
* UDP Echo Server::
* UDP Echo Client::
* Connecting Existing Sockets::
* loop.subprocess_exec() and SubprocessProtocol: loop subprocess_exec and SubprocessProtocol.
Policies
* Getting and Setting the Policy::
* Policy Objects::
* Process Watchers::
* Custom Policies::
Platform Support
* All Platforms::
* Windows::
* macOS::
Windows
* Subprocess Support on Windows::
High-level API Index
* Tasks::
* Queues: Queues<2>.
* Subprocesses: Subprocesses<2>.
* Streams: Streams<2>.
* Synchronization::
* Exceptions: Exceptions<10>.
Low-level API Index
* Obtaining the Event Loop::
* Event Loop Methods: Event Loop Methods<2>.
* Transports: Transports<2>.
* Protocols: Protocols<2>.
* Event Loop Policies::
Developing with asyncio
* Debug Mode::
* Concurrency and Multithreading::
* Running Blocking Code::
* Logging: Logging<3>.
* Detect never-awaited coroutines::
* Detect never-retrieved exceptions::
socket — Low-level networking interface
* Socket families::
* Module contents::
* Socket Objects::
* Notes on socket timeouts::
* Example: Example<8>.
Module contents
* Exceptions: Exceptions<11>.
* Constants: Constants<8>.
* Functions: Functions<5>.
Functions
* Creating sockets::
* Other functions: Other functions<2>.
Notes on socket timeouts
* Timeouts and the connect method::
* Timeouts and the accept method::
ssl — TLS/SSL wrapper for socket objects
* Functions, Constants, and Exceptions: Functions Constants and Exceptions.
* SSL Sockets::
* SSL Contexts::
* Certificates::
* Examples: Examples<16>.
* Notes on non-blocking sockets::
* Memory BIO Support: Memory BIO Support<2>.
* SSL session::
* Security considerations::
* TLS 1.3: TLS 1 3.
* LibreSSL support::
Functions, Constants, and Exceptions
* Socket creation::
* Context creation::
* Exceptions: Exceptions<12>.
* Random generation::
* Certificate handling::
* Constants: Constants<9>.
Certificates
* Certificate chains::
* CA certificates::
* Combined key and certificate::
* Self-signed certificates::
Examples
* Testing for SSL support::
* Client-side operation::
* Server-side operation::
Security considerations
* Best defaults::
* Manual settings::
* Multi-processing::
Manual settings
* Verifying certificates::
* Protocol versions::
* Cipher selection::
select — Waiting for I/O completion
* /dev/poll Polling Objects::
* Edge and Level Trigger Polling (epoll) Objects: Edge and Level Trigger Polling epoll Objects.
* Polling Objects::
* Kqueue Objects::
* Kevent Objects::
selectors — High-level I/O multiplexing
* Introduction: Introduction<9>.
* Classes: Classes<3>.
* Examples: Examples<17>.
asyncore — Asynchronous socket handler
* asyncore Example basic HTTP client::
* asyncore Example basic echo server::
asynchat — Asynchronous socket command/response handler
* asynchat Example::
signal — Set handlers for asynchronous events
* General rules::
* Module contents: Module contents<2>.
* Example: Example<9>.
* Note on SIGPIPE::
General rules
* Execution of Python signal handlers::
* Signals and threads::
mmap — Memory-mapped file support
* MADV_* Constants::
Internet Data Handling
* email — An email and MIME handling package::
* json — JSON encoder and decoder::
* mailcap — Mailcap file handling::
* mailbox — Manipulate mailboxes in various formats::
* mimetypes — Map filenames to MIME types::
* base64 — Base16, Base32, Base64, Base85 Data Encodings: base64 — Base16 Base32 Base64 Base85 Data Encodings.
* binhex — Encode and decode binhex4 files::
* binascii — Convert between binary and ASCII::
* quopri — Encode and decode MIME quoted-printable data::
* uu — Encode and decode uuencode files::
email — An email and MIME handling package
* email.message; Representing an email message: email message Representing an email message.
* email.parser; Parsing email messages: email parser Parsing email messages.
* email.generator; Generating MIME documents: email generator Generating MIME documents.
* email.policy; Policy Objects: email policy Policy Objects.
* email.errors; Exception and Defect classes: email errors Exception and Defect classes.
* email.headerregistry; Custom Header Objects: email headerregistry Custom Header Objects.
* email.contentmanager; Managing MIME Content: email contentmanager Managing MIME Content.
* email; Examples: email Examples.
* email.message.Message; Representing an email message using the compat32 API: email message Message Representing an email message using the compat32 API.
* email.mime; Creating email and MIME objects from scratch: email mime Creating email and MIME objects from scratch.
* email.header; Internationalized headers: email header Internationalized headers.
* email.charset; Representing character sets: email charset Representing character sets.
* email.encoders; Encoders: email encoders Encoders.
* email.utils; Miscellaneous utilities: email utils Miscellaneous utilities.
* email.iterators; Iterators: email iterators Iterators.
email.parser: Parsing email messages
* FeedParser API::
* Parser API::
* Additional notes::
email.contentmanager: Managing MIME Content
* Content Manager Instances::
json — JSON encoder and decoder
* Basic Usage::
* Encoders and Decoders::
* Exceptions: Exceptions<13>.
* Standard Compliance and Interoperability::
* Command Line Interface: Command Line Interface<2>.
Standard Compliance and Interoperability
* Character Encodings::
* Infinite and NaN Number Values::
* Repeated Names Within an Object::
* Top-level Non-Object, Non-Array Values: Top-level Non-Object Non-Array Values.
* Implementation Limitations::
Command Line Interface
* Command line options: Command line options<2>.
mailbox — Manipulate mailboxes in various formats
* Mailbox objects::
* Message objects::
* Exceptions: Exceptions<14>.
* Examples: Examples<18>.
Mailbox objects
* Maildir::
* mbox::
* MH::
* Babyl::
* MMDF::
Message objects
* MaildirMessage::
* mboxMessage::
* MHMessage::
* BabylMessage::
* MMDFMessage::
mimetypes — Map filenames to MIME types
* MimeTypes Objects::
binhex — Encode and decode binhex4 files
* Notes: Notes<2>.
Structured Markup Processing Tools
* html — HyperText Markup Language support::
* html.parser — Simple HTML and XHTML parser: html parser — Simple HTML and XHTML parser.
* html.entities — Definitions of HTML general entities: html entities — Definitions of HTML general entities.
* XML Processing Modules::
* xml.etree.ElementTree — The ElementTree XML API: xml etree ElementTree — The ElementTree XML API.
* xml.dom — The Document Object Model API: xml dom — The Document Object Model API.
* xml.dom.minidom — Minimal DOM implementation: xml dom minidom — Minimal DOM implementation.
* xml.dom.pulldom — Support for building partial DOM trees: xml dom pulldom — Support for building partial DOM trees.
* xml.sax — Support for SAX2 parsers: xml sax — Support for SAX2 parsers.
* xml.sax.handler — Base classes for SAX handlers: xml sax handler — Base classes for SAX handlers.
* xml.sax.saxutils — SAX Utilities: xml sax saxutils — SAX Utilities.
* xml.sax.xmlreader — Interface for XML parsers: xml sax xmlreader — Interface for XML parsers.
* xml.parsers.expat — Fast XML parsing using Expat: xml parsers expat — Fast XML parsing using Expat.
html.parser — Simple HTML and XHTML parser
* Example HTML Parser Application::
* HTMLParser Methods::
* Examples: Examples<19>.
XML Processing Modules
* XML vulnerabilities::
* The defusedxml Package::
xml.etree.ElementTree — The ElementTree XML API
* Tutorial::
* XPath support::
* Reference: Reference<2>.
* XInclude support::
* Reference: Reference<3>.
Tutorial
* XML tree and elements::
* Parsing XML::
* Pull API for non-blocking parsing::
* Finding interesting elements::
* Modifying an XML File::
* Building XML documents::
* Parsing XML with Namespaces::
* Additional resources::
XPath support
* Example: Example<10>.
* Supported XPath syntax::
Reference
* Functions: Functions<6>.
XInclude support
* Example: Example<11>.
Reference
* Functions: Functions<7>.
* Element Objects::
* ElementTree Objects::
* QName Objects::
* TreeBuilder Objects::
* XMLParser Objects::
* XMLPullParser Objects::
* Exceptions: Exceptions<15>.
xml.dom — The Document Object Model API
* Module Contents: Module Contents<4>.
* Objects in the DOM::
* Conformance::
Objects in the DOM
* DOMImplementation Objects::
* Node Objects::
* NodeList Objects::
* DocumentType Objects::
* Document Objects::
* Element Objects: Element Objects<2>.
* Attr Objects::
* NamedNodeMap Objects::
* Comment Objects::
* Text and CDATASection Objects::
* ProcessingInstruction Objects::
* Exceptions: Exceptions<16>.
Conformance
* Type Mapping::
* Accessor Methods::
xml.dom.minidom — Minimal DOM implementation
* DOM Objects::
* DOM Example::
* minidom and the DOM standard::
xml.dom.pulldom — Support for building partial DOM trees
* DOMEventStream Objects::
xml.sax — Support for SAX2 parsers
* SAXException Objects::
xml.sax.handler — Base classes for SAX handlers
* ContentHandler Objects::
* DTDHandler Objects::
* EntityResolver Objects::
* ErrorHandler Objects::
xml.sax.xmlreader — Interface for XML parsers
* XMLReader Objects::
* IncrementalParser Objects::
* Locator Objects::
* InputSource Objects::
* The Attributes Interface::
* The AttributesNS Interface::
xml.parsers.expat — Fast XML parsing using Expat
* XMLParser Objects: XMLParser Objects<2>.
* ExpatError Exceptions::
* Example: Example<12>.
* Content Model Descriptions::
* Expat error constants::
Internet Protocols and Support
* webbrowser — Convenient Web-browser controller::
* cgi — Common Gateway Interface support::
* cgitb — Traceback manager for CGI scripts::
* wsgiref — WSGI Utilities and Reference Implementation::
* urllib — URL handling modules::
* urllib.request — Extensible library for opening URLs: urllib request — Extensible library for opening URLs.
* urllib.response — Response classes used by urllib: urllib response — Response classes used by urllib.
* urllib.parse — Parse URLs into components: urllib parse — Parse URLs into components.
* urllib.error — Exception classes raised by urllib.request: urllib error — Exception classes raised by urllib request.
* urllib.robotparser — Parser for robots.txt: urllib robotparser — Parser for robots txt.
* http — HTTP modules::
* http.client — HTTP protocol client: http client — HTTP protocol client.
* ftplib — FTP protocol client::
* poplib — POP3 protocol client::
* imaplib — IMAP4 protocol client::
* nntplib — NNTP protocol client::
* smtplib — SMTP protocol client::
* smtpd — SMTP Server::
* telnetlib — Telnet client::
* uuid — UUID objects according to RFC 4122::
* socketserver — A framework for network servers::
* http.server — HTTP servers: http server — HTTP servers.
* http.cookies — HTTP state management: http cookies — HTTP state management.
* http.cookiejar — Cookie handling for HTTP clients: http cookiejar — Cookie handling for HTTP clients.
* xmlrpc — XMLRPC server and client modules::
* xmlrpc.client — XML-RPC client access: xmlrpc client — XML-RPC client access.
* xmlrpc.server — Basic XML-RPC servers: xmlrpc server — Basic XML-RPC servers.
* ipaddress — IPv4/IPv6 manipulation library::
webbrowser — Convenient Web-browser controller
* Browser Controller Objects::
cgi — Common Gateway Interface support
* Introduction: Introduction<10>.
* Using the cgi module::
* Higher Level Interface::
* Functions: Functions<8>.
* Caring about security::
* Installing your CGI script on a Unix system::
* Testing your CGI script::
* Debugging CGI scripts::
* Common problems and solutions::
wsgiref — WSGI Utilities and Reference Implementation
* wsgiref.util – WSGI environment utilities: wsgiref util – WSGI environment utilities.
* wsgiref.headers – WSGI response header tools: wsgiref headers – WSGI response header tools.
* wsgiref.simple_server – a simple WSGI HTTP server: wsgiref simple_server – a simple WSGI HTTP server.
* wsgiref.validate — WSGI conformance checker: wsgiref validate — WSGI conformance checker.
* wsgiref.handlers – server/gateway base classes: wsgiref handlers – server/gateway base classes.
* Examples: Examples<20>.
urllib.request — Extensible library for opening URLs
* Request Objects::
* OpenerDirector Objects::
* BaseHandler Objects::
* HTTPRedirectHandler Objects::
* HTTPCookieProcessor Objects::
* ProxyHandler Objects::
* HTTPPasswordMgr Objects::
* HTTPPasswordMgrWithPriorAuth Objects::
* AbstractBasicAuthHandler Objects::
* HTTPBasicAuthHandler Objects::
* ProxyBasicAuthHandler Objects::
* AbstractDigestAuthHandler Objects::
* HTTPDigestAuthHandler Objects::
* ProxyDigestAuthHandler Objects::
* HTTPHandler Objects::
* HTTPSHandler Objects::
* FileHandler Objects::
* DataHandler Objects::
* FTPHandler Objects::
* CacheFTPHandler Objects::
* UnknownHandler Objects::
* HTTPErrorProcessor Objects::
* Examples: Examples<21>.
* Legacy interface::
* urllib.request Restrictions: urllib request Restrictions.
urllib.parse — Parse URLs into components
* URL Parsing::
* Parsing ASCII Encoded Bytes::
* Structured Parse Results::
* URL Quoting::
http — HTTP modules
* HTTP status codes::
http.client — HTTP protocol client
* HTTPConnection Objects::
* HTTPResponse Objects::
* Examples: Examples<22>.
* HTTPMessage Objects::
ftplib — FTP protocol client
* FTP Objects::
* FTP_TLS Objects::
poplib — POP3 protocol client
* POP3 Objects::
* POP3 Example::
imaplib — IMAP4 protocol client
* IMAP4 Objects::
* IMAP4 Example::
nntplib — NNTP protocol client
* NNTP Objects::
* Utility functions: Utility functions<2>.
NNTP Objects
* Attributes::
* Methods: Methods<3>.
smtplib — SMTP protocol client
* SMTP Objects::
* SMTP Example::
smtpd — SMTP Server
* SMTPServer Objects::
* DebuggingServer Objects::
* PureProxy Objects::
* MailmanProxy Objects::
* SMTPChannel Objects::
telnetlib — Telnet client
* Telnet Objects::
* Telnet Example::
uuid — UUID objects according to RFC 4122
* Example: Example<13>.
socketserver — A framework for network servers
* Server Creation Notes::
* Server Objects: Server Objects<2>.
* Request Handler Objects::
* Examples: Examples<23>.
Examples
* socketserver.TCPServer Example: socketserver TCPServer Example.
* socketserver.UDPServer Example: socketserver UDPServer Example.
* Asynchronous Mixins::
http.cookies — HTTP state management
* Cookie Objects::
* Morsel Objects::
* Example: Example<14>.
http.cookiejar — Cookie handling for HTTP clients
* CookieJar and FileCookieJar Objects::
* FileCookieJar subclasses and co-operation with web browsers::
* CookiePolicy Objects::
* DefaultCookiePolicy Objects::
* Cookie Objects: Cookie Objects<2>.
* Examples: Examples<24>.
xmlrpc.client — XML-RPC client access
* ServerProxy Objects::
* DateTime Objects::
* Binary Objects::
* Fault Objects::
* ProtocolError Objects::
* MultiCall Objects::
* Convenience Functions::
* Example of Client Usage::
* Example of Client and Server Usage::
xmlrpc.server — Basic XML-RPC servers
* SimpleXMLRPCServer Objects::
* CGIXMLRPCRequestHandler::
* Documenting XMLRPC server::
* DocXMLRPCServer Objects::
* DocCGIXMLRPCRequestHandler::
SimpleXMLRPCServer Objects
* SimpleXMLRPCServer Example::
ipaddress — IPv4/IPv6 manipulation library
* Convenience factory functions::
* IP Addresses::
* IP Network definitions::
* Interface objects::
* Other Module Level Functions::
* Custom Exceptions::
IP Addresses
* Address objects::
* Conversion to Strings and Integers::
* Operators: Operators<3>.
Operators
* Comparison operators::
* Arithmetic operators::
IP Network definitions
* Prefix, net mask and host mask: Prefix net mask and host mask.
* Network objects::
* Operators: Operators<4>.
Operators
* Logical operators::
* Iteration: Iteration<2>.
* Networks as containers of addresses::
Interface objects
* Operators: Operators<5>.
Operators
* Logical operators: Logical operators<2>.
Multimedia Services
* audioop — Manipulate raw audio data::
* aifc — Read and write AIFF and AIFC files::
* sunau — Read and write Sun AU files::
* wave — Read and write WAV files::
* chunk — Read IFF chunked data::
* colorsys — Conversions between color systems::
* imghdr — Determine the type of an image::
* sndhdr — Determine type of sound file::
* ossaudiodev — Access to OSS-compatible audio devices::
sunau — Read and write Sun AU files
* AU_read Objects::
* AU_write Objects::
wave — Read and write WAV files
* Wave_read Objects::
* Wave_write Objects::
ossaudiodev — Access to OSS-compatible audio devices
* Audio Device Objects::
* Mixer Device Objects::
Internationalization
* gettext — Multilingual internationalization services::
* locale — Internationalization services::
gettext — Multilingual internationalization services
* GNU gettext API::
* Class-based API::
* Internationalizing your programs and modules::
* Acknowledgements: Acknowledgements<9>.
Class-based API
* The NullTranslations class::
* The GNUTranslations class::
* Solaris message catalog support::
* The Catalog constructor::
Internationalizing your programs and modules
* Localizing your module::
* Localizing your application::
* Changing languages on the fly::
* Deferred translations::
locale — Internationalization services
* Background, details, hints, tips and caveats: Background details hints tips and caveats.
* For extension writers and programs that embed Python::
* Access to message catalogs::
Program Frameworks
* turtle — Turtle graphics::
* cmd — Support for line-oriented command interpreters::
* shlex — Simple lexical analysis::
turtle — Turtle graphics
* Introduction: Introduction<11>.
* Overview of available Turtle and Screen methods::
* Methods of RawTurtle/Turtle and corresponding functions::
* Methods of TurtleScreen/Screen and corresponding functions::
* Public classes::
* Help and configuration::
* turtledemo — Demo scripts::
* Changes since Python 2.6: Changes since Python 2 6.
* Changes since Python 3.0: Changes since Python 3 0.
Overview of available Turtle and Screen methods
* Turtle methods::
* Methods of TurtleScreen/Screen::
Methods of RawTurtle/Turtle and corresponding functions
* Turtle motion::
* Tell Turtle’s state::
* Settings for measurement::
* Pen control::
* Turtle state::
* Using events::
* Special Turtle methods::
* Compound shapes::
Pen control
* Drawing state::
* Color control::
* Filling::
* More drawing control::
Turtle state
* Visibility::
* Appearance::
Methods of TurtleScreen/Screen and corresponding functions
* Window control::
* Animation control::
* Using screen events::
* Input methods::
* Settings and special methods::
* Methods specific to Screen, not inherited from TurtleScreen: Methods specific to Screen not inherited from TurtleScreen.
Help and configuration
* How to use help::
* Translation of docstrings into different languages::
* How to configure Screen and Turtles::
cmd — Support for line-oriented command interpreters
* Cmd Objects::
* Cmd Example::
shlex — Simple lexical analysis
* shlex Objects::
* Parsing Rules::
* Improved Compatibility with Shells::
Graphical User Interfaces with Tk
* tkinter — Python interface to Tcl/Tk::
* tkinter.ttk — Tk themed widgets: tkinter ttk — Tk themed widgets.
* tkinter.tix — Extension widgets for Tk: tkinter tix — Extension widgets for Tk.
* tkinter.scrolledtext — Scrolled Text Widget: tkinter scrolledtext — Scrolled Text Widget.
* IDLE: IDLE<3>.
* Other Graphical User Interface Packages::
tkinter — Python interface to Tcl/Tk
* Tkinter Modules::
* Tkinter Life Preserver::
* A (Very) Quick Look at Tcl/Tk: A Very Quick Look at Tcl/Tk.
* Mapping Basic Tk into Tkinter::
* How Tk and Tkinter are Related::
* Handy Reference::
* File Handlers::
Tkinter Life Preserver
* How To Use This Section::
* A Simple Hello World Program::
Handy Reference
* Setting Options::
* The Packer::
* Packer Options::
* Coupling Widget Variables::
* The Window Manager::
* Tk Option Data Types::
* Bindings and Events::
* The index Parameter::
* Images::
tkinter.ttk — Tk themed widgets
* Using Ttk::
* Ttk Widgets::
* Widget::
* Combobox::
* Spinbox::
* Notebook::
* Progressbar::
* Separator::
* Sizegrip::
* Treeview::
* Ttk Styling::
Widget
* Standard Options::
* Scrollable Widget Options::
* Label Options::
* Compatibility Options::
* Widget States::
* ttk.Widget: ttk Widget.
Combobox
* Options::
* Virtual events::
* ttk.Combobox: ttk Combobox.
Spinbox
* Options: Options<2>.
* Virtual events: Virtual events<2>.
* ttk.Spinbox: ttk Spinbox.
Notebook
* Options: Options<3>.
* Tab Options::
* Tab Identifiers::
* Virtual Events::
* ttk.Notebook: ttk Notebook.
Progressbar
* Options: Options<4>.
* ttk.Progressbar: ttk Progressbar.
Separator
* Options: Options<5>.
Sizegrip
* Platform-specific notes::
* Bugs::
Treeview
* Options: Options<6>.
* Item Options::
* Tag Options::
* Column Identifiers::
* Virtual Events: Virtual Events<2>.
* ttk.Treeview: ttk Treeview.
Ttk Styling
* Layouts::
tkinter.tix — Extension widgets for Tk
* Using Tix::
* Tix Widgets::
* Tix Commands::
Tix Widgets
* Basic Widgets::
* File Selectors::
* Hierarchical ListBox::
* Tabular ListBox::
* Manager Widgets::
* Image Types::
* Miscellaneous Widgets::
* Form Geometry Manager::
IDLE
* Menus::
* Editing and navigation::
* Startup and code execution::
* Help and preferences::
Menus
* File menu (Shell and Editor): File menu Shell and Editor.
* Edit menu (Shell and Editor): Edit menu Shell and Editor.
* Format menu (Editor window only): Format menu Editor window only.
* Run menu (Editor window only): Run menu Editor window only.
* Shell menu (Shell window only): Shell menu Shell window only.
* Debug menu (Shell window only): Debug menu Shell window only.
* Options menu (Shell and Editor): Options menu Shell and Editor.
* Window menu (Shell and Editor): Window menu Shell and Editor.
* Help menu (Shell and Editor): Help menu Shell and Editor.
* Context Menus::
Editing and navigation
* Editor windows::
* Key bindings::
* Automatic indentation::
* Completions::
* Calltips::
* Code Context::
* Python Shell window::
* Text colors::
Startup and code execution
* Command line usage::
* Startup failure::
* Running user code::
* User output in Shell::
* Developing tkinter applications::
* Running without a subprocess::
Help and preferences
* Help sources::
* Setting preferences::
* IDLE on macOS::
* Extensions::
Development Tools
* typing — Support for type hints::
* pydoc — Documentation generator and online help system::
* doctest — Test interactive Python examples::
* unittest — Unit testing framework::
* unittest.mock — mock object library: unittest mock — mock object library.
* unittest.mock — getting started: unittest mock — getting started.
* 2to3 - Automated Python 2 to 3 code translation::
* test — Regression tests package for Python::
* test.support — Utilities for the Python test suite: test support — Utilities for the Python test suite.
* test.support.script_helper — Utilities for the Python execution tests: test support script_helper — Utilities for the Python execution tests.
typing — Support for type hints
* Type aliases::
* NewType::
* Callable::
* Generics::
* User-defined generic types::
* The Any type::
* Nominal vs structural subtyping::
* Classes, functions, and decorators: Classes functions and decorators.
doctest — Test interactive Python examples
* Simple Usage; Checking Examples in Docstrings: Simple Usage Checking Examples in Docstrings.
* Simple Usage; Checking Examples in a Text File: Simple Usage Checking Examples in a Text File.
* How It Works::
* Basic API::
* Unittest API::
* Advanced API::
* Debugging::
* Soapbox::
How It Works
* Which Docstrings Are Examined?::
* How are Docstring Examples Recognized?::
* What’s the Execution Context?::
* What About Exceptions?::
* Option Flags::
* Directives::
* Warnings: Warnings<2>.
Advanced API
* DocTest Objects::
* Example Objects::
* DocTestFinder objects::
* DocTestParser objects::
* DocTestRunner objects::
* OutputChecker objects::
unittest — Unit testing framework
* Basic example::
* Command-Line Interface: Command-Line Interface<3>.
* Test Discovery::
* Organizing test code::
* Re-using old test code::
* Skipping tests and expected failures::
* Distinguishing test iterations using subtests::
* Classes and functions::
* Class and Module Fixtures::
* Signal Handling::
Command-Line Interface
* Command-line options: Command-line options<3>.
Classes and functions
* Test cases::
* Grouping tests::
* Loading and running tests::
Test cases
* Deprecated aliases::
Loading and running tests
* load_tests Protocol::
Class and Module Fixtures
* setUpClass and tearDownClass::
* setUpModule and tearDownModule::
unittest.mock — mock object library
* Quick Guide::
* The Mock Class::
* The patchers::
* MagicMock and magic method support::
* Helpers::
The Mock Class
* Calling::
* Deleting Attributes::
* Mock names and the name attribute::
* Attaching Mocks as Attributes::
The patchers
* patch::
* patch.object: patch object.
* patch.dict: patch dict.
* patch.multiple: patch multiple.
* patch methods; start and stop: patch methods start and stop.
* patch builtins::
* TEST_PREFIX::
* Nesting Patch Decorators::
* Where to patch::
* Patching Descriptors and Proxy Objects::
MagicMock and magic method support
* Mocking Magic Methods::
* Magic Mock::
Helpers
* sentinel::
* DEFAULT::
* call::
* create_autospec::
* ANY::
* FILTER_DIR::
* mock_open::
* Autospeccing::
* Sealing mocks::
unittest.mock — getting started
* Using Mock::
* Patch Decorators::
* Further Examples::
Using Mock
* Mock Patching Methods::
* Mock for Method Calls on an Object::
* Mocking Classes::
* Naming your mocks::
* Tracking all Calls::
* Setting Return Values and Attributes::
* Raising exceptions with mocks::
* Side effect functions and iterables::
* Mocking asynchronous iterators::
* Mocking asynchronous context manager::
* Creating a Mock from an Existing Object::
Further Examples
* Mocking chained calls::
* Partial mocking::
* Mocking a Generator Method::
* Applying the same patch to every test method::
* Mocking Unbound Methods::
* Checking multiple calls with mock::
* Coping with mutable arguments::
* Nesting Patches::
* Mocking a dictionary with MagicMock::
* Mock subclasses and their attributes::
* Mocking imports with patch.dict: Mocking imports with patch dict.
* Tracking order of calls and less verbose call assertions::
* More complex argument matching::
2to3 - Automated Python 2 to 3 code translation
* Using 2to3::
* Fixers::
* lib2to3 - 2to3’s library::
test — Regression tests package for Python
* Writing Unit Tests for the test package::
* Running tests using the command-line interface::
Debugging and Profiling
* Audit events table::
* bdb — Debugger framework::
* faulthandler — Dump the Python traceback::
* pdb — The Python Debugger::
* The Python Profilers::
* timeit — Measure execution time of small code snippets::
* trace — Trace or track Python statement execution::
* tracemalloc — Trace memory allocations::
faulthandler — Dump the Python traceback
* Dumping the traceback::
* Fault handler state::
* Dumping the tracebacks after a timeout::
* Dumping the traceback on a user signal::
* Issue with file descriptors::
* Example: Example<15>.
pdb — The Python Debugger
* Debugger Commands::
The Python Profilers
* Introduction to the profilers::
* Instant User’s Manual::
* profile and cProfile Module Reference::
* The Stats Class::
* What Is Deterministic Profiling?::
* Limitations::
* Calibration::
* Using a custom timer::
timeit — Measure execution time of small code snippets
* Basic Examples: Basic Examples<2>.
* Python Interface::
* Command-Line Interface: Command-Line Interface<4>.
* Examples: Examples<25>.
trace — Trace or track Python statement execution
* Command-Line Usage::
* Programmatic Interface::
Command-Line Usage
* Main options::
* Modifiers::
* Filters::
tracemalloc — Trace memory allocations
* Examples: Examples<26>.
* API::
Examples
* Display the top 10::
* Compute differences::
* Get the traceback of a memory block::
* Pretty top::
API
* Functions: Functions<9>.
* DomainFilter::
* Filter::
* Frame::
* Snapshot::
* Statistic::
* StatisticDiff::
* Trace::
* Traceback::
Software Packaging and Distribution
* distutils — Building and installing Python modules::
* ensurepip — Bootstrapping the pip installer::
* venv — Creation of virtual environments::
* zipapp — Manage executable Python zip archives::
ensurepip — Bootstrapping the pip installer
* Command line interface::
* Module API::
venv — Creation of virtual environments
* Creating virtual environments::
* API: API<2>.
* An example of extending EnvBuilder::
zipapp — Manage executable Python zip archives
* Basic Example::
* Command-Line Interface: Command-Line Interface<5>.
* Python API::
* Examples: Examples<27>.
* Specifying the Interpreter::
* Creating Standalone Applications with zipapp::
* The Python Zip Application Archive Format::
Creating Standalone Applications with zipapp
* Making a Windows executable::
* Caveats::
Python Runtime Services
* sys — System-specific parameters and functions::
* sysconfig — Provide access to Python’s configuration information::
* builtins — Built-in objects::
* __main__ — Top-level script environment::
* warnings — Warning control::
* dataclasses — Data Classes::
* contextlib — Utilities for with-statement contexts::
* abc — Abstract Base Classes::
* atexit — Exit handlers::
* traceback — Print or retrieve a stack traceback::
* __future__ — Future statement definitions::
* gc — Garbage Collector interface::
* inspect — Inspect live objects::
* site — Site-specific configuration hook::
sysconfig — Provide access to Python’s configuration information
* Configuration variables::
* Installation paths::
* Other functions: Other functions<3>.
* Using sysconfig as a script::
warnings — Warning control
* Warning Categories::
* The Warnings Filter::
* Temporarily Suppressing Warnings::
* Testing Warnings::
* Updating Code For New Versions of Dependencies::
* Available Functions::
* Available Context Managers::
The Warnings Filter
* Describing Warning Filters::
* Default Warning Filter::
* Overriding the default filter::
dataclasses — Data Classes
* Module-level decorators, classes, and functions: Module-level decorators classes and functions.
* Post-init processing::
* Class variables::
* Init-only variables::
* Frozen instances::
* Inheritance: Inheritance<2>.
* Default factory functions::
* Mutable default values::
* Exceptions: Exceptions<17>.
contextlib — Utilities for with-statement contexts
* Utilities::
* Examples and Recipes: Examples and Recipes<2>.
* Single use, reusable and reentrant context managers: Single use reusable and reentrant context managers.
Examples and Recipes
* Supporting a variable number of context managers::
* Catching exceptions from __enter__ methods::
* Cleaning up in an __enter__ implementation::
* Replacing any use of try-finally and flag variables::
* Using a context manager as a function decorator::
Single use, reusable and reentrant context managers
* Reentrant context managers::
* Reusable context managers::
atexit — Exit handlers
* atexit Example::
traceback — Print or retrieve a stack traceback
* TracebackException Objects::
* StackSummary Objects::
* FrameSummary Objects::
* Traceback Examples::
inspect — Inspect live objects
* Types and members::
* Retrieving source code::
* Introspecting callables with the Signature object::
* Classes and functions: Classes and functions<2>.
* The interpreter stack::
* Fetching attributes statically::
* Current State of Generators and Coroutines::
* Code Objects Bit Flags::
* Command Line Interface: Command Line Interface<3>.
site — Site-specific configuration hook
* Readline configuration::
* Module contents: Module contents<3>.
* Command Line Interface: Command Line Interface<4>.
Custom Python Interpreters
* code — Interpreter base classes::
* codeop — Compile Python code::
code — Interpreter base classes
* Interactive Interpreter Objects::
* Interactive Console Objects::
Importing Modules
* zipimport — Import modules from Zip archives::
* pkgutil — Package extension utility::
* modulefinder — Find modules used by a script::
* runpy — Locating and executing Python modules::
* importlib — The implementation of import::
* Using importlib.metadata: Using importlib metadata.
zipimport — Import modules from Zip archives
* zipimporter Objects::
* Examples: Examples<28>.
modulefinder — Find modules used by a script
* Example usage of ModuleFinder::
importlib — The implementation of import
* Introduction: Introduction<12>.
* Functions: Functions<10>.
* importlib.abc – Abstract base classes related to import: importlib abc – Abstract base classes related to import.
* importlib.resources – Resources: importlib resources – Resources.
* importlib.machinery – Importers and path hooks: importlib machinery – Importers and path hooks.
* importlib.util – Utility code for importers: importlib util – Utility code for importers.
* Examples: Examples<29>.
Examples
* Importing programmatically::
* Checking if a module can be imported::
* Importing a source file directly::
* Setting up an importer::
* Approximating importlib.import_module(): Approximating importlib import_module.
Using importlib.metadata
* Overview: Overview<2>.
* Functional API: Functional API<2>.
* Distributions::
* Extending the search algorithm::
Functional API
* Entry points::
* Distribution metadata::
* Distribution versions::
* Distribution files::
* Distribution requirements::
Python Language Services
* parser — Access Python parse trees::
* ast — Abstract Syntax Trees::
* symtable — Access to the compiler’s symbol tables::
* symbol — Constants used with Python parse trees::
* token — Constants used with Python parse trees::
* keyword — Testing for Python keywords::
* tokenize — Tokenizer for Python source::
* tabnanny — Detection of ambiguous indentation::
* pyclbr — Python module browser support::
* py_compile — Compile Python source files::
* compileall — Byte-compile Python libraries::
* dis — Disassembler for Python bytecode::
* pickletools — Tools for pickle developers::
parser — Access Python parse trees
* Creating ST Objects::
* Converting ST Objects::
* Queries on ST Objects::
* Exceptions and Error Handling::
* ST Objects::
* Example; Emulation of compile(): Example Emulation of compile.
ast — Abstract Syntax Trees
* Node classes::
* Abstract Grammar::
* ast Helpers::
symtable — Access to the compiler’s symbol tables
* Generating Symbol Tables::
* Examining Symbol Tables::
tokenize — Tokenizer for Python source
* Tokenizing Input::
* Command-Line Usage: Command-Line Usage<2>.
* Examples: Examples<30>.
pyclbr — Python module browser support
* Function Objects::
* Class Objects: Class Objects<2>.
compileall — Byte-compile Python libraries
* Command-line use::
* Public functions::
dis — Disassembler for Python bytecode
* Bytecode analysis::
* Analysis functions::
* Python Bytecode Instructions::
* Opcode collections::
pickletools — Tools for pickle developers
* Command line usage: Command line usage<2>.
* Programmatic Interface: Programmatic Interface<2>.
Command line usage
* Command line options: Command line options<3>.
Miscellaneous Services
* formatter — Generic output formatting::
formatter — Generic output formatting
* The Formatter Interface::
* Formatter Implementations::
* The Writer Interface::
* Writer Implementations::
MS Windows Specific Services
* msilib — Read and write Microsoft Installer files::
* msvcrt — Useful routines from the MS VC++ runtime::
* winreg — Windows registry access::
* winsound — Sound-playing interface for Windows::
msilib — Read and write Microsoft Installer files
* Database Objects::
* View Objects::
* Summary Information Objects::
* Record Objects::
* Errors::
* CAB Objects::
* Directory Objects::
* Features: Features<3>.
* GUI classes::
* Precomputed tables::
msvcrt — Useful routines from the MS VC++ runtime
* File Operations::
* Console I/O::
* Other Functions::
winreg — Windows registry access
* Functions: Functions<11>.
* Constants: Constants<10>.
* Registry Handle Objects::
Constants
* HKEY_* Constants::
* Access Rights::
* Value Types::
Access Rights
* 64-bit Specific::
Unix Specific Services
* posix — The most common POSIX system calls::
* pwd — The password database::
* spwd — The shadow password database::
* grp — The group database::
* crypt — Function to check Unix passwords::
* termios — POSIX style tty control::
* tty — Terminal control functions::
* pty — Pseudo-terminal utilities::
* fcntl — The fcntl and ioctl system calls::
* pipes — Interface to shell pipelines::
* resource — Resource usage information::
* nis — Interface to Sun’s NIS (Yellow Pages): nis — Interface to Sun’s NIS Yellow Pages.
* syslog — Unix syslog library routines::
posix — The most common POSIX system calls
* Large File Support::
* Notable Module Contents::
crypt — Function to check Unix passwords
* Hashing Methods::
* Module Attributes::
* Module Functions: Module Functions<2>.
* Examples: Examples<31>.
termios — POSIX style tty control
* Example: Example<16>.
pty — Pseudo-terminal utilities
* Example: Example<17>.
pipes — Interface to shell pipelines
* Template Objects::
resource — Resource usage information
* Resource Limits::
* Resource Usage::
syslog — Unix syslog library routines
* Examples: Examples<32>.
Examples
* Simple example::
Superseded Modules
* optparse — Parser for command line options::
* imp — Access the import internals::
optparse — Parser for command line options
* Background::
* Tutorial: Tutorial<2>.
* Reference Guide::
* Option Callbacks::
* Extending optparse::
Background
* Terminology::
* What are options for?::
* What are positional arguments for?::
Tutorial
* Understanding option actions::
* The store action::
* Handling boolean (flag) options: Handling boolean flag options.
* Other actions::
* Default values::
* Generating help::
* Printing a version string::
* How optparse handles errors::
* Putting it all together::
Generating help
* Grouping Options::
Reference Guide
* Creating the parser::
* Populating the parser::
* Defining options::
* Option attributes::
* Standard option actions::
* Standard option types::
* Parsing arguments: Parsing arguments<2>.
* Querying and manipulating your option parser::
* Conflicts between options::
* Cleanup: Cleanup<2>.
* Other methods::
Option Callbacks
* Defining a callback option::
* How callbacks are called::
* Raising errors in a callback::
* Callback example 1; trivial callback: Callback example 1 trivial callback.
* Callback example 2; check option order: Callback example 2 check option order.
* Callback example 3; check option order (generalized): Callback example 3 check option order generalized.
* Callback example 4; check arbitrary condition: Callback example 4 check arbitrary condition.
* Callback example 5; fixed arguments: Callback example 5 fixed arguments.
* Callback example 6; variable arguments: Callback example 6 variable arguments.
Extending optparse
* Adding new types::
* Adding new actions::
imp — Access the import internals
* Examples: Examples<33>.
Undocumented Modules
* Platform specific modules::
Extending and Embedding the Python Interpreter
* Recommended third party tools::
* Creating extensions without third party tools::
* Embedding the CPython runtime in a larger application::
Creating extensions without third party tools
* Extending Python with C or C++::
* Defining Extension Types; Tutorial: Defining Extension Types Tutorial.
* Defining Extension Types; Assorted Topics: Defining Extension Types Assorted Topics.
* Building C and C++ Extensions::
* Building C and C++ Extensions on Windows::
Extending Python with C or C++
* A Simple Example::
* Intermezzo; Errors and Exceptions: Intermezzo Errors and Exceptions.
* Back to the Example::
* The Module’s Method Table and Initialization Function::
* Compilation and Linkage::
* Calling Python Functions from C::
* Extracting Parameters in Extension Functions::
* Keyword Parameters for Extension Functions::
* Building Arbitrary Values::
* Reference Counts::
* Writing Extensions in C++::
* Providing a C API for an Extension Module::
Reference Counts
* Reference Counting in Python::
* Ownership Rules::
* Thin Ice::
* NULL Pointers::
Defining Extension Types: Tutorial
* The Basics::
* Adding data and methods to the Basic example::
* Providing finer control over data attributes::
* Supporting cyclic garbage collection::
* Subclassing other types::
Defining Extension Types: Assorted Topics
* Finalization and De-allocation::
* Object Presentation::
* Attribute Management::
* Object Comparison::
* Abstract Protocol Support::
* Weak Reference Support::
* More Suggestions::
Attribute Management
* Generic Attribute Management::
* Type-specific Attribute Management::
Building C and C++ Extensions
* Building C and C++ Extensions with distutils::
* Distributing your extension modules::
Building C and C++ Extensions on Windows
* A Cookbook Approach::
* Differences Between Unix and Windows::
* Using DLLs in Practice::
Embedding the CPython runtime in a larger application
* Embedding Python in Another Application::
Embedding Python in Another Application
* Very High Level Embedding::
* Beyond Very High Level Embedding; An overview: Beyond Very High Level Embedding An overview.
* Pure Embedding::
* Extending Embedded Python::
* Embedding Python in C++::
* Compiling and Linking under Unix-like systems::
Python/C API Reference Manual
* Introduction: Introduction<13>.
* Stable Application Binary Interface::
* The Very High Level Layer::
* Reference Counting::
* Exception Handling::
* Utilities: Utilities<2>.
* Abstract Objects Layer::
* Concrete Objects Layer::
* Initialization, Finalization, and Threads: Initialization Finalization and Threads.
* Python Initialization Configuration::
* Memory Management::
* Object Implementation Support::
* API and ABI Versioning::
Introduction
* Coding standards::
* Include Files::
* Useful macros::
* Objects, Types and Reference Counts: Objects Types and Reference Counts.
* Exceptions: Exceptions<18>.
* Embedding Python: Embedding Python<2>.
* Debugging Builds::
Objects, Types and Reference Counts
* Reference Counts: Reference Counts<2>.
* Types::
Reference Counts
* Reference Count Details::
Exception Handling
* Printing and clearing::
* Raising exceptions::
* Issuing warnings::
* Querying the error indicator::
* Signal Handling: Signal Handling<2>.
* Exception Classes::
* Exception Objects::
* Unicode Exception Objects::
* Recursion Control::
* Standard Exceptions::
* Standard Warning Categories::
Utilities
* Operating System Utilities::
* System Functions::
* Process Control::
* Importing Modules: Importing Modules<2>.
* Data marshalling support::
* Parsing arguments and building values::
* String conversion and formatting::
* Reflection::
* Codec registry and support functions::
Parsing arguments and building values
* Parsing arguments: Parsing arguments<3>.
* Building values::
Parsing arguments
* Strings and buffers::
* Numbers: Numbers<2>.
* Other objects::
* API Functions::
Codec registry and support functions
* Codec lookup API::
* Registry API for Unicode encoding error handlers::
Abstract Objects Layer
* Object Protocol::
* Number Protocol::
* Sequence Protocol::
* Mapping Protocol::
* Iterator Protocol::
* Buffer Protocol::
* Old Buffer Protocol::
Buffer Protocol
* Buffer structure::
* Buffer request types::
* Complex arrays::
* Buffer-related functions::
Buffer request types
* request-independent fields::
* readonly, format: readonly format.
* shape, strides, suboffsets: shape strides suboffsets.
* contiguity requests::
* compound requests::
Complex arrays
* NumPy-style; shape and strides: NumPy-style shape and strides.
* PIL-style; shape, strides and suboffsets: PIL-style shape strides and suboffsets.
Concrete Objects Layer
* Fundamental Objects::
* Numeric Objects::
* Sequence Objects::
* Container Objects::
* Function Objects: Function Objects<2>.
* Other Objects::
Fundamental Objects
* Type Objects: Type Objects<2>.
* The None Object::
Type Objects
* Creating Heap-Allocated Types::
Numeric Objects
* Integer Objects::
* Boolean Objects::
* Floating Point Objects::
* Complex Number Objects::
Complex Number Objects
* Complex Numbers as C Structures::
* Complex Numbers as Python Objects::
Sequence Objects
* Bytes Objects: Bytes Objects<2>.
* Byte Array Objects::
* Unicode Objects and Codecs::
* Tuple Objects::
* Struct Sequence Objects::
* List Objects::
Byte Array Objects
* Type check macros::
* Direct API functions::
* Macros::
Unicode Objects and Codecs
* Unicode Objects::
* Built-in Codecs::
* Methods and Slot Functions::
Unicode Objects
* Unicode Type::
* Unicode Character Properties::
* Creating and accessing Unicode strings::
* Deprecated Py_UNICODE APIs::
* Locale Encoding::
* File System Encoding::
* wchar_t Support::
Built-in Codecs
* Generic Codecs::
* UTF-8 Codecs::
* UTF-32 Codecs::
* UTF-16 Codecs::
* UTF-7 Codecs::
* Unicode-Escape Codecs::
* Raw-Unicode-Escape Codecs::
* Latin-1 Codecs::
* ASCII Codecs::
* Character Map Codecs::
* MBCS codecs for Windows::
* Methods & Slots::
Container Objects
* Dictionary Objects::
* Set Objects::
Function Objects
* Function Objects: Function Objects<3>.
* Instance Method Objects::
* Method Objects: Method Objects<2>.
* Cell Objects::
* Code Objects: Code Objects<2>.
Other Objects
* File Objects::
* Module Objects::
* Iterator Objects::
* Descriptor Objects::
* Slice Objects::
* Ellipsis Object::
* MemoryView objects::
* Weak Reference Objects: Weak Reference Objects<2>.
* Capsules: Capsules<2>.
* Generator Objects::
* Coroutine Objects: Coroutine Objects<2>.
* Context Variables Objects::
* DateTime Objects: DateTime Objects<2>.
Module Objects
* Initializing C modules::
* Module lookup::
Initializing C modules
* Single-phase initialization::
* Multi-phase initialization::
* Low-level module creation functions::
* Support functions::
Initialization, Finalization, and Threads
* Before Python Initialization::
* Global configuration variables::
* Initializing and finalizing the interpreter::
* Process-wide parameters::
* Thread State and the Global Interpreter Lock::
* Sub-interpreter support::
* Asynchronous Notifications::
* Profiling and Tracing::
* Advanced Debugger Support::
* Thread Local Storage Support::
Thread State and the Global Interpreter Lock
* Releasing the GIL from extension code::
* Non-Python created threads::
* Cautions about fork(): Cautions about fork.
* High-level API::
* Low-level API::
Sub-interpreter support
* Bugs and caveats::
Thread Local Storage Support
* Thread Specific Storage (TSS) API: Thread Specific Storage TSS API.
* Thread Local Storage (TLS) API: Thread Local Storage TLS API.
Thread Specific Storage (TSS) API
* Dynamic Allocation::
* Methods: Methods<4>.
Python Initialization Configuration
* PyWideStringList::
* PyStatus::
* PyPreConfig::
* Preinitialization with PyPreConfig::
* PyConfig::
* Initialization with PyConfig::
* Isolated Configuration::
* Python Configuration::
* Path Configuration::
* Py_RunMain(): Py_RunMain.
* Multi-Phase Initialization Private Provisional API::
Memory Management
* Overview: Overview<3>.
* Raw Memory Interface::
* Memory Interface::
* Object allocators::
* Default Memory Allocators::
* Customize Memory Allocators::
* The pymalloc allocator::
* tracemalloc C API::
* Examples: Examples<34>.
The pymalloc allocator
* Customize pymalloc Arena Allocator::
Object Implementation Support
* Allocating Objects on the Heap::
* Common Object Structures::
* Type Objects: Type Objects<3>.
* Number Object Structures::
* Mapping Object Structures::
* Sequence Object Structures::
* Buffer Object Structures::
* Async Object Structures::
* Slot Type typedefs::
* Examples: Examples<35>.
* Supporting Cyclic Garbage Collection::
Type Objects
* Quick Reference::
* PyTypeObject Definition::
* PyObject Slots::
* PyVarObject Slots::
* PyTypeObject Slots::
* Heap Types::
Quick Reference
* “tp slots”::
* sub-slots::
* slot typedefs::
Distributing Python Modules
* Key terms::
* Open source licensing and collaboration::
* Installing the tools::
* Reading the Python Packaging User Guide::
* How do I…?::
How do I…?
* … choose a name for my project?::
* … create and distribute binary extensions?::
Installing Python Modules
* Key terms: Key terms<2>.
* Basic usage::
* How do I …?::
* Common installation issues::
How do I …?
* … install pip in versions of Python prior to Python 3.4?: … install pip in versions of Python prior to Python 3 4?.
* … install packages just for the current user?::
* … install scientific Python packages?::
* … work with multiple versions of Python installed in parallel?::
Common installation issues
* Installing into the system Python on Linux::
* Pip not installed::
* Installing binary extensions::
Python HOWTOs
* Porting Python 2 Code to Python 3::
* Porting Extension Modules to Python 3::
* Curses Programming with Python::
* Descriptor HowTo Guide::
* Functional Programming HOWTO::
* Logging HOWTO::
* Logging Cookbook::
* Regular Expression HOWTO::
* Socket Programming HOWTO::
* Sorting HOW TO::
* Unicode HOWTO::
* HOWTO Fetch Internet Resources Using The urllib Package::
* Argparse Tutorial::
* An introduction to the ipaddress module::
* Argument Clinic How-To::
* Instrumenting CPython with DTrace and SystemTap::
Porting Python 2 Code to Python 3
* The Short Explanation::
* Details::
Details
* Drop support for Python 2.6 and older: Drop support for Python 2 6 and older.
* Make sure you specify the proper version support in your setup.py file: Make sure you specify the proper version support in your setup py file.
* Have good test coverage::
* Learn the differences between Python 2 & 3::
* Update your code::
* Prevent compatibility regressions::
* Check which dependencies block your transition::
* Update your setup.py file to denote Python 3 compatibility: Update your setup py file to denote Python 3 compatibility.
* Use continuous integration to stay compatible::
* Consider using optional static type checking::
Update your code
* Division::
* Text versus binary data::
* Use feature detection instead of version detection::
Curses Programming with Python
* What is curses?::
* Starting and ending a curses application::
* Windows and Pads::
* Displaying Text::
* User Input::
* For More Information::
What is curses?
* The Python curses module::
Displaying Text
* Attributes and Color::
Descriptor HowTo Guide
* Abstract::
* Definition and Introduction::
* Descriptor Protocol::
* Invoking Descriptors: Invoking Descriptors<2>.
* Descriptor Example::
* Properties::
* Functions and Methods::
* Static Methods and Class Methods::
Functional Programming HOWTO
* Introduction: Introduction<14>.
* Iterators: Iterators<2>.
* Generator expressions and list comprehensions::
* Generators: Generators<2>.
* Built-in functions::
* The itertools module::
* The functools module::
* Small functions and the lambda expression::
* Revision History and Acknowledgements::
* References: References<2>.
Introduction
* Formal provability::
* Modularity::
* Ease of debugging and testing::
* Composability::
Iterators
* Data Types That Support Iterators::
Generators
* Passing values into a generator::
The itertools module
* Creating new iterators::
* Calling functions on elements::
* Selecting elements::
* Combinatoric functions::
* Grouping elements::
The functools module
* The operator module::
References
* General::
* Python-specific::
* Python documentation::
Logging HOWTO
* Basic Logging Tutorial::
* Advanced Logging Tutorial::
* Logging Levels: Logging Levels<2>.
* Useful Handlers::
* Exceptions raised during logging::
* Using arbitrary objects as messages::
* Optimization::
Basic Logging Tutorial
* When to use logging::
* A simple example::
* Logging to a file::
* Logging from multiple modules::
* Logging variable data::
* Changing the format of displayed messages::
* Displaying the date/time in messages::
* Next Steps::
Advanced Logging Tutorial
* Logging Flow::
* Loggers::
* Handlers::
* Formatters::
* Configuring Logging::
* What happens if no configuration is provided::
* Configuring Logging for a Library::
Logging Levels
* Custom Levels::
Logging Cookbook
* Using logging in multiple modules::
* Logging from multiple threads::
* Multiple handlers and formatters::
* Logging to multiple destinations::
* Configuration server example::
* Dealing with handlers that block::
* Sending and receiving logging events across a network::
* Adding contextual information to your logging output::
* Logging to a single file from multiple processes::
* Using file rotation::
* Use of alternative formatting styles::
* Customizing LogRecord::
* Subclassing QueueHandler - a ZeroMQ example::
* Subclassing QueueListener - a ZeroMQ example::
* An example dictionary-based configuration::
* Using a rotator and namer to customize log rotation processing::
* A more elaborate multiprocessing example::
* Inserting a BOM into messages sent to a SysLogHandler::
* Implementing structured logging::
* Customizing handlers with dictConfig(): Customizing handlers with dictConfig.
* Using particular formatting styles throughout your application::
* Configuring filters with dictConfig(): Configuring filters with dictConfig.
* Customized exception formatting::
* Speaking logging messages::
* Buffering logging messages and outputting them conditionally::
* Formatting times using UTC (GMT) via configuration: Formatting times using UTC GMT via configuration.
* Using a context manager for selective logging::
* A CLI application starter template::
* A Qt GUI for logging::
Adding contextual information to your logging output
* Using LoggerAdapters to impart contextual information::
* Using Filters to impart contextual information::
Using LoggerAdapters to impart contextual information
* Using objects other than dicts to pass contextual information::
Logging to a single file from multiple processes
* Using concurrent.futures.ProcessPoolExecutor: Using concurrent futures ProcessPoolExecutor.
Using particular formatting styles throughout your application
* Using LogRecord factories::
* Using custom message objects::
Regular Expression HOWTO
* Introduction: Introduction<15>.
* Simple Patterns::
* Using Regular Expressions::
* More Pattern Power::
* Modifying Strings::
* Common Problems::
* Feedback::
Simple Patterns
* Matching Characters::
* Repeating Things::
Using Regular Expressions
* Compiling Regular Expressions::
* The Backslash Plague::
* Performing Matches::
* Module-Level Functions: Module-Level Functions<2>.
* Compilation Flags::
More Pattern Power
* More Metacharacters::
* Grouping::
* Non-capturing and Named Groups::
* Lookahead Assertions::
Modifying Strings
* Splitting Strings::
* Search and Replace::
Common Problems
* Use String Methods::
* match() versus search(): match versus search.
* Greedy versus Non-Greedy::
* Using re.VERBOSE: Using re VERBOSE.
Socket Programming HOWTO
* Sockets::
* Creating a Socket::
* Using a Socket::
* Disconnecting::
* Non-blocking Sockets::
Sockets
* History::
Creating a Socket
* IPC::
Using a Socket
* Binary Data::
Disconnecting
* When Sockets Die::
Sorting HOW TO
* Sorting Basics::
* Key Functions::
* Operator Module Functions::
* Ascending and Descending::
* Sort Stability and Complex Sorts::
* The Old Way Using Decorate-Sort-Undecorate::
* The Old Way Using the cmp Parameter::
* Odd and Ends::
Unicode HOWTO
* Introduction to Unicode::
* Python’s Unicode Support::
* Reading and Writing Unicode Data::
* Acknowledgements: Acknowledgements<10>.
Introduction to Unicode
* Definitions::
* Encodings::
* References: References<3>.
Python’s Unicode Support
* The String Type::
* Converting to Bytes::
* Unicode Literals in Python Source Code::
* Unicode Properties::
* Comparing Strings::
* Unicode Regular Expressions::
* References: References<4>.
Reading and Writing Unicode Data
* Unicode filenames::
* Tips for Writing Unicode-aware Programs::
* References: References<5>.
Tips for Writing Unicode-aware Programs
* Converting Between File Encodings::
* Files in an Unknown Encoding::
HOWTO Fetch Internet Resources Using The urllib Package
* Introduction: Introduction<16>.
* Fetching URLs::
* Handling Exceptions: Handling Exceptions<2>.
* info and geturl::
* Openers and Handlers::
* Basic Authentication::
* Proxies::
* Sockets and Layers::
* Footnotes::
Fetching URLs
* Data::
* Headers::
Handling Exceptions
* URLError::
* HTTPError::
* Wrapping it Up::
HTTPError
* Error Codes::
Wrapping it Up
* Number 1::
* Number 2::
Argparse Tutorial
* Concepts::
* The basics::
* Introducing Positional arguments::
* Introducing Optional arguments::
* Combining Positional and Optional arguments::
* Getting a little more advanced::
* Conclusion::
Introducing Optional arguments
* Short options::
Getting a little more advanced
* Conflicting options::
An introduction to the ipaddress module
* Creating Address/Network/Interface objects::
* Inspecting Address/Network/Interface Objects::
* Networks as lists of Addresses::
* Comparisons: Comparisons<4>.
* Using IP Addresses with other modules::
* Getting more detail when instance creation fails::
Creating Address/Network/Interface objects
* A Note on IP Versions::
* IP Host Addresses::
* Defining Networks::
* Host Interfaces::
Argument Clinic How-To
* The Goals Of Argument Clinic::
* Basic Concepts And Usage::
* Converting Your First Function::
* Advanced Topics::
Advanced Topics
* Symbolic default values::
* Renaming the C functions and variables generated by Argument Clinic::
* Converting functions using PyArg_UnpackTuple::
* Optional Groups::
* Using real Argument Clinic converters, instead of “legacy converters”: Using real Argument Clinic converters instead of “legacy converters”.
* Py_buffer::
* Advanced converters::
* Parameter default values::
* The NULL default value::
* Expressions specified as default values::
* Using a return converter::
* Cloning existing functions::
* Calling Python code::
* Using a “self converter”::
* Writing a custom converter::
* Writing a custom return converter::
* METH_O and METH_NOARGS::
* tp_new and tp_init functions::
* Changing and redirecting Clinic’s output::
* The #ifdef trick::
* Using Argument Clinic in Python files::
Instrumenting CPython with DTrace and SystemTap
* Enabling the static markers::
* Static DTrace probes::
* Static SystemTap markers::
* Available static markers::
* SystemTap Tapsets::
* Examples: Examples<36>.
Python Frequently Asked Questions
* General Python FAQ::
* Programming FAQ::
* Design and History FAQ::
* Library and Extension FAQ::
* Extending/Embedding FAQ::
* Python on Windows FAQ::
* Graphic User Interface FAQ::
* “Why is Python Installed on my Computer?” FAQ::
General Python FAQ
* General Information::
* Python in the real world::
General Information
* What is Python?::
* What is the Python Software Foundation?::
* Are there copyright restrictions on the use of Python?::
* Why was Python created in the first place?::
* What is Python good for?::
* How does the Python version numbering scheme work?::
* How do I obtain a copy of the Python source?::
* How do I get documentation on Python?::
* I’ve never programmed before. Is there a Python tutorial?: I’ve never programmed before Is there a Python tutorial?.
* Is there a newsgroup or mailing list devoted to Python?::
* How do I get a beta test version of Python?::
* How do I submit bug reports and patches for Python?::
* Are there any published articles about Python that I can reference?::
* Are there any books on Python?::
* Where in the world is www.python.org located?: Where in the world is www python org located?.
* Why is it called Python?::
* Do I have to like “Monty Python’s Flying Circus”?::
Python in the real world
* How stable is Python?::
* How many people are using Python?::
* Have any significant projects been done in Python?::
* What new developments are expected for Python in the future?::
* Is it reasonable to propose incompatible changes to Python?::
* Is Python a good language for beginning programmers?::
Programming FAQ
* General Questions::
* Core Language::
* Numbers and strings::
* Performance: Performance<4>.
* Sequences (Tuples/Lists): Sequences Tuples/Lists.
* Objects::
* Modules: Modules<3>.
General Questions
* Is there a source code level debugger with breakpoints, single-stepping, etc.?: Is there a source code level debugger with breakpoints single-stepping etc ?.
* Are there tools to help find bugs or perform static analysis?::
* How can I create a stand-alone binary from a Python script?::
* Are there coding standards or a style guide for Python programs?::
Core Language
* Why am I getting an UnboundLocalError when the variable has a value?::
* What are the rules for local and global variables in Python?::
* Why do lambdas defined in a loop with different values all return the same result?::
* How do I share global variables across modules?::
* What are the “best practices” for using import in a module?::
* Why are default values shared between objects?::
* How can I pass optional or keyword parameters from one function to another?::
* What is the difference between arguments and parameters?::
* Why did changing list ‘y’ also change list ‘x’?::
* How do I write a function with output parameters (call by reference)?: How do I write a function with output parameters call by reference ?.
* How do you make a higher order function in Python?::
* How do I copy an object in Python?::
* How can I find the methods or attributes of an object?::
* How can my code discover the name of an object?::
* What’s up with the comma operator’s precedence?::
* Is there an equivalent of C’s “?;” ternary operator?: Is there an equivalent of C’s “? ” ternary operator?.
* Is it possible to write obfuscated one-liners in Python?::
* What does the slash(/) in the parameter list of a function mean?: What does the slash / in the parameter list of a function mean?.
Numbers and strings
* How do I specify hexadecimal and octal integers?::
* Why does -22 // 10 return -3?::
* How do I convert a string to a number?::
* How do I convert a number to a string?::
* How do I modify a string in place?::
* How do I use strings to call functions/methods?::
* Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings?: Is there an equivalent to Perl’s chomp for removing trailing newlines from strings?.
* Is there a scanf() or sscanf() equivalent?: Is there a scanf or sscanf equivalent?.
* What does ‘UnicodeDecodeError’ or ‘UnicodeEncodeError’ error mean?::
Performance
* My program is too slow. How do I speed it up?: My program is too slow How do I speed it up?.
* What is the most efficient way to concatenate many strings together?::
Sequences (Tuples/Lists)
* How do I convert between tuples and lists?::
* What’s a negative index?::
* How do I iterate over a sequence in reverse order?::
* How do you remove duplicates from a list?::
* How do you remove multiple items from a list::
* How do you make an array in Python?::
* How do I create a multidimensional list?::
* How do I apply a method to a sequence of objects?::
* Why does a_tuple[i] += [‘item’] raise an exception when the addition works?::
* I want to do a complicated sort; can you do a Schwartzian Transform in Python?: I want to do a complicated sort can you do a Schwartzian Transform in Python?.
* How can I sort one list by values from another list?::
Objects
* What is a class?::
* What is a method?::
* What is self?::
* How do I check if an object is an instance of a given class or of a subclass of it?::
* What is delegation?::
* How do I call a method defined in a base class from a derived class that overrides it?::
* How can I organize my code to make it easier to change the base class?::
* How do I create static class data and static class methods?::
* How can I overload constructors (or methods) in Python?: How can I overload constructors or methods in Python?.
* I try to use __spam and I get an error about _SomeClassName__spam.: I try to use __spam and I get an error about _SomeClassName__spam.
* My class defines __del__ but it is not called when I delete the object.: My class defines __del__ but it is not called when I delete the object.
* How do I get a list of all instances of a given class?::
* Why does the result of id() appear to be not unique?: Why does the result of id appear to be not unique?.
Modules
* How do I create a .pyc file?: How do I create a pyc file?.
* How do I find the current module name?::
* How can I have modules that mutually import each other?::
* __import__(‘x.y.z’) returns ; how do I get z?: __import__ ‘x y z’ returns ; how do I get z?.
* When I edit an imported module and reimport it, the changes don’t show up. Why does this happen?: When I edit an imported module and reimport it the changes don’t show up Why does this happen?.
Design and History FAQ
* Why does Python use indentation for grouping of statements?::
* Why am I getting strange results with simple arithmetic operations?::
* Why are floating-point calculations so inaccurate?::
* Why are Python strings immutable?::
* Why must ‘self’ be used explicitly in method definitions and calls?::
* Why can’t I use an assignment in an expression?::
* Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?: Why does Python use methods for some functionality e g list index but functions for other e g len list ?.
* Why is join() a string method instead of a list or tuple method?: Why is join a string method instead of a list or tuple method?.
* How fast are exceptions?::
* Why isn’t there a switch or case statement in Python?::
* Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?::
* Why can’t lambda expressions contain statements?::
* Can Python be compiled to machine code, C or some other language?: Can Python be compiled to machine code C or some other language?.
* How does Python manage memory?::
* Why doesn’t CPython use a more traditional garbage collection scheme?::
* Why isn’t all memory freed when CPython exits?::
* Why are there separate tuple and list data types?::
* How are lists implemented in CPython?::
* How are dictionaries implemented in CPython?::
* Why must dictionary keys be immutable?::
* Why doesn’t list.sort() return the sorted list?: Why doesn’t list sort return the sorted list?.
* How do you specify and enforce an interface spec in Python?::
* Why is there no goto?::
* Why can’t raw strings (r-strings) end with a backslash?: Why can’t raw strings r-strings end with a backslash?.
* Why doesn’t Python have a “with” statement for attribute assignments?::
* Why are colons required for the if/while/def/class statements?::
* Why does Python allow commas at the end of lists and tuples?::
Library and Extension FAQ
* General Library Questions::
* Common tasks::
* Threads::
* Input and Output: Input and Output<2>.
* Network/Internet Programming::
* Databases::
* Mathematics and Numerics::
General Library Questions
* How do I find a module or application to perform task X?::
* Where is the math.py (socket.py, regex.py, etc.) source file?: Where is the math py socket py regex py etc source file?.
* How do I make a Python script executable on Unix?::
* Is there a curses/termcap package for Python?::
* Is there an equivalent to C’s onexit() in Python?: Is there an equivalent to C’s onexit in Python?.
* Why don’t my signal handlers work?::
Common tasks
* How do I test a Python program or component?::
* How do I create documentation from doc strings?::
* How do I get a single keypress at a time?::
Threads
* How do I program using threads?::
* None of my threads seem to run; why?: None of my threads seem to run why?.
* How do I parcel out work among a bunch of worker threads?::
* What kinds of global value mutation are thread-safe?::
* Can’t we get rid of the Global Interpreter Lock?::
Input and Output
* How do I delete a file? (And other file questions…): How do I delete a file? And other file questions….
* How do I copy a file?::
* How do I read (or write) binary data?: How do I read or write binary data?.
* I can’t seem to use os.read() on a pipe created with os.popen(); why?: I can’t seem to use os read on a pipe created with os popen ; why?.
* How do I access the serial (RS232) port?: How do I access the serial RS232 port?.
* Why doesn’t closing sys.stdout (stdin, stderr) really close it?: Why doesn’t closing sys stdout stdin stderr really close it?.
Network/Internet Programming
* What WWW tools are there for Python?::
* How can I mimic CGI form submission (METHOD=POST)?: How can I mimic CGI form submission METHOD=POST ?.
* What module should I use to help with generating HTML?::
* How do I send mail from a Python script?::
* How do I avoid blocking in the connect() method of a socket?: How do I avoid blocking in the connect method of a socket?.
Databases
* Are there any interfaces to database packages in Python?::
* How do you implement persistent objects in Python?::
Mathematics and Numerics
* How do I generate random numbers in Python?::
Extending/Embedding FAQ
* Can I create my own functions in C?::
* Can I create my own functions in C++?::
* Writing C is hard; are there any alternatives?::
* How can I execute arbitrary Python statements from C?::
* How can I evaluate an arbitrary Python expression from C?::
* How do I extract C values from a Python object?::
* How do I use Py_BuildValue() to create a tuple of arbitrary length?: How do I use Py_BuildValue to create a tuple of arbitrary length?.
* How do I call an object’s method from C?::
* How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?: How do I catch the output from PyErr_Print or anything that prints to stdout/stderr ?.
* How do I access a module written in Python from C?::
* How do I interface to C++ objects from Python?::
* I added a module using the Setup file and the make fails; why?::
* How do I debug an extension?::
* I want to compile a Python module on my Linux system, but some files are missing. Why?: I want to compile a Python module on my Linux system but some files are missing Why?.
* How do I tell “incomplete input” from “invalid input”?::
* How do I find undefined g++ symbols __builtin_new or __pure_virtual?::
* Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?: Can I create an object class with some methods implemented in C and others in Python e g through inheritance ?.
Python on Windows FAQ
* How do I run a Python program under Windows?::
* How do I make Python scripts executable?::
* Why does Python sometimes take so long to start?::
* How do I make an executable from a Python script?::
* Is a *.pyd file the same as a DLL?: Is a * pyd file the same as a DLL?.
* How can I embed Python into a Windows application?::
* How do I keep editors from inserting tabs into my Python source?::
* How do I check for a keypress without blocking?::
Graphic User Interface FAQ
* General GUI Questions::
* What platform-independent GUI toolkits exist for Python?::
* What platform-specific GUI toolkits exist for Python?::
* Tkinter questions::
What platform-independent GUI toolkits exist for Python?
* Tkinter::
* wxWidgets::
* Qt::
* Gtk+::
* Kivy::
* FLTK::
* OpenGL::
Tkinter questions
* How do I freeze Tkinter applications?::
* Can I have Tk events handled while waiting for I/O?::
* I can’t get key bindings to work in Tkinter; why?: I can’t get key bindings to work in Tkinter why?.
“Why is Python Installed on my Computer?” FAQ
* What is Python?: What is Python?<2>.
* Why is Python installed on my machine?::
* Can I delete Python?::
About these documents
* Contributors to the Python Documentation::
Dealing with Bugs
* Documentation bugs::
* Using the Python issue tracker::
* Getting started contributing to Python yourself::
History and License
* History of the software::
* Terms and conditions for accessing or otherwise using Python::
* Licenses and Acknowledgements for Incorporated Software::
Terms and conditions for accessing or otherwise using Python
* PSF LICENSE AGREEMENT FOR PYTHON 3.8.9: PSF LICENSE AGREEMENT FOR PYTHON 3 8 9.
* BEOPEN.COM LICENSE AGREEMENT FOR PYTHON 2.0: BEOPEN COM LICENSE AGREEMENT FOR PYTHON 2 0.
* CNRI LICENSE AGREEMENT FOR PYTHON 1.6.1: CNRI LICENSE AGREEMENT FOR PYTHON 1 6 1.
* CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2: CWI LICENSE AGREEMENT FOR PYTHON 0 9 0 THROUGH 1 2.
* ZERO-CLAUSE BSD LICENSE FOR CODE IN THE PYTHON 3.8.9 DOCUMENTATION: ZERO-CLAUSE BSD LICENSE FOR CODE IN THE PYTHON 3 8 9 DOCUMENTATION.
Licenses and Acknowledgements for Incorporated Software
* Mersenne Twister::
* Sockets: Sockets<2>.
* Asynchronous socket services::
* Cookie management::
* Execution tracing::
* UUencode and UUdecode functions::
* XML Remote Procedure Calls::
* test_epoll::
* Select kqueue::
* SipHash24::
* strtod and dtoa::
* OpenSSL::
* expat::
* libffi::
* zlib: zlib<3>.
* cfuhash::
* libmpdec::
* W3C C14N test suite::
Distributing Python Modules (Legacy version)
* An Introduction to Distutils::
* Writing the Setup Script::
* Writing the Setup Configuration File::
* Creating a Source Distribution::
* Creating Built Distributions::
* Distutils Examples::
* Extending Distutils::
* Command Reference::
* API Reference::
An Introduction to Distutils
* Concepts & Terminology::
* A Simple Example: A Simple Example<2>.
* General Python terminology::
* Distutils-specific terminology::
Writing the Setup Script
* Listing whole packages::
* Listing individual modules::
* Describing extension modules::
* Relationships between Distributions and Packages::
* Installing Scripts::
* Installing Package Data::
* Installing Additional Files::
* Additional meta-data::
* Debugging the setup script::
Describing extension modules
* Extension names and packages::
* Extension source files::
* Preprocessor options::
* Library options::
* Other options::
Creating a Source Distribution
* Specifying the files to distribute::
* Manifest-related options::
Creating Built Distributions
* Creating RPM packages::
* Creating Windows Installers::
* Cross-compiling on Windows::
* Vista User Access Control (UAC): Vista User Access Control UAC.
Cross-compiling on Windows
* The Postinstallation script::
Distutils Examples
* Pure Python distribution (by module): Pure Python distribution by module.
* Pure Python distribution (by package): Pure Python distribution by package.
* Single extension module::
* Checking a package::
* Reading the metadata::
Extending Distutils
* Integrating new commands::
* Adding new distribution types::
Command Reference
* Installing modules; the install command family: Installing modules the install command family.
* Creating a source distribution; the sdist command: Creating a source distribution the sdist command.
Installing modules: the install command family
* install_data::
* install_scripts::
API Reference
* distutils.core — Core Distutils functionality: distutils core — Core Distutils functionality.
* distutils.ccompiler — CCompiler base class: distutils ccompiler — CCompiler base class.
* distutils.unixccompiler — Unix C Compiler: distutils unixccompiler — Unix C Compiler.
* distutils.msvccompiler — Microsoft Compiler: distutils msvccompiler — Microsoft Compiler.
* distutils.bcppcompiler — Borland Compiler: distutils bcppcompiler — Borland Compiler.
* distutils.cygwincompiler — Cygwin Compiler: distutils cygwincompiler — Cygwin Compiler.
* distutils.archive_util — Archiving utilities: distutils archive_util — Archiving utilities.
* distutils.dep_util — Dependency checking: distutils dep_util — Dependency checking.
* distutils.dir_util — Directory tree operations: distutils dir_util — Directory tree operations.
* distutils.file_util — Single file operations: distutils file_util — Single file operations.
* distutils.util — Miscellaneous other utility functions: distutils util — Miscellaneous other utility functions.
* distutils.dist — The Distribution class: distutils dist — The Distribution class.
* distutils.extension — The Extension class: distutils extension — The Extension class.
* distutils.debug — Distutils debug mode: distutils debug — Distutils debug mode.
* distutils.errors — Distutils exceptions: distutils errors — Distutils exceptions.
* distutils.fancy_getopt — Wrapper around the standard getopt module: distutils fancy_getopt — Wrapper around the standard getopt module.
* distutils.filelist — The FileList class: distutils filelist — The FileList class.
* distutils.log — Simple PEP 282-style logging: distutils log — Simple PEP 282-style logging.
* distutils.spawn — Spawn a sub-process: distutils spawn — Spawn a sub-process.
* distutils.sysconfig — System configuration information: distutils sysconfig — System configuration information.
* distutils.text_file — The TextFile class: distutils text_file — The TextFile class.
* distutils.version — Version number classes: distutils version — Version number classes.
* distutils.cmd — Abstract base class for Distutils commands: distutils cmd — Abstract base class for Distutils commands.
* Creating a new Distutils command::
* distutils.command — Individual Distutils commands: distutils command — Individual Distutils commands.
* distutils.command.bdist — Build a binary installer: distutils command bdist — Build a binary installer.
* distutils.command.bdist_packager — Abstract base class for packagers: distutils command bdist_packager — Abstract base class for packagers.
* distutils.command.bdist_dumb — Build a “dumb” installer: distutils command bdist_dumb — Build a “dumb” installer.
* distutils.command.bdist_msi — Build a Microsoft Installer binary package: distutils command bdist_msi — Build a Microsoft Installer binary package.
* distutils.command.bdist_rpm — Build a binary distribution as a Redhat RPM and SRPM: distutils command bdist_rpm — Build a binary distribution as a Redhat RPM and SRPM.
* distutils.command.bdist_wininst — Build a Windows installer: distutils command bdist_wininst — Build a Windows installer.
* distutils.command.sdist — Build a source distribution: distutils command sdist — Build a source distribution.
* distutils.command.build — Build all files of a package: distutils command build — Build all files of a package.
* distutils.command.build_clib — Build any C libraries in a package: distutils command build_clib — Build any C libraries in a package.
* distutils.command.build_ext — Build any extensions in a package: distutils command build_ext — Build any extensions in a package.
* distutils.command.build_py — Build the .py/.pyc files of a package: distutils command build_py — Build the py/ pyc files of a package.
* distutils.command.build_scripts — Build the scripts of a package: distutils command build_scripts — Build the scripts of a package.
* distutils.command.clean — Clean a package build area: distutils command clean — Clean a package build area.
* distutils.command.config — Perform package configuration: distutils command config — Perform package configuration.
* distutils.command.install — Install a package: distutils command install — Install a package.
* distutils.command.install_data — Install data files from a package: distutils command install_data — Install data files from a package.
* distutils.command.install_headers — Install C/C++ header files from a package: distutils command install_headers — Install C/C++ header files from a package.
* distutils.command.install_lib — Install library files from a package: distutils command install_lib — Install library files from a package.
* distutils.command.install_scripts — Install script files from a package: distutils command install_scripts — Install script files from a package.
* distutils.command.register — Register a module with the Python Package Index: distutils command register — Register a module with the Python Package Index.
* distutils.command.check — Check the meta-data of a package: distutils command check — Check the meta-data of a package.
Installing Python Modules (Legacy version)
* Introduction: Introduction<17>.
* Standard Build and Install::
* Alternate Installation::
* Custom Installation::
* Distutils Configuration Files::
* Building Extensions; Tips and Tricks: Building Extensions Tips and Tricks.
Introduction
* Distutils based source distributions::
Standard Build and Install
* Platform variations::
* Splitting the job up::
* How building works::
* How installation works::
Alternate Installation
* Alternate installation; the user scheme: Alternate installation the user scheme.
* Alternate installation; the home scheme: Alternate installation the home scheme.
* Alternate installation; Unix (the prefix scheme): Alternate installation Unix the prefix scheme.
* Alternate installation; Windows (the prefix scheme): Alternate installation Windows the prefix scheme.
Custom Installation
* Modifying Python’s Search Path::
Distutils Configuration Files
* Location and names of config files::
* Syntax of config files::
Building Extensions: Tips and Tricks
* Tweaking compiler/linker flags::
* Using non-Microsoft compilers on Windows::
Using non-Microsoft compilers on Windows
* Borland/CodeGear C++::
* GNU C / Cygwin / MinGW::
GNU C / Cygwin / MinGW
* Older Versions of Python and MinGW::
File: python.info, Node: What’s New in Python, Next: The Python Tutorial, Prev: Top, Up: Top
1 What’s New in Python
**********************
The “What’s New in Python” series of essays takes tours through the most
important changes between major Python versions. They are a “must read”
for anyone wishing to stay up-to-date after a new release.
* Menu:
* What’s New In Python 3.8: What’s New In Python 3 8.
* What’s New In Python 3.7: What’s New In Python 3 7.
* What’s New In Python 3.6: What’s New In Python 3 6.
* What’s New In Python 3.5: What’s New In Python 3 5.
* What’s New In Python 3.4: What’s New In Python 3 4.
* What’s New In Python 3.3: What’s New In Python 3 3.
* What’s New In Python 3.2: What’s New In Python 3 2.
* What’s New In Python 3.1: What’s New In Python 3 1.
* What’s New In Python 3.0: What’s New In Python 3 0.
* What’s New in Python 2.7: What’s New in Python 2 7.
* What’s New in Python 2.6: What’s New in Python 2 6.
* What’s New in Python 2.5: What’s New in Python 2 5.
* What’s New in Python 2.4: What’s New in Python 2 4.
* What’s New in Python 2.3: What’s New in Python 2 3.
* What’s New in Python 2.2: What’s New in Python 2 2.
* What’s New in Python 2.1: What’s New in Python 2 1.
* What’s New in Python 2.0: What’s New in Python 2 0.
* Changelog::
File: python.info, Node: What’s New In Python 3 8, Next: What’s New In Python 3 7, Up: What’s New in Python
1.1 What’s New In Python 3.8
============================
Editor: Raymond Hettinger
This article explains the new features in Python 3.8, compared to 3.7.
For full details, see the *note changelog: 14c.
Python 3.8 was released on October 14th, 2019.
* Menu:
* Summary – Release highlights::
* New Features::
* Other Language Changes::
* New Modules::
* Improved Modules::
* Optimizations::
* Build and C API Changes::
* Deprecated::
* API and Feature Removals::
* Porting to Python 3.8: Porting to Python 3 8.
* Notable changes in Python 3.8.1: Notable changes in Python 3 8 1.
* Notable changes in Python 3.8.2: Notable changes in Python 3 8 2.
* Notable changes in Python 3.8.3: Notable changes in Python 3 8 3.
* Notable changes in Python 3.8.8: Notable changes in Python 3 8 8.
* Notable changes in Python 3.8.9: Notable changes in Python 3 8 9.
File: python.info, Node: Summary – Release highlights, Next: New Features, Up: What’s New In Python 3 8
1.1.1 Summary – Release highlights
----------------------------------
File: python.info, Node: New Features, Next: Other Language Changes, Prev: Summary – Release highlights, Up: What’s New In Python 3 8
1.1.2 New Features
------------------
* Menu:
* Assignment expressions::
* Positional-only parameters::
* Parallel filesystem cache for compiled bytecode files::
* Debug build uses the same ABI as release build::
* f-strings support = for self-documenting expressions and debugging::
* PEP 578; Python Runtime Audit Hooks: PEP 578 Python Runtime Audit Hooks.
* PEP 587; Python Initialization Configuration: PEP 587 Python Initialization Configuration.
* Vectorcall; a fast calling protocol for CPython: Vectorcall a fast calling protocol for CPython.
* Pickle protocol 5 with out-of-band data buffers::
File: python.info, Node: Assignment expressions, Next: Positional-only parameters, Up: New Features
1.1.2.1 Assignment expressions
..............................
There is new syntax ‘:=’ that assigns values to variables as part of a
larger expression. It is affectionately known as “the walrus operator”
due to its resemblance to the eyes and tusks of a walrus(1).
In this example, the assignment expression helps avoid calling *note
len(): 150. twice:
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
A similar benefit arises during regular expression matching where match
objects are needed twice, once to test whether a match occurred and
another to extract a subgroup:
discount = 0.0
if (mo := re.search(r'(\d+)% discount', advertisement)):
discount = float(mo.group(1)) / 100.0
The operator is also useful with while-loops that compute a value to
test loop termination and then need that same value again in the body of
the loop:
# Loop over fixed length blocks
while (block := f.read(256)) != '':
process(block)
Another motivating use case arises in list comprehensions where a value
computed in a filtering condition is also needed in the expression body:
[clean_name.title() for name in names
if (clean_name := normalize('NFC', name)) in allowed_names]
Try to limit use of the walrus operator to clean cases that reduce
complexity and improve readability.
See PEP 572(2) for a full description.
(Contributed by Emily Morehouse in bpo-35224(3).)
---------- Footnotes ----------
(1)
https://en.wikipedia.org/wiki/Walrus#/media/File:Pacific_Walrus_-_Bull_(8247646168).jpg
(2) https://www.python.org/dev/peps/pep-0572
(3) https://bugs.python.org/issue35224
File: python.info, Node: Positional-only parameters, Next: Parallel filesystem cache for compiled bytecode files, Prev: Assignment expressions, Up: New Features
1.1.2.2 Positional-only parameters
..................................
There is a new function parameter syntax ‘/’ to indicate that some
function parameters must be specified positionally and cannot be used as
keyword arguments. This is the same notation shown by ‘help()’ for C
functions annotated with Larry Hastings’ Argument Clinic(1) tool.
In the following example, parameters `a' and `b' are positional-only,
while `c' or `d' can be positional or keyword, and `e' or `f' are
required to be keywords:
def f(a, b, /, c, d, *, e, f):
print(a, b, c, d, e, f)
The following is a valid call:
f(10, 20, 30, d=40, e=50, f=60)
However, these are invalid calls:
f(10, b=20, c=30, d=40, e=50, f=60) # b cannot be a keyword argument
f(10, 20, 30, 40, 50, f=60) # e must be a keyword argument
One use case for this notation is that it allows pure Python functions
to fully emulate behaviors of existing C coded functions. For example,
the built-in *note divmod(): 152. function does not accept keyword
arguments:
def divmod(a, b, /):
"Emulate the built in divmod() function"
return (a // b, a % b)
Another use case is to preclude keyword arguments when the parameter
name is not helpful. For example, the builtin *note len(): 150.
function has the signature ‘len(obj, /)’. This precludes awkward calls
such as:
len(obj='hello') # The "obj" keyword argument impairs readability
A further benefit of marking a parameter as positional-only is that it
allows the parameter name to be changed in the future without risk of
breaking client code. For example, in the *note statistics: f5. module,
the parameter name `dist' may be changed in the future. This was made
possible with the following function specification:
def quantiles(dist, /, *, n=4, method='exclusive')
...
Since the parameters to the left of ‘/’ are not exposed as possible
keywords, the parameters names remain available for use in ‘**kwargs’:
>>> def f(a, b, /, **kwargs):
... print(a, b, kwargs)
...
>>> f(10, 20, a=1, b=2, c=3) # a and b are used in two ways
10 20 {'a': 1, 'b': 2, 'c': 3}
This greatly simplifies the implementation of functions and methods that
need to accept arbitrary keyword arguments. For example, here is an
excerpt from code in the *note collections: 1e. module:
class Counter(dict):
def __init__(self, iterable=None, /, **kwds):
# Note "iterable" is a possible keyword argument
See PEP 570(2) for a full description.
(Contributed by Pablo Galindo in bpo-36540(3).)
---------- Footnotes ----------
(1) https://docs.python.org/3/howto/clinic.html
(2) https://www.python.org/dev/peps/pep-0570
(3) https://bugs.python.org/issue36540
File: python.info, Node: Parallel filesystem cache for compiled bytecode files, Next: Debug build uses the same ABI as release build, Prev: Positional-only parameters, Up: New Features
1.1.2.3 Parallel filesystem cache for compiled bytecode files
.............................................................
The new *note PYTHONPYCACHEPREFIX: 154. setting (also available as *note
-X: 155. ‘pycache_prefix’) configures the implicit bytecode cache to use
a separate parallel filesystem tree, rather than the default
‘__pycache__’ subdirectories within each source directory.
The location of the cache is reported in *note sys.pycache_prefix: 156.
(*note None: 157. indicates the default location in ‘__pycache__’
subdirectories).
(Contributed by Carl Meyer in bpo-33499(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33499
File: python.info, Node: Debug build uses the same ABI as release build, Next: f-strings support = for self-documenting expressions and debugging, Prev: Parallel filesystem cache for compiled bytecode files, Up: New Features
1.1.2.4 Debug build uses the same ABI as release build
......................................................
Python now uses the same ABI whether it’s built in release or debug
mode. On Unix, when Python is built in debug mode, it is now possible
to load C extensions built in release mode and C extensions built using
the stable ABI.
Release builds and debug builds are now ABI compatible: defining the
‘Py_DEBUG’ macro no longer implies the ‘Py_TRACE_REFS’ macro, which
introduces the only ABI incompatibility. The ‘Py_TRACE_REFS’ macro,
which adds the ‘sys.getobjects()’ function and the *note PYTHONDUMPREFS:
159. environment variable, can be set using the new ‘./configure
--with-trace-refs’ build option. (Contributed by Victor Stinner in
bpo-36465(1).)
On Unix, C extensions are no longer linked to libpython except on
Android and Cygwin. It is now possible for a statically linked Python
to load a C extension built using a shared library Python. (Contributed
by Victor Stinner in bpo-21536(2).)
On Unix, when Python is built in debug mode, import now also looks for C
extensions compiled in release mode and for C extensions compiled with
the stable ABI. (Contributed by Victor Stinner in bpo-36722(3).)
To embed Python into an application, a new ‘--embed’ option must be
passed to ‘python3-config --libs --embed’ to get ‘-lpython3.8’ (link the
application to libpython). To support both 3.8 and older, try
‘python3-config --libs --embed’ first and fallback to ‘python3-config
--libs’ (without ‘--embed’) if the previous command fails.
Add a pkg-config ‘python-3.8-embed’ module to embed Python into an
application: ‘pkg-config python-3.8-embed --libs’ includes
‘-lpython3.8’. To support both 3.8 and older, try ‘pkg-config
python-X.Y-embed --libs’ first and fallback to ‘pkg-config python-X.Y
--libs’ (without ‘--embed’) if the previous command fails (replace ‘X.Y’
with the Python version).
On the other hand, ‘pkg-config python3.8 --libs’ no longer contains
‘-lpython3.8’. C extensions must not be linked to libpython (except on
Android and Cygwin, whose cases are handled by the script); this change
is backward incompatible on purpose. (Contributed by Victor Stinner in
bpo-36721(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36465
(2) https://bugs.python.org/issue21536
(3) https://bugs.python.org/issue36722
(4) https://bugs.python.org/issue36721
File: python.info, Node: f-strings support = for self-documenting expressions and debugging, Next: PEP 578 Python Runtime Audit Hooks, Prev: Debug build uses the same ABI as release build, Up: New Features
1.1.2.5 f-strings support ‘=’ for self-documenting expressions and debugging
............................................................................
Added an ‘=’ specifier to *note f-string: 15b.s. An f-string such as
‘f'{expr=}'’ will expand to the text of the expression, an equal sign,
then the representation of the evaluated expression. For example:
>>> user = 'eric_idle'
>>> member_since = date(1975, 7, 31)
>>> f'{user=} {member_since=}'
"user='eric_idle' member_since=datetime.date(1975, 7, 31)"
The usual *note f-string format specifiers: 15c. allow more control over
how the result of the expression is displayed:
>>> delta = date.today() - member_since
>>> f'{user=!s} {delta.days=:,d}'
'user=eric_idle delta.days=16,075'
The ‘=’ specifier will display the whole expression so that calculations
can be shown:
>>> print(f'{theta=} {cos(radians(theta))=:.3f}')
theta=30 cos(radians(theta))=0.866
(Contributed by Eric V. Smith and Larry Hastings in bpo-36817(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36817
File: python.info, Node: PEP 578 Python Runtime Audit Hooks, Next: PEP 587 Python Initialization Configuration, Prev: f-strings support = for self-documenting expressions and debugging, Up: New Features
1.1.2.6 PEP 578: Python Runtime Audit Hooks
...........................................
The PEP adds an Audit Hook and Verified Open Hook. Both are available
from Python and native code, allowing applications and frameworks
written in pure Python code to take advantage of extra notifications,
while also allowing embedders or system administrators to deploy builds
of Python where auditing is always enabled.
See PEP 578(1) for full details.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0578
File: python.info, Node: PEP 587 Python Initialization Configuration, Next: Vectorcall a fast calling protocol for CPython, Prev: PEP 578 Python Runtime Audit Hooks, Up: New Features
1.1.2.7 PEP 587: Python Initialization Configuration
....................................................
The PEP 587(1) adds a new C API to configure the Python Initialization
providing finer control on the whole configuration and better error
reporting.
New structures:
* *note PyConfig: 15f.
* *note PyPreConfig: 160.
* *note PyStatus: 161.
* *note PyWideStringList: 162.
New functions:
* *note PyConfig_Clear(): 163.
* *note PyConfig_InitIsolatedConfig(): 164.
* *note PyConfig_InitPythonConfig(): 165.
* *note PyConfig_Read(): 166.
* *note PyConfig_SetArgv(): 167.
* *note PyConfig_SetBytesArgv(): 168.
* *note PyConfig_SetBytesString(): 169.
* *note PyConfig_SetString(): 16a.
* *note PyPreConfig_InitIsolatedConfig(): 16b.
* *note PyPreConfig_InitPythonConfig(): 16c.
* *note PyStatus_Error(): 16d.
* *note PyStatus_Exception(): 16e.
* *note PyStatus_Exit(): 16f.
* *note PyStatus_IsError(): 170.
* *note PyStatus_IsExit(): 171.
* *note PyStatus_NoMemory(): 172.
* *note PyStatus_Ok(): 173.
* *note PyWideStringList_Append(): 174.
* *note PyWideStringList_Insert(): 175.
* *note Py_BytesMain(): 176.
* *note Py_ExitStatusException(): 177.
* *note Py_InitializeFromConfig(): 178.
* *note Py_PreInitialize(): 179.
* *note Py_PreInitializeFromArgs(): 17a.
* *note Py_PreInitializeFromBytesArgs(): 17b.
* *note Py_RunMain(): 17c.
This PEP also adds ‘_PyRuntimeState.preconfig’ (*note PyPreConfig: 160.
type) and ‘PyInterpreterState.config’ (*note PyConfig: 15f. type) fields
to these internal structures. ‘PyInterpreterState.config’ becomes the
new reference configuration, replacing global configuration variables
and other private variables.
See *note Python Initialization Configuration: 17d. for the
documentation.
See PEP 587(2) for a full description.
(Contributed by Victor Stinner in bpo-36763(3).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0587
(2) https://www.python.org/dev/peps/pep-0587
(3) https://bugs.python.org/issue36763
File: python.info, Node: Vectorcall a fast calling protocol for CPython, Next: Pickle protocol 5 with out-of-band data buffers, Prev: PEP 587 Python Initialization Configuration, Up: New Features
1.1.2.8 Vectorcall: a fast calling protocol for CPython
.......................................................
The “vectorcall” protocol is added to the Python/C API. It is meant to
formalize existing optimizations which were already done for various
classes. Any extension type implementing a callable can use this
protocol.
This is currently provisional. The aim is to make it fully public in
Python 3.9.
See PEP 590(1) for a full description.
(Contributed by Jeroen Demeyer and Mark Shannon in bpo-36974(2).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0590
(2) https://bugs.python.org/issue36974
File: python.info, Node: Pickle protocol 5 with out-of-band data buffers, Prev: Vectorcall a fast calling protocol for CPython, Up: New Features
1.1.2.9 Pickle protocol 5 with out-of-band data buffers
.......................................................
When *note pickle: ca. is used to transfer large data between Python
processes in order to take advantage of multi-core or multi-machine
processing, it is important to optimize the transfer by reducing memory
copies, and possibly by applying custom techniques such as
data-dependent compression.
The *note pickle: ca. protocol 5 introduces support for out-of-band
buffers where PEP 3118(1)-compatible data can be transmitted separately
from the main pickle stream, at the discretion of the communication
layer.
See PEP 574(2) for a full description.
(Contributed by Antoine Pitrou in bpo-36785(3).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3118
(2) https://www.python.org/dev/peps/pep-0574
(3) https://bugs.python.org/issue36785
File: python.info, Node: Other Language Changes, Next: New Modules, Prev: New Features, Up: What’s New In Python 3 8
1.1.3 Other Language Changes
----------------------------
* A *note continue: 181. statement was illegal in the *note finally:
182. clause due to a problem with the implementation. In Python
3.8 this restriction was lifted. (Contributed by Serhiy Storchaka
in bpo-32489(1).)
* The *note bool: 183, *note int: 184, and *note fractions.Fraction:
185. types now have an *note as_integer_ratio(): 186. method like
that found in *note float: 187. and *note decimal.Decimal: 188.
This minor API extension makes it possible to write ‘numerator,
denominator = x.as_integer_ratio()’ and have it work across
multiple numeric types. (Contributed by Lisa Roach in bpo-33073(2)
and Raymond Hettinger in bpo-37819(3).)
* Constructors of *note int: 184, *note float: 187. and *note
complex: 189. will now use the *note __index__(): 18a. special
method, if available and the corresponding method *note __int__():
18b, *note __float__(): 18c. or *note __complex__(): 18d. is not
available. (Contributed by Serhiy Storchaka in bpo-20092(4).)
* Added support of ‘\N{name}’ escapes in *note regular expressions:
dd.:
>>> notice = 'Copyright © 2019'
>>> copyright_year_pattern = re.compile(r'\N{copyright sign}\s*(\d{4})')
>>> int(copyright_year_pattern.search(notice).group(1))
2019
(Contributed by Jonathan Eunice and Serhiy Storchaka in
bpo-30688(5).)
* Dict and dictviews are now iterable in reversed insertion order
using *note reversed(): 18e. (Contributed by Rémi Lapeyre in
bpo-33462(6).)
* The syntax allowed for keyword names in function calls was further
restricted. In particular, ‘f((keyword)=arg)’ is no longer
allowed. It was never intended to permit more than a bare name on
the left-hand side of a keyword argument assignment term.
(Contributed by Benjamin Peterson in bpo-34641(7).)
* Generalized iterable unpacking in *note yield: 18f. and *note
return: 190. statements no longer requires enclosing parentheses.
This brings the `yield' and `return' syntax into better agreement
with normal assignment syntax:
>>> def parse(family):
lastname, *members = family.split()
return lastname.upper(), *members
>>> parse('simpsons homer marge bart lisa sally')
('SIMPSONS', 'homer', 'marge', 'bart', 'lisa', 'sally')
(Contributed by David Cuthbert and Jordan Chapman in bpo-32117(8).)
* When a comma is missed in code such as ‘[(10, 20) (30, 40)]’, the
compiler displays a *note SyntaxWarning: 191. with a helpful
suggestion. This improves on just having a *note TypeError: 192.
indicating that the first tuple was not callable. (Contributed by
Serhiy Storchaka in bpo-15248(9).)
* Arithmetic operations between subclasses of *note datetime.date:
193. or *note datetime.datetime: 194. and *note datetime.timedelta:
195. objects now return an instance of the subclass, rather than
the base class. This also affects the return type of operations
whose implementation (directly or indirectly) uses *note
datetime.timedelta: 195. arithmetic, such as *note astimezone():
196. (Contributed by Paul Ganssle in bpo-32417(10).)
* When the Python interpreter is interrupted by Ctrl-C (SIGINT) and
the resulting *note KeyboardInterrupt: 197. exception is not
caught, the Python process now exits via a SIGINT signal or with
the correct exit code such that the calling process can detect that
it died due to a Ctrl-C. Shells on POSIX and Windows use this to
properly terminate scripts in interactive sessions. (Contributed
by Google via Gregory P. Smith in bpo-1054041(11).)
* Some advanced styles of programming require updating the *note
types.CodeType: 198. object for an existing function. Since code
objects are immutable, a new code object needs to be created, one
that is modeled on the existing code object. With 19 parameters,
this was somewhat tedious. Now, the new ‘replace()’ method makes
it possible to create a clone with a few altered parameters.
Here’s an example that alters the *note statistics.mean(): 199.
function to prevent the `data' parameter from being used as a
keyword argument:
>>> from statistics import mean
>>> mean(data=[10, 20, 90])
40
>>> mean.__code__ = mean.__code__.replace(co_posonlyargcount=1)
>>> mean(data=[10, 20, 90])
Traceback (most recent call last):
...
TypeError: mean() got some positional-only arguments passed as keyword arguments: 'data'
(Contributed by Victor Stinner in bpo-37032(12).)
* For integers, the three-argument form of the *note pow(): 19a.
function now permits the exponent to be negative in the case where
the base is relatively prime to the modulus. It then computes a
modular inverse to the base when the exponent is ‘-1’, and a
suitable power of that inverse for other negative exponents. For
example, to compute the modular multiplicative inverse(13) of 38
modulo 137, write:
>>> pow(38, -1, 137)
119
>>> 119 * 38 % 137
1
Modular inverses arise in the solution of linear Diophantine
equations(14). For example, to find integer solutions for ‘4258𝑥 +
147𝑦 = 369’, first rewrite as ‘4258𝑥 ≡ 369 (mod 147)’ then solve:
>>> x = 369 * pow(4258, -1, 147) % 147
>>> y = (4258 * x - 369) // -147
>>> 4258 * x + 147 * y
369
(Contributed by Mark Dickinson in bpo-36027(15).)
* Dict comprehensions have been synced-up with dict literals so that
the key is computed first and the value second:
>>> # Dict comprehension
>>> cast = {input('role? '): input('actor? ') for i in range(2)}
role? King Arthur
actor? Chapman
role? Black Knight
actor? Cleese
>>> # Dict literal
>>> cast = {input('role? '): input('actor? ')}
role? Sir Robin
actor? Eric Idle
The guaranteed execution order is helpful with assignment
expressions because variables assigned in the key expression will
be available in the value expression:
>>> names = ['Martin von Löwis', 'Łukasz Langa', 'Walter Dörwald']
>>> {(n := normalize('NFC', name)).casefold() : n for name in names}
{'martin von löwis': 'Martin von Löwis',
'łukasz langa': 'Łukasz Langa',
'walter dörwald': 'Walter Dörwald'}
(Contributed by Jörn Heissler in bpo-35224(16).)
* The *note object.__reduce__(): 19b. method can now return a tuple
from two to six elements long. Formerly, five was the limit. The
new, optional sixth element is a callable with a ‘(obj, state)’
signature. This allows the direct control over the state-updating
behavior of a specific object. If not `None', this callable will
have priority over the object’s *note __setstate__(): 19c. method.
(Contributed by Pierre Glaser and Olivier Grisel in bpo-35900(17).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32489
(2) https://bugs.python.org/issue33073
(3) https://bugs.python.org/issue37819
(4) https://bugs.python.org/issue20092
(5) https://bugs.python.org/issue30688
(6) https://bugs.python.org/issue33462
(7) https://bugs.python.org/issue34641
(8) https://bugs.python.org/issue32117
(9) https://bugs.python.org/issue15248
(10) https://bugs.python.org/issue32417
(11) https://bugs.python.org/issue1054041
(12) https://bugs.python.org/issue37032
(13) https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
(14) https://en.wikipedia.org/wiki/Diophantine_equation
(15) https://bugs.python.org/issue36027
(16) https://bugs.python.org/issue35224
(17) https://bugs.python.org/issue35900
File: python.info, Node: New Modules, Next: Improved Modules, Prev: Other Language Changes, Up: What’s New In Python 3 8
1.1.4 New Modules
-----------------
* The new ‘importlib.metadata’ module provides (provisional) support
for reading metadata from third-party packages. For example, it
can extract an installed package’s version number, list of entry
points, and more:
>>> # Note following example requires that the popular "requests"
>>> # package has been installed.
>>>
>>> from importlib.metadata import version, requires, files
>>> version('requests')
'2.22.0'
>>> list(requires('requests'))
['chardet (<3.1.0,>=3.0.2)']
>>> list(files('requests'))[:5]
[PackagePath('requests-2.22.0.dist-info/INSTALLER'),
PackagePath('requests-2.22.0.dist-info/LICENSE'),
PackagePath('requests-2.22.0.dist-info/METADATA'),
PackagePath('requests-2.22.0.dist-info/RECORD'),
PackagePath('requests-2.22.0.dist-info/WHEEL')]
(Contributed by Barry Warsaw and Jason R. Coombs in bpo-34632(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34632
File: python.info, Node: Improved Modules, Next: Optimizations, Prev: New Modules, Up: What’s New In Python 3 8
1.1.5 Improved Modules
----------------------
* Menu:
* ast::
* asyncio::
* builtins::
* collections::
* cProfile::
* csv::
* curses::
* ctypes::
* datetime::
* functools::
* gc::
* gettext::
* gzip::
* IDLE and idlelib::
* inspect::
* io::
* itertools::
* json.tool: json tool.
* logging::
* math::
* mmap::
* multiprocessing::
* os::
* os.path: os path.
* pathlib::
* pickle::
* plistlib::
* pprint::
* py_compile::
* shlex::
* shutil::
* socket::
* ssl::
* statistics::
* sys::
* tarfile::
* threading::
* tokenize::
* tkinter::
* time::
* typing::
* unicodedata::
* unittest::
* venv::
* weakref::
* xml::
* xmlrpc::
File: python.info, Node: ast, Next: asyncio, Up: Improved Modules
1.1.5.1 ast
...........
AST nodes now have ‘end_lineno’ and ‘end_col_offset’ attributes, which
give the precise location of the end of the node. (This only applies to
nodes that have ‘lineno’ and ‘col_offset’ attributes.)
New function *note ast.get_source_segment(): 1a0. returns the source
code for a specific AST node.
(Contributed by Ivan Levkivskyi in bpo-33416(1).)
The *note ast.parse(): 1a1. function has some new flags:
* ‘type_comments=True’ causes it to return the text of PEP 484(2) and
PEP 526(3) type comments associated with certain AST nodes;
* ‘mode='func_type'’ can be used to parse PEP 484(4) “signature type
comments” (returned for function definition AST nodes);
* ‘feature_version=(3, N)’ allows specifying an earlier Python 3
version. For example, ‘feature_version=(3, 4)’ will treat *note
async: 1a2. and *note await: 1a3. as non-reserved words.
(Contributed by Guido van Rossum in bpo-35766(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33416
(2) https://www.python.org/dev/peps/pep-0484
(3) https://www.python.org/dev/peps/pep-0526
(4) https://www.python.org/dev/peps/pep-0484
(5) https://bugs.python.org/issue35766
File: python.info, Node: asyncio, Next: builtins, Prev: ast, Up: Improved Modules
1.1.5.2 asyncio
...............
*note asyncio.run(): 1a5. has graduated from the provisional to stable
API. This function can be used to execute a *note coroutine: 1a6. and
return the result while automatically managing the event loop. For
example:
import asyncio
async def main():
await asyncio.sleep(0)
return 42
asyncio.run(main())
This is `roughly' equivalent to:
import asyncio
async def main():
await asyncio.sleep(0)
return 42
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(main())
finally:
asyncio.set_event_loop(None)
loop.close()
The actual implementation is significantly more complex. Thus, *note
asyncio.run(): 1a5. should be the preferred way of running asyncio
programs.
(Contributed by Yury Selivanov in bpo-32314(1).)
Running ‘python -m asyncio’ launches a natively async REPL. This allows
rapid experimentation with code that has a top-level *note await: 1a3.
There is no longer a need to directly call ‘asyncio.run()’ which would
spawn a new event loop on every invocation:
$ python -m asyncio
asyncio REPL 3.8.0
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> await asyncio.sleep(10, result='hello')
hello
(Contributed by Yury Selivanov in bpo-37028(2).)
The exception *note asyncio.CancelledError: 1a7. now inherits from *note
BaseException: 1a8. rather than *note Exception: 1a9. and no longer
inherits from *note concurrent.futures.CancelledError: 1aa.
(Contributed by Yury Selivanov in bpo-32528(3).)
On Windows, the default event loop is now *note ProactorEventLoop: 1ab.
(Contributed by Victor Stinner in bpo-34687(4).)
*note ProactorEventLoop: 1ab. now also supports UDP. (Contributed by
Adam Meily and Andrew Svetlov in bpo-29883(5).)
*note ProactorEventLoop: 1ab. can now be interrupted by *note
KeyboardInterrupt: 197. (“CTRL+C”). (Contributed by Vladimir Matveev in
bpo-23057(6).)
Added *note asyncio.Task.get_coro(): 1ac. for getting the wrapped
coroutine within an *note asyncio.Task: 1ad. (Contributed by Alex
Grönholm in bpo-36999(7).)
Asyncio tasks can now be named, either by passing the ‘name’ keyword
argument to *note asyncio.create_task(): 1ae. or the *note
create_task(): 1af. event loop method, or by calling the *note
set_name(): 1b0. method on the task object. The task name is visible in
the ‘repr()’ output of *note asyncio.Task: 1ad. and can also be
retrieved using the *note get_name(): 1b1. method. (Contributed by Alex
Grönholm in bpo-34270(8).)
Added support for Happy Eyeballs(9) to *note
asyncio.loop.create_connection(): 1b2. To specify the behavior, two new
parameters have been added: `happy_eyeballs_delay' and `interleave'.
The Happy Eyeballs algorithm improves responsiveness in applications
that support IPv4 and IPv6 by attempting to simultaneously connect using
both. (Contributed by twisteroid ambassador in bpo-33530(10).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32314
(2) https://bugs.python.org/issue37028
(3) https://bugs.python.org/issue32528
(4) https://bugs.python.org/issue34687
(5) https://bugs.python.org/issue29883
(6) https://bugs.python.org/issue23057
(7) https://bugs.python.org/issue36999
(8) https://bugs.python.org/issue34270
(9) https://en.wikipedia.org/wiki/Happy_Eyeballs
(10) https://bugs.python.org/issue33530
File: python.info, Node: builtins, Next: collections, Prev: asyncio, Up: Improved Modules
1.1.5.3 builtins
................
The *note compile(): 1b4. built-in has been improved to accept the
‘ast.PyCF_ALLOW_TOP_LEVEL_AWAIT’ flag. With this new flag passed, *note
compile(): 1b4. will allow top-level ‘await’, ‘async for’ and ‘async
with’ constructs that are usually considered invalid syntax.
Asynchronous code object marked with the ‘CO_COROUTINE’ flag may then be
returned. (Contributed by Matthias Bussonnier in bpo-34616(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue34616
File: python.info, Node: collections, Next: cProfile, Prev: builtins, Up: Improved Modules
1.1.5.4 collections
...................
The *note _asdict(): 1b6. method for *note collections.namedtuple():
1b7. now returns a *note dict: 1b8. instead of a *note
collections.OrderedDict: 1b9. This works because regular dicts have
guaranteed ordering since Python 3.7. If the extra features of
‘OrderedDict’ are required, the suggested remediation is to cast the
result to the desired type: ‘OrderedDict(nt._asdict())’. (Contributed
by Raymond Hettinger in bpo-35864(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35864
File: python.info, Node: cProfile, Next: csv, Prev: collections, Up: Improved Modules
1.1.5.5 cProfile
................
The *note cProfile.Profile: 1bb. class can now be used as a context
manager. Profile a block of code by running:
import cProfile
with cProfile.Profile() as profiler:
# code to be profiled
...
(Contributed by Scott Sanderson in bpo-29235(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue29235
File: python.info, Node: csv, Next: curses, Prev: cProfile, Up: Improved Modules
1.1.5.6 csv
...........
The *note csv.DictReader: 1bd. now returns instances of *note dict: 1b8.
instead of a *note collections.OrderedDict: 1b9. The tool is now faster
and uses less memory while still preserving the field order.
(Contributed by Michael Selik in bpo-34003(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34003
File: python.info, Node: curses, Next: ctypes, Prev: csv, Up: Improved Modules
1.1.5.7 curses
..............
Added a new variable holding structured version information for the
underlying ncurses library: *note ncurses_version: 1bf. (Contributed by
Serhiy Storchaka in bpo-31680(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31680
File: python.info, Node: ctypes, Next: datetime, Prev: curses, Up: Improved Modules
1.1.5.8 ctypes
..............
On Windows, *note CDLL: 1c1. and subclasses now accept a `winmode'
parameter to specify flags for the underlying ‘LoadLibraryEx’ call. The
default flags are set to only load DLL dependencies from trusted
locations, including the path where the DLL is stored (if a full or
partial path is used to load the initial DLL) and paths added by *note
add_dll_directory(): 1c2. (Contributed by Steve Dower in bpo-36085(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36085
File: python.info, Node: datetime, Next: functools, Prev: ctypes, Up: Improved Modules
1.1.5.9 datetime
................
Added new alternate constructors *note datetime.date.fromisocalendar():
1c4. and *note datetime.datetime.fromisocalendar(): 1c5, which construct
‘date’ and *note datetime: 31. objects respectively from ISO year, week
number, and weekday; these are the inverse of each class’s ‘isocalendar’
method. (Contributed by Paul Ganssle in bpo-36004(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36004
File: python.info, Node: functools, Next: gc, Prev: datetime, Up: Improved Modules
1.1.5.10 functools
..................
*note functools.lru_cache(): 1c7. can now be used as a straight
decorator rather than as a function returning a decorator. So both of
these are now supported:
@lru_cache
def f(x):
...
@lru_cache(maxsize=256)
def f(x):
...
(Contributed by Raymond Hettinger in bpo-36772(1).)
Added a new *note functools.cached_property(): 1c8. decorator, for
computed properties cached for the life of the instance.
import functools
import statistics
class Dataset:
def __init__(self, sequence_of_numbers):
self.data = sequence_of_numbers
@functools.cached_property
def variance(self):
return statistics.variance(self.data)
(Contributed by Carl Meyer in bpo-21145(2))
Added a new *note functools.singledispatchmethod(): 1c9. decorator that
converts methods into *note generic functions: 1ca. using *note single
dispatch: 1cb.:
from functools import singledispatchmethod
from contextlib import suppress
class TaskManager:
def __init__(self, tasks):
self.tasks = list(tasks)
@singledispatchmethod
def discard(self, value):
with suppress(ValueError):
self.tasks.remove(value)
@discard.register(list)
def _(self, tasks):
targets = set(tasks)
self.tasks = [x for x in self.tasks if x not in targets]
(Contributed by Ethan Smith in bpo-32380(3))
---------- Footnotes ----------
(1) https://bugs.python.org/issue36772
(2) https://bugs.python.org/issue21145
(3) https://bugs.python.org/issue32380
File: python.info, Node: gc, Next: gettext, Prev: functools, Up: Improved Modules
1.1.5.11 gc
...........
*note get_objects(): 1cd. can now receive an optional `generation'
parameter indicating a generation to get objects from. (Contributed by
Pablo Galindo in bpo-36016(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36016
File: python.info, Node: gettext, Next: gzip, Prev: gc, Up: Improved Modules
1.1.5.12 gettext
................
Added *note pgettext(): 1cf. and its variants. (Contributed by Franz
Glasner, Éric Araujo, and Cheryl Sabella in bpo-2504(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue2504
File: python.info, Node: gzip, Next: IDLE and idlelib, Prev: gettext, Up: Improved Modules
1.1.5.13 gzip
.............
Added the `mtime' parameter to *note gzip.compress(): 1d1. for
reproducible output. (Contributed by Guo Ci Teo in bpo-34898(1).)
A *note BadGzipFile: 1d2. exception is now raised instead of *note
OSError: 1d3. for certain types of invalid or corrupt gzip files.
(Contributed by Filip Gruszczyński, Michele Orrù, and Zackery Spytz in
bpo-6584(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34898
(2) https://bugs.python.org/issue6584
File: python.info, Node: IDLE and idlelib, Next: inspect, Prev: gzip, Up: Improved Modules
1.1.5.14 IDLE and idlelib
.........................
Output over N lines (50 by default) is squeezed down to a button. N can
be changed in the PyShell section of the General page of the Settings
dialog. Fewer, but possibly extra long, lines can be squeezed by right
clicking on the output. Squeezed output can be expanded in place by
double-clicking the button or into the clipboard or a separate window by
right-clicking the button. (Contributed by Tal Einat in
bpo-1529353(1).)
Add “Run Customized” to the Run menu to run a module with customized
settings. Any command line arguments entered are added to sys.argv.
They also re-appear in the box for the next customized run. One can
also suppress the normal Shell main module restart. (Contributed by
Cheryl Sabella, Terry Jan Reedy, and others in bpo-5680(2) and
bpo-37627(3).)
Added optional line numbers for IDLE editor windows. Windows open
without line numbers unless set otherwise in the General tab of the
configuration dialog. Line numbers for an existing window are shown and
hidden in the Options menu. (Contributed by Tal Einat and Saimadhav
Heblikar in bpo-17535(4).)
OS native encoding is now used for converting between Python strings and
Tcl objects. This allows IDLE to work with emoji and other non-BMP
characters. These characters can be displayed or copied and pasted to
or from the clipboard. Converting strings from Tcl to Python and back
now never fails. (Many people worked on this for eight years but the
problem was finally solved by Serhiy Storchaka in bpo-13153(5).)
New in 3.8.1:
Add option to toggle cursor blink off. (Contributed by Zackery Spytz in
bpo-4603(6).)
Escape key now closes IDLE completion windows. (Contributed by Johnny
Najera in bpo-38944(7).)
The changes above have been backported to 3.7 maintenance releases.
Add keywords to module name completion list. (Contributed by Terry J.
Reedy in bpo-37765(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1529353
(2) https://bugs.python.org/issue5680
(3) https://bugs.python.org/issue37627
(4) https://bugs.python.org/issue17535
(5) https://bugs.python.org/issue13153
(6) https://bugs.python.org/issue4603
(7) https://bugs.python.org/issue38944
(8) https://bugs.python.org/issue37765
File: python.info, Node: inspect, Next: io, Prev: IDLE and idlelib, Up: Improved Modules
1.1.5.15 inspect
................
The *note inspect.getdoc(): 1d6. function can now find docstrings for
‘__slots__’ if that attribute is a *note dict: 1b8. where the values are
docstrings. This provides documentation options similar to what we
already have for *note property(): 1d7, *note classmethod(): 1d8, and
*note staticmethod(): 1d9.:
class AudioClip:
__slots__ = {'bit_rate': 'expressed in kilohertz to one decimal place',
'duration': 'in seconds, rounded up to an integer'}
def __init__(self, bit_rate, duration):
self.bit_rate = round(bit_rate / 1000.0, 1)
self.duration = ceil(duration)
(Contributed by Raymond Hettinger in bpo-36326(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36326
File: python.info, Node: io, Next: itertools, Prev: inspect, Up: Improved Modules
1.1.5.16 io
...........
In development mode (*note -X: 155. ‘env’) and in debug build, the *note
io.IOBase: 1db. finalizer now logs the exception if the ‘close()’ method
fails. The exception is ignored silently by default in release build.
(Contributed by Victor Stinner in bpo-18748(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18748
File: python.info, Node: itertools, Next: json tool, Prev: io, Up: Improved Modules
1.1.5.17 itertools
..................
The *note itertools.accumulate(): 1dd. function added an option
`initial' keyword argument to specify an initial value:
>>> from itertools import accumulate
>>> list(accumulate([10, 5, 30, 15], initial=1000))
[1000, 1010, 1015, 1045, 1060]
(Contributed by Lisa Roach in bpo-34659(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34659
File: python.info, Node: json tool, Next: logging, Prev: itertools, Up: Improved Modules
1.1.5.18 json.tool
..................
Add option ‘--json-lines’ to parse every input line as a separate JSON
object. (Contributed by Weipeng Hong in bpo-31553(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31553
File: python.info, Node: logging, Next: math, Prev: json tool, Up: Improved Modules
1.1.5.19 logging
................
Added a `force' keyword argument to *note logging.basicConfig(): 1e0.
When set to true, any existing handlers attached to the root logger are
removed and closed before carrying out the configuration specified by
the other arguments.
This solves a long-standing problem. Once a logger or `basicConfig()'
had been called, subsequent calls to `basicConfig()' were silently
ignored. This made it difficult to update, experiment with, or teach
the various logging configuration options using the interactive prompt
or a Jupyter notebook.
(Suggested by Raymond Hettinger, implemented by Dong-hee Na, and
reviewed by Vinay Sajip in bpo-33897(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33897
File: python.info, Node: math, Next: mmap, Prev: logging, Up: Improved Modules
1.1.5.20 math
.............
Added new function *note math.dist(): 1e2. for computing Euclidean
distance between two points. (Contributed by Raymond Hettinger in
bpo-33089(1).)
Expanded the *note math.hypot(): 1e3. function to handle multiple
dimensions. Formerly, it only supported the 2-D case. (Contributed by
Raymond Hettinger in bpo-33089(2).)
Added new function, *note math.prod(): 1e4, as analogous function to
*note sum(): 1e5. that returns the product of a ‘start’ value (default:
1) times an iterable of numbers:
>>> prior = 0.8
>>> likelihoods = [0.625, 0.84, 0.30]
>>> math.prod(likelihoods, start=prior)
0.126
(Contributed by Pablo Galindo in bpo-35606(3).)
Added two new combinatoric functions *note math.perm(): 1e6. and *note
math.comb(): 1e7.:
>>> math.perm(10, 3) # Permutations of 10 things taken 3 at a time
720
>>> math.comb(10, 3) # Combinations of 10 things taken 3 at a time
120
(Contributed by Yash Aggarwal, Keller Fuchs, Serhiy Storchaka, and
Raymond Hettinger in bpo-37128(4), bpo-37178(5), and bpo-35431(6).)
Added a new function *note math.isqrt(): 1e8. for computing accurate
integer square roots without conversion to floating point. The new
function supports arbitrarily large integers. It is faster than
‘floor(sqrt(n))’ but slower than *note math.sqrt(): 1e9.:
>>> r = 650320427
>>> s = r ** 2
>>> isqrt(s - 1) # correct
650320426
>>> floor(sqrt(s - 1)) # incorrect
650320427
(Contributed by Mark Dickinson in bpo-36887(7).)
The function *note math.factorial(): 1ea. no longer accepts arguments
that are not int-like. (Contributed by Pablo Galindo in bpo-33083(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33089
(2) https://bugs.python.org/issue33089
(3) https://bugs.python.org/issue35606
(4) https://bugs.python.org/issue37128
(5) https://bugs.python.org/issue37178
(6) https://bugs.python.org/issue35431
(7) https://bugs.python.org/issue36887
(8) https://bugs.python.org/issue33083
File: python.info, Node: mmap, Next: multiprocessing, Prev: math, Up: Improved Modules
1.1.5.21 mmap
.............
The *note mmap.mmap: 1ec. class now has an *note madvise(): 1ed. method
to access the ‘madvise()’ system call. (Contributed by Zackery Spytz in
bpo-32941(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32941
File: python.info, Node: multiprocessing, Next: os, Prev: mmap, Up: Improved Modules
1.1.5.22 multiprocessing
........................
Added new *note multiprocessing.shared_memory: bc. module. (Contributed
by Davin Potts in bpo-35813(1).)
On macOS, the `spawn' start method is now used by default. (Contributed
by Victor Stinner in bpo-33725(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35813
(2) https://bugs.python.org/issue33725
File: python.info, Node: os, Next: os path, Prev: multiprocessing, Up: Improved Modules
1.1.5.23 os
...........
Added new function *note add_dll_directory(): 1c2. on Windows for
providing additional search paths for native dependencies when importing
extension modules or loading DLLs using *note ctypes: 2b. (Contributed
by Steve Dower in bpo-36085(1).)
A new *note os.memfd_create(): 1f0. function was added to wrap the
‘memfd_create()’ syscall. (Contributed by Zackery Spytz and Christian
Heimes in bpo-26836(2).)
On Windows, much of the manual logic for handling reparse points
(including symlinks and directory junctions) has been delegated to the
operating system. Specifically, *note os.stat(): 1f1. will now traverse
anything supported by the operating system, while *note os.lstat(): 1f2.
will only open reparse points that identify as “name surrogates” while
others are opened as for *note os.stat(): 1f1. In all cases,
‘stat_result.st_mode’ will only have ‘S_IFLNK’ set for symbolic links
and not other kinds of reparse points. To identify other kinds of
reparse point, check the new ‘stat_result.st_reparse_tag’ attribute.
On Windows, *note os.readlink(): 1f3. is now able to read directory
junctions. Note that *note islink(): 1f4. will return ‘False’ for
directory junctions, and so code that checks ‘islink’ first will
continue to treat junctions as directories, while code that handles
errors from *note os.readlink(): 1f3. may now treat junctions as links.
(Contributed by Steve Dower in bpo-37834(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36085
(2) https://bugs.python.org/issue26836
(3) https://bugs.python.org/issue37834
File: python.info, Node: os path, Next: pathlib, Prev: os, Up: Improved Modules
1.1.5.24 os.path
................
*note os.path: c5. functions that return a boolean result like *note
exists(): 1f6, *note lexists(): 1f7, *note isdir(): 1f8, *note isfile():
1f9, *note islink(): 1f4, and *note ismount(): 1fa. now return ‘False’
instead of raising *note ValueError: 1fb. or its subclasses *note
UnicodeEncodeError: 1fc. and *note UnicodeDecodeError: 1fd. for paths
that contain characters or bytes unrepresentable at the OS level.
(Contributed by Serhiy Storchaka in bpo-33721(1).)
*note expanduser(): 1fe. on Windows now prefers the ‘USERPROFILE’
environment variable and does not use ‘HOME’, which is not normally set
for regular user accounts. (Contributed by Anthony Sottile in
bpo-36264(2).)
*note isdir(): 1f8. on Windows no longer returns ‘True’ for a link to a
non-existent directory.
*note realpath(): 1ff. on Windows now resolves reparse points, including
symlinks and directory junctions.
(Contributed by Steve Dower in bpo-37834(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33721
(2) https://bugs.python.org/issue36264
(3) https://bugs.python.org/issue37834
File: python.info, Node: pathlib, Next: pickle, Prev: os path, Up: Improved Modules
1.1.5.25 pathlib
................
*note pathlib.Path: 201. methods that return a boolean result like *note
exists(): 202, *note is_dir(): 203, *note is_file(): 204, *note
is_mount(): 205, *note is_symlink(): 206, *note is_block_device(): 207,
*note is_char_device(): 208, *note is_fifo(): 209, *note is_socket():
20a. now return ‘False’ instead of raising *note ValueError: 1fb. or its
subclass *note UnicodeEncodeError: 1fc. for paths that contain
characters unrepresentable at the OS level. (Contributed by Serhiy
Storchaka in bpo-33721(1).)
Added *note pathlib.Path.link_to(): 20b. which creates a hard link
pointing to a path. (Contributed by Joannah Nanjekye in bpo-26978(2))
---------- Footnotes ----------
(1) https://bugs.python.org/issue33721
(2) https://bugs.python.org/issue26978
File: python.info, Node: pickle, Next: plistlib, Prev: pathlib, Up: Improved Modules
1.1.5.26 pickle
...............
*note pickle: ca. extensions subclassing the C-optimized *note Pickler:
20d. can now override the pickling logic of functions and classes by
defining the special *note reducer_override(): 20e. method.
(Contributed by Pierre Glaser and Olivier Grisel in bpo-35900(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35900
File: python.info, Node: plistlib, Next: pprint, Prev: pickle, Up: Improved Modules
1.1.5.27 plistlib
.................
Added new *note plistlib.UID: 210. and enabled support for reading and
writing NSKeyedArchiver-encoded binary plists. (Contributed by Jon
Janzen in bpo-26707(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26707
File: python.info, Node: pprint, Next: py_compile, Prev: plistlib, Up: Improved Modules
1.1.5.28 pprint
...............
The *note pprint: d2. module added a `sort_dicts' parameter to several
functions. By default, those functions continue to sort dictionaries
before rendering or printing. However, if `sort_dicts' is set to false,
the dictionaries retain the order that keys were inserted. This can be
useful for comparison to JSON inputs during debugging.
In addition, there is a convenience new function, *note pprint.pp():
212. that is like *note pprint.pprint(): 213. but with `sort_dicts'
defaulting to ‘False’:
>>> from pprint import pprint, pp
>>> d = dict(source='input.txt', operation='filter', destination='output.txt')
>>> pp(d, width=40) # Original order
{'source': 'input.txt',
'operation': 'filter',
'destination': 'output.txt'}
>>> pprint(d, width=40) # Keys sorted alphabetically
{'destination': 'output.txt',
'operation': 'filter',
'source': 'input.txt'}
(Contributed by Rémi Lapeyre in bpo-30670(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30670
File: python.info, Node: py_compile, Next: shlex, Prev: pprint, Up: Improved Modules
1.1.5.29 py_compile
...................
*note py_compile.compile(): 215. now supports silent mode. (Contributed
by Joannah Nanjekye in bpo-22640(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22640
File: python.info, Node: shlex, Next: shutil, Prev: py_compile, Up: Improved Modules
1.1.5.30 shlex
..............
The new *note shlex.join(): 217. function acts as the inverse of *note
shlex.split(): 218. (Contributed by Bo Bayles in bpo-32102(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32102
File: python.info, Node: shutil, Next: socket, Prev: shlex, Up: Improved Modules
1.1.5.31 shutil
...............
*note shutil.copytree(): 21a. now accepts a new ‘dirs_exist_ok’ keyword
argument. (Contributed by Josh Bronson in bpo-20849(1).)
*note shutil.make_archive(): 21b. now defaults to the modern pax
(POSIX.1-2001) format for new archives to improve portability and
standards conformance, inherited from the corresponding change to the
*note tarfile: 101. module. (Contributed by C.A.M. Gerlach in
bpo-30661(2).)
*note shutil.rmtree(): 21c. on Windows now removes directory junctions
without recursively removing their contents first. (Contributed by
Steve Dower in bpo-37834(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20849
(2) https://bugs.python.org/issue30661
(3) https://bugs.python.org/issue37834
File: python.info, Node: socket, Next: ssl, Prev: shutil, Up: Improved Modules
1.1.5.32 socket
...............
Added *note create_server(): 21e. and *note has_dualstack_ipv6(): 21f.
convenience functions to automate the necessary tasks usually involved
when creating a server socket, including accepting both IPv4 and IPv6
connections on the same socket. (Contributed by Giampaolo Rodolà in
bpo-17561(1).)
The *note socket.if_nameindex(): 220, *note socket.if_nametoindex():
221, and *note socket.if_indextoname(): 222. functions have been
implemented on Windows. (Contributed by Zackery Spytz in bpo-37007(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17561
(2) https://bugs.python.org/issue37007
File: python.info, Node: ssl, Next: statistics, Prev: socket, Up: Improved Modules
1.1.5.33 ssl
............
Added *note post_handshake_auth: 224. to enable and *note
verify_client_post_handshake(): 225. to initiate TLS 1.3 post-handshake
authentication. (Contributed by Christian Heimes in bpo-34670(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34670
File: python.info, Node: statistics, Next: sys, Prev: ssl, Up: Improved Modules
1.1.5.34 statistics
...................
Added *note statistics.fmean(): 227. as a faster, floating point variant
of *note statistics.mean(): 199. (Contributed by Raymond Hettinger and
Steven D’Aprano in bpo-35904(1).)
Added *note statistics.geometric_mean(): 228. (Contributed by Raymond
Hettinger in bpo-27181(2).)
Added *note statistics.multimode(): 229. that returns a list of the most
common values. (Contributed by Raymond Hettinger in bpo-35892(3).)
Added *note statistics.quantiles(): 22a. that divides data or a
distribution in to equiprobable intervals (e.g. quartiles, deciles, or
percentiles). (Contributed by Raymond Hettinger in bpo-36546(4).)
Added *note statistics.NormalDist: 22b, a tool for creating and
manipulating normal distributions of a random variable. (Contributed by
Raymond Hettinger in bpo-36018(5).)
>>> temperature_feb = NormalDist.from_samples([4, 12, -3, 2, 7, 14])
>>> temperature_feb.mean
6.0
>>> temperature_feb.stdev
6.356099432828281
>>> temperature_feb.cdf(3) # Chance of being under 3 degrees
0.3184678262814532
>>> # Relative chance of being 7 degrees versus 10 degrees
>>> temperature_feb.pdf(7) / temperature_feb.pdf(10)
1.2039930378537762
>>> el_niño = NormalDist(4, 2.5)
>>> temperature_feb += el_niño # Add in a climate effect
>>> temperature_feb
NormalDist(mu=10.0, sigma=6.830080526611674)
>>> temperature_feb * (9/5) + 32 # Convert to Fahrenheit
NormalDist(mu=50.0, sigma=12.294144947901014)
>>> temperature_feb.samples(3) # Generate random samples
[7.672102882379219, 12.000027119750287, 4.647488369766392]
---------- Footnotes ----------
(1) https://bugs.python.org/issue35904
(2) https://bugs.python.org/issue27181
(3) https://bugs.python.org/issue35892
(4) https://bugs.python.org/issue36546
(5) https://bugs.python.org/issue36018
File: python.info, Node: sys, Next: tarfile, Prev: statistics, Up: Improved Modules
1.1.5.35 sys
............
Add new *note sys.unraisablehook(): 22d. function which can be
overridden to control how “unraisable exceptions” are handled. It is
called when an exception has occurred but there is no way for Python to
handle it. For example, when a destructor raises an exception or during
garbage collection (*note gc.collect(): 22e.). (Contributed by Victor
Stinner in bpo-36829(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36829
File: python.info, Node: tarfile, Next: threading, Prev: sys, Up: Improved Modules
1.1.5.36 tarfile
................
The *note tarfile: 101. module now defaults to the modern pax
(POSIX.1-2001) format for new archives, instead of the previous
GNU-specific one. This improves cross-platform portability with a
consistent encoding (UTF-8) in a standardized and extensible format, and
offers several other benefits. (Contributed by C.A.M. Gerlach in
bpo-36268(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36268
File: python.info, Node: threading, Next: tokenize, Prev: tarfile, Up: Improved Modules
1.1.5.37 threading
..................
Add a new *note threading.excepthook(): 231. function which handles
uncaught *note threading.Thread.run(): 232. exception. It can be
overridden to control how uncaught *note threading.Thread.run(): 232.
exceptions are handled. (Contributed by Victor Stinner in
bpo-1230540(1).)
Add a new *note threading.get_native_id(): 233. function and a *note
native_id: 234. attribute to the *note threading.Thread: 235. class.
These return the native integral Thread ID of the current thread
assigned by the kernel. This feature is only available on certain
platforms, see *note get_native_id: 233. for more information.
(Contributed by Jake Tesler in bpo-36084(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1230540
(2) https://bugs.python.org/issue36084
File: python.info, Node: tokenize, Next: tkinter, Prev: threading, Up: Improved Modules
1.1.5.38 tokenize
.................
The *note tokenize: 111. module now implicitly emits a ‘NEWLINE’ token
when provided with input that does not have a trailing new line. This
behavior now matches what the C tokenizer does internally. (Contributed
by Ammar Askar in bpo-33899(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33899
File: python.info, Node: tkinter, Next: time, Prev: tokenize, Up: Improved Modules
1.1.5.39 tkinter
................
Added methods ‘selection_from()’, ‘selection_present()’,
‘selection_range()’ and ‘selection_to()’ in the ‘tkinter.Spinbox’ class.
(Contributed by Juliette Monsel in bpo-34829(1).)
Added method ‘moveto()’ in the ‘tkinter.Canvas’ class. (Contributed by
Juliette Monsel in bpo-23831(2).)
The ‘tkinter.PhotoImage’ class now has ‘transparency_get()’ and
‘transparency_set()’ methods. (Contributed by Zackery Spytz in
bpo-25451(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34829
(2) https://bugs.python.org/issue23831
(3) https://bugs.python.org/issue25451
File: python.info, Node: time, Next: typing, Prev: tkinter, Up: Improved Modules
1.1.5.40 time
.............
Added new clock *note CLOCK_UPTIME_RAW: 239. for macOS 10.12.
(Contributed by Joannah Nanjekye in bpo-35702(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35702
File: python.info, Node: typing, Next: unicodedata, Prev: time, Up: Improved Modules
1.1.5.41 typing
...............
The *note typing: 119. module incorporates several new features:
* A dictionary type with per-key types. See PEP 589(1) and *note
typing.TypedDict: 23b. TypedDict uses only string keys. By
default, every key is required to be present. Specify
“total=False” to allow keys to be optional:
class Location(TypedDict, total=False):
lat_long: tuple
grid_square: str
xy_coordinate: tuple
* Literal types. See PEP 586(2) and *note typing.Literal: 23c.
Literal types indicate that a parameter or return value is
constrained to one or more specific literal values:
def get_status(port: int) -> Literal['connected', 'disconnected']:
...
* “Final” variables, functions, methods and classes. See PEP 591(3),
*note typing.Final: 23d. and *note typing.final(): 23e. The final
qualifier instructs a static type checker to restrict subclassing,
overriding, or reassignment:
pi: Final[float] = 3.1415926536
* Protocol definitions. See PEP 544(4), *note typing.Protocol: 23f.
and *note typing.runtime_checkable(): 240. Simple ABCs like *note
typing.SupportsInt: 241. are now ‘Protocol’ subclasses.
* New protocol class *note typing.SupportsIndex: 242.
* New functions *note typing.get_origin(): 243. and *note
typing.get_args(): 244.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0589
(2) https://www.python.org/dev/peps/pep-0586
(3) https://www.python.org/dev/peps/pep-0591
(4) https://www.python.org/dev/peps/pep-0544
File: python.info, Node: unicodedata, Next: unittest, Prev: typing, Up: Improved Modules
1.1.5.42 unicodedata
....................
The *note unicodedata: 11a. module has been upgraded to use the Unicode
12.1.0(1) release.
New function *note is_normalized(): 246. can be used to verify a string
is in a specific normal form, often much faster than by actually
normalizing the string. (Contributed by Max Belanger, David Euresti,
and Greg Price in bpo-32285(2) and bpo-37966(3)).
---------- Footnotes ----------
(1) http://blog.unicode.org/2019/05/unicode-12-1-en.html
(2) https://bugs.python.org/issue32285
(3) https://bugs.python.org/issue37966
File: python.info, Node: unittest, Next: venv, Prev: unicodedata, Up: Improved Modules
1.1.5.43 unittest
.................
Added *note AsyncMock: 248. to support an asynchronous version of *note
Mock: 249. Appropriate new assert functions for testing have been added
as well. (Contributed by Lisa Roach in bpo-26467(1)).
Added *note addModuleCleanup(): 24a. and *note addClassCleanup(): 24b.
to unittest to support cleanups for ‘setUpModule()’ and *note
setUpClass(): 24c. (Contributed by Lisa Roach in bpo-24412(2).)
Several mock assert functions now also print a list of actual calls upon
failure. (Contributed by Petter Strandmark in bpo-35047(3).)
*note unittest: 11b. module gained support for coroutines to be used as
test cases with *note unittest.IsolatedAsyncioTestCase: 24d.
(Contributed by Andrew Svetlov in bpo-32972(4).)
Example:
import unittest
class TestRequest(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.connection = await AsyncConnection()
async def test_get(self):
response = await self.connection.get("https://example.com")
self.assertEqual(response.status_code, 200)
async def asyncTearDown(self):
await self.connection.close()
if __name__ == "__main__":
unittest.main()
---------- Footnotes ----------
(1) https://bugs.python.org/issue26467
(2) https://bugs.python.org/issue24412
(3) https://bugs.python.org/issue35047
(4) https://bugs.python.org/issue32972
File: python.info, Node: venv, Next: weakref, Prev: unittest, Up: Improved Modules
1.1.5.44 venv
.............
*note venv: 125. now includes an ‘Activate.ps1’ script on all platforms
for activating virtual environments under PowerShell Core 6.1.
(Contributed by Brett Cannon in bpo-32718(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32718
File: python.info, Node: weakref, Next: xml, Prev: venv, Up: Improved Modules
1.1.5.45 weakref
................
The proxy objects returned by *note weakref.proxy(): 250. now support
the matrix multiplication operators ‘@’ and ‘@=’ in addition to the
other numeric operators. (Contributed by Mark Dickinson in
bpo-36669(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36669
File: python.info, Node: xml, Next: xmlrpc, Prev: weakref, Up: Improved Modules
1.1.5.46 xml
............
As mitigation against DTD and external entity retrieval, the *note
xml.dom.minidom: 135. and *note xml.sax: 13b. modules no longer process
external entities by default. (Contributed by Christian Heimes in
bpo-17239(1).)
The ‘.find*()’ methods in the *note xml.etree.ElementTree: 137. module
support wildcard searches like ‘{*}tag’ which ignores the namespace and
‘{namespace}*’ which returns all tags in the given namespace.
(Contributed by Stefan Behnel in bpo-28238(2).)
The *note xml.etree.ElementTree: 137. module provides a new function
‘–xml.etree.ElementTree.canonicalize()’ that implements C14N 2.0.
(Contributed by Stefan Behnel in bpo-13611(3).)
The target object of *note xml.etree.ElementTree.XMLParser: 252. can
receive namespace declaration events through the new callback methods
‘start_ns()’ and ‘end_ns()’. Additionally, the *note
xml.etree.ElementTree.TreeBuilder: 253. target can be configured to
process events about comments and processing instructions to include
them in the generated tree. (Contributed by Stefan Behnel in
bpo-36676(4) and bpo-36673(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17239
(2) https://bugs.python.org/issue28238
(3) https://bugs.python.org/issue13611
(4) https://bugs.python.org/issue36676
(5) https://bugs.python.org/issue36673
File: python.info, Node: xmlrpc, Prev: xml, Up: Improved Modules
1.1.5.47 xmlrpc
...............
*note xmlrpc.client.ServerProxy: 255. now supports an optional `headers'
keyword argument for a sequence of HTTP headers to be sent with each
request. Among other things, this makes it possible to upgrade from
default basic authentication to faster session authentication.
(Contributed by Cédric Krier in bpo-35153(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35153
File: python.info, Node: Optimizations, Next: Build and C API Changes, Prev: Improved Modules, Up: What’s New In Python 3 8
1.1.6 Optimizations
-------------------
* The *note subprocess: f9. module can now use the *note
os.posix_spawn(): 257. function in some cases for better
performance. Currently, it is only used on macOS and Linux (using
glibc 2.24 or newer) if all these conditions are met:
* `close_fds' is false;
* `preexec_fn', `pass_fds', `cwd' and `start_new_session'
parameters are not set;
* the `executable' path contains a directory.
(Contributed by Joannah Nanjekye and Victor Stinner in
bpo-35537(1).)
* *note shutil.copyfile(): 258, *note shutil.copy(): 259, *note
shutil.copy2(): 25a, *note shutil.copytree(): 21a. and *note
shutil.move(): 25b. use platform-specific “fast-copy” syscalls on
Linux and macOS in order to copy the file more efficiently.
“fast-copy” means that the copying operation occurs within the
kernel, avoiding the use of userspace buffers in Python as in
“‘outfd.write(infd.read())’”. On Windows *note shutil.copyfile():
258. uses a bigger default buffer size (1 MiB instead of 16 KiB)
and a *note memoryview(): 25c.-based variant of *note
shutil.copyfileobj(): 25d. is used. The speedup for copying a 512
MiB file within the same partition is about +26% on Linux, +50% on
macOS and +40% on Windows. Also, much less CPU cycles are
consumed. See *note Platform-dependent efficient copy operations:
25e. section. (Contributed by Giampaolo Rodolà in bpo-33671(2).)
* *note shutil.copytree(): 21a. uses *note os.scandir(): 25f.
function and all copy functions depending from it use cached *note
os.stat(): 1f1. values. The speedup for copying a directory with
8000 files is around +9% on Linux, +20% on Windows and +30% on a
Windows SMB share. Also the number of *note os.stat(): 1f1.
syscalls is reduced by 38% making *note shutil.copytree(): 21a.
especially faster on network filesystems. (Contributed by
Giampaolo Rodolà in bpo-33695(3).)
* The default protocol in the *note pickle: ca. module is now
Protocol 4, first introduced in Python 3.4. It offers better
performance and smaller size compared to Protocol 3 available since
Python 3.0.
* Removed one ‘Py_ssize_t’ member from ‘PyGC_Head’. All GC tracked
objects (e.g. tuple, list, dict) size is reduced 4 or 8 bytes.
(Contributed by Inada Naoki in bpo-33597(4).)
* *note uuid.UUID: 260. now uses ‘__slots__’ to reduce its memory
footprint. (Contributed by Wouter Bolsterlee and Tal Einat in
bpo-30977(5))
* Improved performance of *note operator.itemgetter(): 261. by 33%.
Optimized argument handling and added a fast path for the common
case of a single non-negative integer index into a tuple (which is
the typical use case in the standard library). (Contributed by
Raymond Hettinger in bpo-35664(6).)
* Sped-up field lookups in *note collections.namedtuple(): 1b7. They
are now more than two times faster, making them the fastest form of
instance variable lookup in Python. (Contributed by Raymond
Hettinger, Pablo Galindo, and Joe Jevnik, Serhiy Storchaka in
bpo-32492(7).)
* The *note list: 262. constructor does not overallocate the internal
item buffer if the input iterable has a known length (the input
implements ‘__len__’). This makes the created list 12% smaller on
average. (Contributed by Raymond Hettinger and Pablo Galindo in
bpo-33234(8).)
* Doubled the speed of class variable writes. When a non-dunder
attribute was updated, there was an unnecessary call to update
slots. (Contributed by Stefan Behnel, Pablo Galindo Salgado,
Raymond Hettinger, Neil Schemenauer, and Serhiy Storchaka in
bpo-36012(9).)
* Reduced an overhead of converting arguments passed to many builtin
functions and methods. This sped up calling some simple builtin
functions and methods up to 20–50%. (Contributed by Serhiy
Storchaka in bpo-23867(10), bpo-35582(11) and bpo-36127(12).)
* ‘LOAD_GLOBAL’ instruction now uses new “per opcode cache”
mechanism. It is about 40% faster now. (Contributed by Yury
Selivanov and Inada Naoki in bpo-26219(13).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35537
(2) https://bugs.python.org/issue33671
(3) https://bugs.python.org/issue33695
(4) https://bugs.python.org/issue33597
(5) https://bugs.python.org/issue30977
(6) https://bugs.python.org/issue35664
(7) https://bugs.python.org/issue32492
(8) https://bugs.python.org/issue33234
(9) https://bugs.python.org/issue36012
(10) https://bugs.python.org/issue23867
(11) https://bugs.python.org/issue35582
(12) https://bugs.python.org/issue36127
(13) https://bugs.python.org/issue26219
File: python.info, Node: Build and C API Changes, Next: Deprecated, Prev: Optimizations, Up: What’s New In Python 3 8
1.1.7 Build and C API Changes
-----------------------------
* Default *note sys.abiflags: 264. became an empty string: the ‘m’
flag for pymalloc became useless (builds with and without pymalloc
are ABI compatible) and so has been removed. (Contributed by
Victor Stinner in bpo-36707(1).)
Example of changes:
* Only ‘python3.8’ program is installed, ‘python3.8m’ program is
gone.
* Only ‘python3.8-config’ script is installed,
‘python3.8m-config’ script is gone.
* The ‘m’ flag has been removed from the suffix of dynamic
library filenames: extension modules in the standard library
as well as those produced and installed by third-party
packages, like those downloaded from PyPI. On Linux, for
example, the Python 3.7 suffix
‘.cpython-37m-x86_64-linux-gnu.so’ became
‘.cpython-38-x86_64-linux-gnu.so’ in Python 3.8.
* The header files have been reorganized to better separate the
different kinds of APIs:
* ‘Include/*.h’ should be the portable public stable C API.
* ‘Include/cpython/*.h’ should be the unstable C API specific to
CPython; public API, with some private API prefixed by ‘_Py’
or ‘_PY’.
* ‘Include/internal/*.h’ is the private internal C API very
specific to CPython. This API comes with no backward
compatibility warranty and should not be used outside CPython.
It is only exposed for very specific needs like debuggers and
profiles which has to access to CPython internals without
calling functions. This API is now installed by ‘make
install’.
(Contributed by Victor Stinner in bpo-35134(2) and bpo-35081(3),
work initiated by Eric Snow in Python 3.7.)
* Some macros have been converted to static inline functions:
parameter types and return type are well defined, they don’t have
issues specific to macros, variables have a local scopes.
Examples:
* *note Py_INCREF(): 265, *note Py_DECREF(): 266.
* *note Py_XINCREF(): 267, *note Py_XDECREF(): 268.
* ‘PyObject_INIT()’, ‘PyObject_INIT_VAR()’
* Private functions: ‘_PyObject_GC_TRACK()’,
‘_PyObject_GC_UNTRACK()’, ‘_Py_Dealloc()’
(Contributed by Victor Stinner in bpo-35059(4).)
* The ‘PyByteArray_Init()’ and ‘PyByteArray_Fini()’ functions have
been removed. They did nothing since Python 2.7.4 and Python
3.2.0, were excluded from the limited API (stable ABI), and were
not documented. (Contributed by Victor Stinner in bpo-35713(5).)
* The result of ‘PyExceptionClass_Name()’ is now of type ‘const char
*’ rather of ‘char *’. (Contributed by Serhiy Storchaka in
bpo-33818(6).)
* The duality of ‘Modules/Setup.dist’ and ‘Modules/Setup’ has been
removed. Previously, when updating the CPython source tree, one
had to manually copy ‘Modules/Setup.dist’ (inside the source tree)
to ‘Modules/Setup’ (inside the build tree) in order to reflect any
changes upstream. This was of a small benefit to packagers at the
expense of a frequent annoyance to developers following CPython
development, as forgetting to copy the file could produce build
failures.
Now the build system always reads from ‘Modules/Setup’ inside the
source tree. People who want to customize that file are encouraged
to maintain their changes in a git fork of CPython or as patch
files, as they would do for any other change to the source tree.
(Contributed by Antoine Pitrou in bpo-32430(7).)
* Functions that convert Python number to C integer like *note
PyLong_AsLong(): 269. and argument parsing functions like *note
PyArg_ParseTuple(): 26a. with integer converting format units like
‘'i'’ will now use the *note __index__(): 18a. special method
instead of *note __int__(): 18b, if available. The deprecation
warning will be emitted for objects with the ‘__int__()’ method but
without the ‘__index__()’ method (like *note Decimal: 188. and
*note Fraction: 185.). *note PyNumber_Check(): 26b. will now
return ‘1’ for objects implementing ‘__index__()’. *note
PyNumber_Long(): 26c, *note PyNumber_Float(): 26d. and *note
PyFloat_AsDouble(): 26e. also now use the ‘__index__()’ method if
available. (Contributed by Serhiy Storchaka in bpo-36048(8) and
bpo-20092(9).)
* Heap-allocated type objects will now increase their reference count
in *note PyObject_Init(): 26f. (and its parallel macro
‘PyObject_INIT’) instead of in *note PyType_GenericAlloc(): 270.
Types that modify instance allocation or deallocation may need to
be adjusted. (Contributed by Eddie Elizondo in bpo-35810(10).)
* The new function *note PyCode_NewWithPosOnlyArgs(): 271. allows to
create code objects like *note PyCode_New(): 272, but with an extra
`posonlyargcount' parameter for indicating the number of
positional-only arguments. (Contributed by Pablo Galindo in
bpo-37221(11).)
* *note Py_SetPath(): 273. now sets *note sys.executable: 274. to the
program full path (*note Py_GetProgramFullPath(): 275.) rather than
to the program name (*note Py_GetProgramName(): 276.).
(Contributed by Victor Stinner in bpo-38234(12).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36707
(2) https://bugs.python.org/issue35134
(3) https://bugs.python.org/issue35081
(4) https://bugs.python.org/issue35059
(5) https://bugs.python.org/issue35713
(6) https://bugs.python.org/issue33818
(7) https://bugs.python.org/issue32430
(8) https://bugs.python.org/issue36048
(9) https://bugs.python.org/issue20092
(10) https://bugs.python.org/issue35810
(11) https://bugs.python.org/issue37221
(12) https://bugs.python.org/issue38234
File: python.info, Node: Deprecated, Next: API and Feature Removals, Prev: Build and C API Changes, Up: What’s New In Python 3 8
1.1.8 Deprecated
----------------
* The distutils ‘bdist_wininst’ command is now deprecated, use
‘bdist_wheel’ (wheel packages) instead. (Contributed by Victor
Stinner in bpo-37481(1).)
* Deprecated methods ‘getchildren()’ and ‘getiterator()’ in the *note
ElementTree: 137. module now emit a *note DeprecationWarning: 278.
instead of *note PendingDeprecationWarning: 279. They will be
removed in Python 3.9. (Contributed by Serhiy Storchaka in
bpo-29209(2).)
* Passing an object that is not an instance of *note
concurrent.futures.ThreadPoolExecutor: 27a. to *note
loop.set_default_executor(): 27b. is deprecated and will be
prohibited in Python 3.9. (Contributed by Elvis Pranskevichus in
bpo-34075(3).)
* The *note __getitem__(): 27c. methods of *note
xml.dom.pulldom.DOMEventStream: 27d, *note
wsgiref.util.FileWrapper: 27e. and *note fileinput.FileInput: 27f.
have been deprecated.
Implementations of these methods have been ignoring their `index'
parameter, and returning the next item instead. (Contributed by
Berker Peksag in bpo-9372(4).)
* The *note typing.NamedTuple: 280. class has deprecated the
‘_field_types’ attribute in favor of the ‘__annotations__’
attribute which has the same information. (Contributed by Raymond
Hettinger in bpo-36320(5).)
* *note ast: 8. classes ‘Num’, ‘Str’, ‘Bytes’, ‘NameConstant’ and
‘Ellipsis’ are considered deprecated and will be removed in future
Python versions. ‘Constant’ should be used instead. (Contributed
by Serhiy Storchaka in bpo-32892(6).)
* *note ast.NodeVisitor: 281. methods ‘visit_Num()’, ‘visit_Str()’,
‘visit_Bytes()’, ‘visit_NameConstant()’ and ‘visit_Ellipsis()’ are
deprecated now and will not be called in future Python versions.
Add the ‘visit_Constant()’ method to handle all constant nodes.
(Contributed by Serhiy Storchaka in bpo-36917(7).)
* The *note asyncio.coroutine(): 282. *note decorator: 283. is
deprecated and will be removed in version 3.10. Instead of
‘@asyncio.coroutine’, use *note async def: 284. instead.
(Contributed by Andrew Svetlov in bpo-36921(8).)
* In *note asyncio: a, the explicit passing of a `loop' argument has
been deprecated and will be removed in version 3.10 for the
following: *note asyncio.sleep(): 285, *note asyncio.gather(): 286,
*note asyncio.shield(): 287, *note asyncio.wait_for(): 288, *note
asyncio.wait(): 289, *note asyncio.as_completed(): 28a, *note
asyncio.Task: 1ad, *note asyncio.Lock: 28b, *note asyncio.Event:
28c, *note asyncio.Condition: 28d, *note asyncio.Semaphore: 28e,
*note asyncio.BoundedSemaphore: 28f, *note asyncio.Queue: 290,
*note asyncio.create_subprocess_exec(): 291, and *note
asyncio.create_subprocess_shell(): 292.
* The explicit passing of coroutine objects to *note asyncio.wait():
289. has been deprecated and will be removed in version 3.11.
(Contributed by Yury Selivanov in bpo-34790(9).)
* The following functions and methods are deprecated in the *note
gettext: 89. module: *note lgettext(): 293, *note ldgettext(): 294,
*note lngettext(): 295. and *note ldngettext(): 296. They return
encoded bytes, and it’s possible that you will get unexpected
Unicode-related exceptions if there are encoding problems with the
translated strings. It’s much better to use alternatives which
return Unicode strings in Python 3. These functions have been
broken for a long time.
Function *note bind_textdomain_codeset(): 297, methods *note
output_charset(): 298. and *note set_output_charset(): 299, and the
`codeset' parameter of functions *note translation(): 29a. and
*note install(): 29b. are also deprecated, since they are only used
for the ‘l*gettext()’ functions. (Contributed by Serhiy Storchaka
in bpo-33710(10).)
* The ‘isAlive()’ method of *note threading.Thread: 235. has been
deprecated. (Contributed by Dong-hee Na in bpo-35283(11).)
* Many builtin and extension functions that take integer arguments
will now emit a deprecation warning for *note Decimal: 188.s, *note
Fraction: 185.s and any other objects that can be converted to
integers only with a loss (e.g. that have the *note __int__():
18b. method but do not have the *note __index__(): 18a. method).
In future version they will be errors. (Contributed by Serhiy
Storchaka in bpo-36048(12).)
* Deprecated passing the following arguments as keyword arguments:
- `func' in *note functools.partialmethod(): 29c, *note
weakref.finalize(): 29d, *note profile.Profile.runcall(): 29e,
‘cProfile.Profile.runcall()’, *note bdb.Bdb.runcall(): 29f,
*note trace.Trace.runfunc(): 2a0. and *note curses.wrapper():
2a1.
- `function' in *note unittest.TestCase.addCleanup(): 2a2.
- `fn' in the *note submit(): 2a3. method of *note
concurrent.futures.ThreadPoolExecutor: 27a. and *note
concurrent.futures.ProcessPoolExecutor: 2a4.
- `callback' in *note contextlib.ExitStack.callback(): 2a5,
‘contextlib.AsyncExitStack.callback()’ and *note
contextlib.AsyncExitStack.push_async_callback(): 2a6.
- `c' and `typeid' in the ‘create()’ method of
‘multiprocessing.managers.Server’ and
‘multiprocessing.managers.SharedMemoryServer’.
- `obj' in *note weakref.finalize(): 29d.
In future releases of Python, they will be *note positional-only:
2a7. (Contributed by Serhiy Storchaka in bpo-36492(13).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue37481
(2) https://bugs.python.org/issue29209
(3) https://bugs.python.org/issue34075
(4) https://bugs.python.org/issue9372
(5) https://bugs.python.org/issue36320
(6) https://bugs.python.org/issue32892
(7) https://bugs.python.org/issue36917
(8) https://bugs.python.org/issue36921
(9) https://bugs.python.org/issue34790
(10) https://bugs.python.org/issue33710
(11) https://bugs.python.org/issue35283
(12) https://bugs.python.org/issue36048
(13) https://bugs.python.org/issue36492
File: python.info, Node: API and Feature Removals, Next: Porting to Python 3 8, Prev: Deprecated, Up: What’s New In Python 3 8
1.1.9 API and Feature Removals
------------------------------
The following features and APIs have been removed from Python 3.8:
* Starting with Python 3.3, importing ABCs from *note collections:
1e. was deprecated, and importing should be done from *note
collections.abc: 1f. Being able to import from collections was
marked for removal in 3.8, but has been delayed to 3.9. (See
bpo-36952(1).)
* The ‘macpath’ module, deprecated in Python 3.7, has been removed.
(Contributed by Victor Stinner in bpo-35471(2).)
* The function ‘platform.popen()’ has been removed, after having been
deprecated since Python 3.3: use *note os.popen(): 2a9. instead.
(Contributed by Victor Stinner in bpo-35345(3).)
* The function ‘time.clock()’ has been removed, after having been
deprecated since Python 3.3: use *note time.perf_counter(): 2aa. or
*note time.process_time(): 2ab. instead, depending on your
requirements, to have well-defined behavior. (Contributed by
Matthias Bussonnier in bpo-36895(4).)
* The ‘pyvenv’ script has been removed in favor of ‘python3.8 -m
venv’ to help eliminate confusion as to what Python interpreter the
‘pyvenv’ script is tied to. (Contributed by Brett Cannon in
bpo-25427(5).)
* ‘parse_qs’, ‘parse_qsl’, and ‘escape’ are removed from the *note
cgi: 16. module. They are deprecated in Python 3.2 or older. They
should be imported from the ‘urllib.parse’ and ‘html’ modules
instead.
* ‘filemode’ function is removed from the *note tarfile: 101. module.
It is not documented and deprecated since Python 3.3.
* The *note XMLParser: 252. constructor no longer accepts the `html'
argument. It never had an effect and was deprecated in Python 3.4.
All other parameters are now *note keyword-only: 2ac. (Contributed
by Serhiy Storchaka in bpo-29209(6).)
* Removed the ‘doctype()’ method of *note XMLParser: 252.
(Contributed by Serhiy Storchaka in bpo-29209(7).)
* “unicode_internal” codec is removed. (Contributed by Inada Naoki
in bpo-36297(8).)
* The ‘Cache’ and ‘Statement’ objects of the *note sqlite3: f2.
module are not exposed to the user. (Contributed by Aviv Palivoda
in bpo-30262(9).)
* The ‘bufsize’ keyword argument of *note fileinput.input(): 2ad. and
*note fileinput.FileInput(): 27f. which was ignored and deprecated
since Python 3.6 has been removed. bpo-36952(10) (Contributed by
Matthias Bussonnier.)
* The functions ‘sys.set_coroutine_wrapper()’ and
‘sys.get_coroutine_wrapper()’ deprecated in Python 3.7 have been
removed; bpo-36933(11) (Contributed by Matthias Bussonnier.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue36952
(2) https://bugs.python.org/issue35471
(3) https://bugs.python.org/issue35345
(4) https://bugs.python.org/issue36895
(5) https://bugs.python.org/issue25427
(6) https://bugs.python.org/issue29209
(7) https://bugs.python.org/issue29209
(8) https://bugs.python.org/issue36297
(9) https://bugs.python.org/issue30262
(10) https://bugs.python.org/issue36952
(11) https://bugs.python.org/issue36933
File: python.info, Node: Porting to Python 3 8, Next: Notable changes in Python 3 8 1, Prev: API and Feature Removals, Up: What’s New In Python 3 8
1.1.10 Porting to Python 3.8
----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code.
* Menu:
* Changes in Python behavior::
* Changes in the Python API::
* Changes in the C API::
* CPython bytecode changes::
* Demos and Tools::
File: python.info, Node: Changes in Python behavior, Next: Changes in the Python API, Up: Porting to Python 3 8
1.1.10.1 Changes in Python behavior
...................................
* Yield expressions (both ‘yield’ and ‘yield from’ clauses) are now
disallowed in comprehensions and generator expressions (aside from
the iterable expression in the leftmost ‘for’ clause).
(Contributed by Serhiy Storchaka in bpo-10544(1).)
* The compiler now produces a *note SyntaxWarning: 191. when identity
checks (‘is’ and ‘is not’) are used with certain types of literals
(e.g. strings, numbers). These can often work by accident in
CPython, but are not guaranteed by the language spec. The warning
advises users to use equality tests (‘==’ and ‘!=’) instead.
(Contributed by Serhiy Storchaka in bpo-34850(2).)
* The CPython interpreter can swallow exceptions in some
circumstances. In Python 3.8 this happens in fewer cases. In
particular, exceptions raised when getting the attribute from the
type dictionary are no longer ignored. (Contributed by Serhiy
Storchaka in bpo-35459(3).)
* Removed ‘__str__’ implementations from builtin types *note bool:
183, *note int: 184, *note float: 187, *note complex: 189. and few
classes from the standard library. They now inherit ‘__str__()’
from *note object: 2b0. As result, defining the ‘__repr__()’
method in the subclass of these classes will affect their string
representation. (Contributed by Serhiy Storchaka in bpo-36793(4).)
* On AIX, *note sys.platform: 2b1. doesn’t contain the major version
anymore. It is always ‘'aix'’, instead of ‘'aix3'’ .. ‘'aix7'’.
Since older Python versions include the version number, so it is
recommended to always use ‘sys.platform.startswith('aix')’.
(Contributed by M. Felt in bpo-36588(5).)
* *note PyEval_AcquireLock(): 2b2. and *note PyEval_AcquireThread():
2b3. now terminate the current thread if called while the
interpreter is finalizing, making them consistent with *note
PyEval_RestoreThread(): 2b4, *note Py_END_ALLOW_THREADS(): 2b5, and
*note PyGILState_Ensure(): 2b6. If this behavior is not desired,
guard the call by checking ‘_Py_IsFinalizing()’ or
‘sys.is_finalizing()’. (Contributed by Joannah Nanjekye in
bpo-36475(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10544
(2) https://bugs.python.org/issue34850
(3) https://bugs.python.org/issue35459
(4) https://bugs.python.org/issue36793
(5) https://bugs.python.org/issue36588
(6) https://bugs.python.org/issue36475
File: python.info, Node: Changes in the Python API, Next: Changes in the C API, Prev: Changes in Python behavior, Up: Porting to Python 3 8
1.1.10.2 Changes in the Python API
..................................
* The *note os.getcwdb(): 2b8. function now uses the UTF-8 encoding
on Windows, rather than the ANSI code page: see PEP 529(1) for the
rationale. The function is no longer deprecated on Windows.
(Contributed by Victor Stinner in bpo-37412(2).)
* *note subprocess.Popen: 2b9. can now use *note os.posix_spawn():
257. in some cases for better performance. On Windows Subsystem
for Linux and QEMU User Emulation, the ‘Popen’ constructor using
*note os.posix_spawn(): 257. no longer raises an exception on
errors like “missing program”. Instead the child process fails
with a non-zero ‘returncode’. (Contributed by Joannah Nanjekye and
Victor Stinner in bpo-35537(3).)
* The `preexec_fn' argument of * *note subprocess.Popen: 2b9. is no
longer compatible with subinterpreters. The use of the parameter
in a subinterpreter now raises *note RuntimeError: 2ba.
(Contributed by Eric Snow in bpo-34651(4), modified by Christian
Heimes in bpo-37951(5).)
* The ‘imap.IMAP4.logout()’ method no longer silently ignores
arbitrary exceptions. (Contributed by Victor Stinner in
bpo-36348(6).)
* The function ‘platform.popen()’ has been removed, after having been
deprecated since Python 3.3: use *note os.popen(): 2a9. instead.
(Contributed by Victor Stinner in bpo-35345(7).)
* The *note statistics.mode(): 2bb. function no longer raises an
exception when given multimodal data. Instead, it returns the
first mode encountered in the input data. (Contributed by Raymond
Hettinger in bpo-35892(8).)
* The *note selection(): 2bc. method of the *note
tkinter.ttk.Treeview: 2bd. class no longer takes arguments. Using
it with arguments for changing the selection was deprecated in
Python 3.6. Use specialized methods like *note selection_set():
2be. for changing the selection. (Contributed by Serhiy Storchaka
in bpo-31508(9).)
* The ‘writexml()’, ‘toxml()’ and ‘toprettyxml()’ methods of *note
xml.dom.minidom: 135, and the ‘write()’ method of ‘xml.etree’, now
preserve the attribute order specified by the user. (Contributed
by Diego Rojas and Raymond Hettinger in bpo-34160(10).)
* A *note dbm.dumb: 33. database opened with flags ‘'r'’ is now
read-only. *note dbm.dumb.open(): 2bf. with flags ‘'r'’ and ‘'w'’
no longer creates a database if it does not exist. (Contributed by
Serhiy Storchaka in bpo-32749(11).)
* The ‘doctype()’ method defined in a subclass of *note XMLParser:
252. will no longer be called and will emit a *note RuntimeWarning:
2c0. instead of a *note DeprecationWarning: 278. Define the *note
doctype(): 2c1. method on a target for handling an XML doctype
declaration. (Contributed by Serhiy Storchaka in bpo-29209(12).)
* A *note RuntimeError: 2ba. is now raised when the custom metaclass
doesn’t provide the ‘__classcell__’ entry in the namespace passed
to ‘type.__new__’. A *note DeprecationWarning: 278. was emitted in
Python 3.6–3.7. (Contributed by Serhiy Storchaka in
bpo-23722(13).)
* The ‘cProfile.Profile’ class can now be used as a context manager.
(Contributed by Scott Sanderson in bpo-29235(14).)
* *note shutil.copyfile(): 258, *note shutil.copy(): 259, *note
shutil.copy2(): 25a, *note shutil.copytree(): 21a. and *note
shutil.move(): 25b. use platform-specific “fast-copy” syscalls (see
*note Platform-dependent efficient copy operations: 25e. section).
* *note shutil.copyfile(): 258. default buffer size on Windows was
changed from 16 KiB to 1 MiB.
* The ‘PyGC_Head’ struct has changed completely. All code that
touched the struct member should be rewritten. (See
bpo-33597(15).)
* The *note PyInterpreterState: 2c2. struct has been moved into the
“internal” header files (specifically
Include/internal/pycore_pystate.h). An opaque ‘PyInterpreterState’
is still available as part of the public API (and stable ABI). The
docs indicate that none of the struct’s fields are public, so we
hope no one has been using them. However, if you do rely on one or
more of those private fields and have no alternative then please
open a BPO issue. We’ll work on helping you adjust (possibly
including adding accessor functions to the public API). (See
bpo-35886(16).)
* The *note mmap.flush(): 2c3. method now returns ‘None’ on success
and raises an exception on error under all platforms. Previously,
its behavior was platform-dependent: a nonzero value was returned
on success; zero was returned on error under Windows. A zero value
was returned on success; an exception was raised on error under
Unix. (Contributed by Berker Peksag in bpo-2122(17).)
* *note xml.dom.minidom: 135. and *note xml.sax: 13b. modules no
longer process external entities by default. (Contributed by
Christian Heimes in bpo-17239(18).)
* Deleting a key from a read-only *note dbm: 32. database (*note
dbm.dumb: 33, *note dbm.gnu: 34. or *note dbm.ndbm: 35.) raises
‘error’ (*note dbm.dumb.error: 2c4, *note dbm.gnu.error: 2c5. or
*note dbm.ndbm.error: 2c6.) instead of *note KeyError: 2c7.
(Contributed by Xiang Zhang in bpo-33106(19).)
* Simplified AST for literals. All constants will be represented as
‘ast.Constant’ instances. Instantiating old classes ‘Num’, ‘Str’,
‘Bytes’, ‘NameConstant’ and ‘Ellipsis’ will return an instance of
‘Constant’. (Contributed by Serhiy Storchaka in bpo-32892(20).)
* *note expanduser(): 1fe. on Windows now prefers the ‘USERPROFILE’
environment variable and does not use ‘HOME’, which is not normally
set for regular user accounts. (Contributed by Anthony Sottile in
bpo-36264(21).)
* The exception *note asyncio.CancelledError: 1a7. now inherits from
*note BaseException: 1a8. rather than *note Exception: 1a9. and no
longer inherits from *note concurrent.futures.CancelledError: 1aa.
(Contributed by Yury Selivanov in bpo-32528(22).)
* The function *note asyncio.wait_for(): 288. now correctly waits for
cancellation when using an instance of *note asyncio.Task: 1ad.
Previously, upon reaching `timeout', it was cancelled and
immediately returned. (Contributed by Elvis Pranskevichus in
bpo-32751(23).)
* The function *note asyncio.BaseTransport.get_extra_info(): 2c8. now
returns a safe to use socket object when ‘socket’ is passed to the
`name' parameter. (Contributed by Yury Selivanov in
bpo-37027(24).)
* *note asyncio.BufferedProtocol: 2c9. has graduated to the stable
API.
* DLL dependencies for extension modules and DLLs loaded with *note
ctypes: 2b. on Windows are now resolved more securely. Only the
system paths, the directory containing the DLL or PYD file, and
directories added with *note add_dll_directory(): 1c2. are searched
for load-time dependencies. Specifically, ‘PATH’ and the current
working directory are no longer used, and modifications to these
will no longer have any effect on normal DLL resolution. If your
application relies on these mechanisms, you should check for *note
add_dll_directory(): 1c2. and if it exists, use it to add your DLLs
directory while loading your library. Note that Windows 7 users
will need to ensure that Windows Update KB2533623 has been
installed (this is also verified by the installer). (Contributed
by Steve Dower in bpo-36085(25).)
* The header files and functions related to pgen have been removed
after its replacement by a pure Python implementation.
(Contributed by Pablo Galindo in bpo-36623(26).)
* *note types.CodeType: 198. has a new parameter in the second
position of the constructor (`posonlyargcount') to support
positional-only arguments defined in PEP 570(27). The first
argument (`argcount') now represents the total number of positional
arguments (including positional-only arguments). The new
‘replace()’ method of *note types.CodeType: 198. can be used to
make the code future-proof.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0529
(2) https://bugs.python.org/issue37412
(3) https://bugs.python.org/issue35537
(4) https://bugs.python.org/issue34651
(5) https://bugs.python.org/issue37951
(6) https://bugs.python.org/issue36348
(7) https://bugs.python.org/issue35345
(8) https://bugs.python.org/issue35892
(9) https://bugs.python.org/issue31508
(10) https://bugs.python.org/issue34160
(11) https://bugs.python.org/issue32749
(12) https://bugs.python.org/issue29209
(13) https://bugs.python.org/issue23722
(14) https://bugs.python.org/issue29235
(15) https://bugs.python.org/issue33597
(16) https://bugs.python.org/issue35886
(17) https://bugs.python.org/issue2122
(18) https://bugs.python.org/issue17239
(19) https://bugs.python.org/issue33106
(20) https://bugs.python.org/issue32892
(21) https://bugs.python.org/issue36264
(22) https://bugs.python.org/issue32528
(23) https://bugs.python.org/issue32751
(24) https://bugs.python.org/issue37027
(25) https://bugs.python.org/issue36085
(26) https://bugs.python.org/issue36623
(27) https://www.python.org/dev/peps/pep-0570
File: python.info, Node: Changes in the C API, Next: CPython bytecode changes, Prev: Changes in the Python API, Up: Porting to Python 3 8
1.1.10.3 Changes in the C API
.............................
* The *note PyCompilerFlags: 2cc. structure got a new
`cf_feature_version' field. It should be initialized to
‘PY_MINOR_VERSION’. The field is ignored by default, and is used
if and only if ‘PyCF_ONLY_AST’ flag is set in `cf_flags'.
(Contributed by Guido van Rossum in bpo-35766(1).)
* The ‘PyEval_ReInitThreads()’ function has been removed from the C
API. It should not be called explicitly: use *note
PyOS_AfterFork_Child(): 2cd. instead. (Contributed by Victor
Stinner in bpo-36728(2).)
* On Unix, C extensions are no longer linked to libpython except on
Android and Cygwin. When Python is embedded, ‘libpython’ must not
be loaded with ‘RTLD_LOCAL’, but ‘RTLD_GLOBAL’ instead.
Previously, using ‘RTLD_LOCAL’, it was already not possible to load
C extensions which were not linked to ‘libpython’, like C
extensions of the standard library built by the ‘*shared*’ section
of ‘Modules/Setup’. (Contributed by Victor Stinner in
bpo-21536(3).)
* Use of ‘#’ variants of formats in parsing or building value (e.g.
*note PyArg_ParseTuple(): 26a, *note Py_BuildValue(): 2ce, *note
PyObject_CallFunction(): 2cf, etc.) without ‘PY_SSIZE_T_CLEAN’
defined raises ‘DeprecationWarning’ now. It will be removed in
3.10 or 4.0. Read *note Parsing arguments and building values:
2d0. for detail. (Contributed by Inada Naoki in bpo-36381(4).)
* Instances of heap-allocated types (such as those created with *note
PyType_FromSpec(): 2d1.) hold a reference to their type object.
Increasing the reference count of these type objects has been moved
from *note PyType_GenericAlloc(): 270. to the more low-level
functions, *note PyObject_Init(): 26f. and ‘PyObject_INIT()’. This
makes types created through *note PyType_FromSpec(): 2d1. behave
like other classes in managed code.
Statically allocated types are not affected.
For the vast majority of cases, there should be no side effect.
However, types that manually increase the reference count after
allocating an instance (perhaps to work around the bug) may now
become immortal. To avoid this, these classes need to call
Py_DECREF on the type object during instance deallocation.
To correctly port these types into 3.8, please apply the following
changes:
* Remove *note Py_INCREF: 265. on the type object after
allocating an instance - if any. This may happen after
calling *note PyObject_New(): 2d2, *note PyObject_NewVar():
2d3, *note PyObject_GC_New(): 2d4, *note PyObject_GC_NewVar():
2d5, or any other custom allocator that uses *note
PyObject_Init(): 26f. or ‘PyObject_INIT()’.
Example:
static foo_struct *
foo_new(PyObject *type) {
foo_struct *foo = PyObject_GC_New(foo_struct, (PyTypeObject *) type);
if (foo == NULL)
return NULL;
#if PY_VERSION_HEX < 0x03080000
// Workaround for Python issue 35810; no longer necessary in Python 3.8
PY_INCREF(type)
#endif
return foo;
}
* Ensure that all custom ‘tp_dealloc’ functions of
heap-allocated types decrease the type’s reference count.
Example:
static void
foo_dealloc(foo_struct *instance) {
PyObject *type = Py_TYPE(instance);
PyObject_GC_Del(instance);
#if PY_VERSION_HEX >= 0x03080000
// This was not needed before Python 3.8 (Python issue 35810)
Py_DECREF(type);
#endif
}
(Contributed by Eddie Elizondo in bpo-35810(5).)
* The ‘Py_DEPRECATED()’ macro has been implemented for MSVC. The
macro now must be placed before the symbol name.
Example:
Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);
(Contributed by Zackery Spytz in bpo-33407(6).)
* The interpreter does not pretend to support binary compatibility of
extension types across feature releases, anymore. A *note
PyTypeObject: 2d6. exported by a third-party extension module is
supposed to have all the slots expected in the current Python
version, including *note tp_finalize: 2d7. (*note
Py_TPFLAGS_HAVE_FINALIZE: 2d8. is not checked anymore before
reading *note tp_finalize: 2d7.).
(Contributed by Antoine Pitrou in bpo-32388(7).)
* The functions ‘PyNode_AddChild()’ and ‘PyParser_AddToken()’ now
accept two additional ‘int’ arguments `end_lineno' and
`end_col_offset'.
* The ‘libpython38.a’ file to allow MinGW tools to link directly
against ‘python38.dll’ is no longer included in the regular Windows
distribution. If you require this file, it may be generated with
the ‘gendef’ and ‘dlltool’ tools, which are part of the MinGW
binutils package:
gendef - python38.dll > tmp.def
dlltool --dllname python38.dll --def tmp.def --output-lib libpython38.a
The location of an installed ‘pythonXY.dll’ will depend on the
installation options and the version and language of Windows. See
*note Using Python on Windows: 2d9. for more information. The
resulting library should be placed in the same directory as
‘pythonXY.lib’, which is generally the ‘libs’ directory under your
Python installation.
(Contributed by Steve Dower in bpo-37351(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue35766
(2) https://bugs.python.org/issue36728
(3) https://bugs.python.org/issue21536
(4) https://bugs.python.org/issue36381
(5) https://bugs.python.org/issue35810
(6) https://bugs.python.org/issue33407
(7) https://bugs.python.org/issue32388
(8) https://bugs.python.org/issue37351
File: python.info, Node: CPython bytecode changes, Next: Demos and Tools, Prev: Changes in the C API, Up: Porting to Python 3 8
1.1.10.4 CPython bytecode changes
.................................
* The interpreter loop has been simplified by moving the logic of
unrolling the stack of blocks into the compiler. The compiler
emits now explicit instructions for adjusting the stack of values
and calling the cleaning-up code for *note break: 2db, *note
continue: 181. and *note return: 190.
Removed opcodes ‘BREAK_LOOP’, ‘CONTINUE_LOOP’, ‘SETUP_LOOP’ and
‘SETUP_EXCEPT’. Added new opcodes *note ROT_FOUR: 2dc, *note
BEGIN_FINALLY: 2dd, *note CALL_FINALLY: 2de. and *note POP_FINALLY:
2df. Changed the behavior of *note END_FINALLY: 2e0. and *note
WITH_CLEANUP_START: 2e1.
(Contributed by Mark Shannon, Antoine Pitrou and Serhiy Storchaka
in bpo-17611(1).)
* Added new opcode *note END_ASYNC_FOR: 2e2. for handling exceptions
raised when awaiting a next item in an *note async for: 2e3. loop.
(Contributed by Serhiy Storchaka in bpo-33041(2).)
* The *note MAP_ADD: 2e4. now expects the value as the first element
in the stack and the key as the second element. This change was
made so the key is always evaluated before the value in dictionary
comprehensions, as proposed by PEP 572(3). (Contributed by Jörn
Heissler in bpo-35224(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17611
(2) https://bugs.python.org/issue33041
(3) https://www.python.org/dev/peps/pep-0572
(4) https://bugs.python.org/issue35224
File: python.info, Node: Demos and Tools, Prev: CPython bytecode changes, Up: Porting to Python 3 8
1.1.10.5 Demos and Tools
........................
Added a benchmark script for timing various ways to access variables:
‘Tools/scripts/var_access_benchmark.py’. (Contributed by Raymond
Hettinger in bpo-35884(1).)
Here’s a summary of performance improvements since Python 3.3:
Python version 3.3 3.4 3.5 3.6 3.7 3.8
-------------- --- --- --- --- --- ---
Variable and attribute read access:
read_local 4.0 7.1 7.1 5.4 5.1 3.9
read_nonlocal 5.3 7.1 8.1 5.8 5.4 4.4
read_global 13.3 15.5 19.0 14.3 13.6 7.6
read_builtin 20.0 21.1 21.6 18.5 19.0 7.5
read_classvar_from_class 20.5 25.6 26.5 20.7 19.5 18.4
read_classvar_from_instance 18.5 22.8 23.5 18.8 17.1 16.4
read_instancevar 26.8 32.4 33.1 28.0 26.3 25.4
read_instancevar_slots 23.7 27.8 31.3 20.8 20.8 20.2
read_namedtuple 68.5 73.8 57.5 45.0 46.8 18.4
read_boundmethod 29.8 37.6 37.9 29.6 26.9 27.7
Variable and attribute write access:
write_local 4.6 8.7 9.3 5.5 5.3 4.3
write_nonlocal 7.3 10.5 11.1 5.6 5.5 4.7
write_global 15.9 19.7 21.2 18.0 18.0 15.8
write_classvar 81.9 92.9 96.0 104.6 102.1 39.2
write_instancevar 36.4 44.6 45.8 40.0 38.9 35.5
write_instancevar_slots 28.7 35.6 36.1 27.3 26.6 25.7
Data structure read access:
read_list 19.2 24.2 24.5 20.8 20.8 19.0
read_deque 19.9 24.7 25.5 20.2 20.6 19.8
read_dict 19.7 24.3 25.7 22.3 23.0 21.0
read_strdict 17.9 22.6 24.3 19.5 21.2 18.9
Data structure write access:
write_list 21.2 27.1 28.5 22.5 21.6 20.0
write_deque 23.8 28.7 30.1 22.7 21.8 23.5
write_dict 25.9 31.4 33.3 29.3 29.2 24.7
write_strdict 22.9 28.4 29.9 27.5 25.2 23.1
Stack (or queue) operations:
list_append_pop 144.2 93.4 112.7 75.4 74.2 50.8
deque_append_pop 30.4 43.5 57.0 49.4 49.2 42.5
deque_append_popleft 30.8 43.7 57.3 49.7 49.7 42.8
Timing loop:
loop_overhead 0.3 0.5 0.6 0.4 0.3 0.3
The benchmarks were measured on an Intel® Core™ i7-4960HQ processor(2)
running the macOS 64-bit builds found at python.org(3). The benchmark
script displays timings in nanoseconds.
---------- Footnotes ----------
(1) https://bugs.python.org/issue35884
(2)
https://ark.intel.com/content/www/us/en/ark/products/76088/intel-core-i7-4960hq-processor-6m-cache-up-to-3-80-ghz.html
(3) https://www.python.org/downloads/mac-osx/
File: python.info, Node: Notable changes in Python 3 8 1, Next: Notable changes in Python 3 8 2, Prev: Porting to Python 3 8, Up: What’s New In Python 3 8
1.1.11 Notable changes in Python 3.8.1
--------------------------------------
Due to significant security concerns, the `reuse_address' parameter of
*note asyncio.loop.create_datagram_endpoint(): 2e7. is no longer
supported. This is because of the behavior of the socket option
‘SO_REUSEADDR’ in UDP. For more details, see the documentation for
‘loop.create_datagram_endpoint()’. (Contributed by Kyle Stanley,
Antoine Pitrou, and Yury Selivanov in bpo-37228(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue37228
File: python.info, Node: Notable changes in Python 3 8 2, Next: Notable changes in Python 3 8 3, Prev: Notable changes in Python 3 8 1, Up: What’s New In Python 3 8
1.1.12 Notable changes in Python 3.8.2
--------------------------------------
Fixed a regression with the ‘ignore’ callback of *note
shutil.copytree(): 21a. The argument types are now str and List[str]
again. (Contributed by Manuel Barkhau and Giampaolo Rodola in
bpo-39390(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue39390
File: python.info, Node: Notable changes in Python 3 8 3, Next: Notable changes in Python 3 8 8, Prev: Notable changes in Python 3 8 2, Up: What’s New In Python 3 8
1.1.13 Notable changes in Python 3.8.3
--------------------------------------
The constant values of future flags in the *note __future__: 0. module
are updated in order to prevent collision with compiler flags.
Previously ‘PyCF_ALLOW_TOP_LEVEL_AWAIT’ was clashing with
‘CO_FUTURE_DIVISION’. (Contributed by Batuhan Taskaya in bpo-39562(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue39562
File: python.info, Node: Notable changes in Python 3 8 8, Next: Notable changes in Python 3 8 9, Prev: Notable changes in Python 3 8 3, Up: What’s New In Python 3 8
1.1.14 Notable changes in Python 3.8.8
--------------------------------------
Earlier Python versions allowed using both ‘;’ and ‘&’ as query
parameter separators in *note urllib.parse.parse_qs(): 2eb. and *note
urllib.parse.parse_qsl(): 2ec. Due to security concerns, and to conform
with newer W3C recommendations, this has been changed to allow only a
single separator key, with ‘&’ as the default. This change also affects
*note cgi.parse(): 2ed. and *note cgi.parse_multipart(): 2ee. as they
use the affected functions internally. For more details, please see
their respective documentation. (Contributed by Adam Goldschmidt,
Senthil Kumaran and Ken Jin in bpo-42967(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue42967
File: python.info, Node: Notable changes in Python 3 8 9, Prev: Notable changes in Python 3 8 8, Up: What’s New In Python 3 8
1.1.15 Notable changes in Python 3.8.9
--------------------------------------
A security fix alters the *note ftplib.FTP: 2f0. behavior to not trust
the IPv4 address sent from the remote server when setting up a passive
data channel. We reuse the ftp server IP address instead. For unusual
code requiring the old behavior, set a ‘trust_server_pasv_ipv4_address’
attribute on your FTP instance to ‘True’. (See bpo-43285(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue43285
File: python.info, Node: What’s New In Python 3 7, Next: What’s New In Python 3 6, Prev: What’s New In Python 3 8, Up: What’s New in Python
1.2 What’s New In Python 3.7
============================
Editor: Elvis Pranskevichus <>
This article explains the new features in Python 3.7, compared to 3.6.
Python 3.7 was released on June 27, 2018. For full details, see the
*note changelog: 14c.
* Menu:
* Summary – Release Highlights::
* New Features: New Features<2>.
* Other Language Changes: Other Language Changes<2>.
* New Modules: New Modules<2>.
* Improved Modules: Improved Modules<2>.
* C API Changes::
* Build Changes::
* Optimizations: Optimizations<2>.
* Other CPython Implementation Changes::
* Deprecated Python Behavior::
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods.
* Deprecated functions and types of the C API::
* Platform Support Removals::
* API and Feature Removals: API and Feature Removals<2>.
* Module Removals::
* Windows-only Changes::
* Porting to Python 3.7: Porting to Python 3 7.
* Notable changes in Python 3.7.1: Notable changes in Python 3 7 1.
* Notable changes in Python 3.7.2: Notable changes in Python 3 7 2.
* Notable changes in Python 3.7.6: Notable changes in Python 3 7 6.
* Notable changes in Python 3.7.10: Notable changes in Python 3 7 10.
File: python.info, Node: Summary – Release Highlights, Next: New Features<2>, Up: What’s New In Python 3 7
1.2.1 Summary – Release Highlights
----------------------------------
New syntax features:
* *note PEP 563: 2f4, postponed evaluation of type annotations.
Backwards incompatible syntax changes:
* *note async: 1a2. and *note await: 1a3. are now reserved keywords.
New library modules:
* *note contextvars: 25.: *note PEP 567 – Context Variables: 2f5.
* *note dataclasses: 30.: *note PEP 557 – Data Classes: 2f6.
* *note importlib.resources: 2f7.
New built-in features:
* *note PEP 553: 2f8, the new *note breakpoint(): 2f9. function.
Python data model improvements:
* *note PEP 562: 2fa, customization of access to module attributes.
* *note PEP 560: 2fb, core support for typing module and generic
types.
* the insertion-order preservation nature of *note dict: 2fc. objects
has been declared(1) to be an official part of the Python language
spec.
Significant improvements in the standard library:
* The *note asyncio: a. module has received new features, significant
*note usability and performance improvements: 2fd.
* The *note time: 10a. module gained support for *note functions with
nanosecond resolution: 2fe.
CPython implementation improvements:
* Avoiding the use of ASCII as a default text encoding:
* *note PEP 538: 2ff, legacy C locale coercion
* *note PEP 540: 300, forced UTF-8 runtime mode
* *note PEP 552: 301, deterministic .pycs
* *note the new development runtime mode: 302.
* *note PEP 565: 303, improved *note DeprecationWarning: 278.
handling
C API improvements:
* *note PEP 539: 304, new C API for thread-local storage
Documentation improvements:
* *note PEP 545: 305, Python documentation translations
* New documentation translations: Japanese(2), French(3), and
Korean(4).
This release features notable performance improvements in many areas.
The *note Optimizations: 306. section lists them in detail.
For a list of changes that may affect compatibility with previous Python
releases please refer to the *note Porting to Python 3.7: 307. section.
---------- Footnotes ----------
(1)
https://mail.python.org/pipermail/python-dev/2017-December/151283.html
(2) https://docs.python.org/ja/
(3) https://docs.python.org/fr/
(4) https://docs.python.org/ko/
File: python.info, Node: New Features<2>, Next: Other Language Changes<2>, Prev: Summary – Release Highlights, Up: What’s New In Python 3 7
1.2.2 New Features
------------------
* Menu:
* PEP 563; Postponed Evaluation of Annotations: PEP 563 Postponed Evaluation of Annotations.
* PEP 538; Legacy C Locale Coercion: PEP 538 Legacy C Locale Coercion.
* PEP 540; Forced UTF-8 Runtime Mode: PEP 540 Forced UTF-8 Runtime Mode.
* PEP 553; Built-in breakpoint(): PEP 553 Built-in breakpoint.
* PEP 539; New C API for Thread-Local Storage: PEP 539 New C API for Thread-Local Storage.
* PEP 562; Customization of Access to Module Attributes: PEP 562 Customization of Access to Module Attributes.
* PEP 564; New Time Functions With Nanosecond Resolution: PEP 564 New Time Functions With Nanosecond Resolution.
* PEP 565; Show DeprecationWarning in __main__: PEP 565 Show DeprecationWarning in __main__.
* PEP 560; Core Support for typing module and Generic Types: PEP 560 Core Support for typing module and Generic Types.
* PEP 552; Hash-based .pyc Files: PEP 552 Hash-based pyc Files.
* PEP 545; Python Documentation Translations: PEP 545 Python Documentation Translations.
* Development Runtime Mode; -X dev: Development Runtime Mode -X dev.
File: python.info, Node: PEP 563 Postponed Evaluation of Annotations, Next: PEP 538 Legacy C Locale Coercion, Up: New Features<2>
1.2.2.1 PEP 563: Postponed Evaluation of Annotations
....................................................
The advent of type hints in Python uncovered two glaring usability
issues with the functionality of annotations added in PEP 3107(1) and
refined further in PEP 526(2):
* annotations could only use names which were already available in
the current scope, in other words they didn’t support forward
references of any kind; and
* annotating source code had adverse effects on startup time of
Python programs.
Both of these issues are fixed by postponing the evaluation of
annotations. Instead of compiling code which executes expressions in
annotations at their definition time, the compiler stores the annotation
in a string form equivalent to the AST of the expression in question.
If needed, annotations can be resolved at runtime using *note
typing.get_type_hints(): 30a. In the common case where this is not
required, the annotations are cheaper to store (since short strings are
interned by the interpreter) and make startup time faster.
Usability-wise, annotations now support forward references, making the
following syntax valid:
class C:
@classmethod
def from_string(cls, source: str) -> C:
...
def validate_b(self, obj: B) -> bool:
...
class B:
...
Since this change breaks compatibility, the new behavior needs to be
enabled on a per-module basis in Python 3.7 using a *note __future__: 0.
import:
from __future__ import annotations
It will become the default in Python 3.10.
See also
........
PEP 563(3) – Postponed evaluation of annotations
PEP written and implemented by Łukasz Langa.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3107
(2) https://www.python.org/dev/peps/pep-0526
(3) https://www.python.org/dev/peps/pep-0563
File: python.info, Node: PEP 538 Legacy C Locale Coercion, Next: PEP 540 Forced UTF-8 Runtime Mode, Prev: PEP 563 Postponed Evaluation of Annotations, Up: New Features<2>
1.2.2.2 PEP 538: Legacy C Locale Coercion
.........................................
An ongoing challenge within the Python 3 series has been determining a
sensible default strategy for handling the “7-bit ASCII” text encoding
assumption currently implied by the use of the default C or POSIX locale
on non-Windows platforms.
PEP 538(1) updates the default interpreter command line interface to
automatically coerce that locale to an available UTF-8 based locale as
described in the documentation of the new *note PYTHONCOERCECLOCALE:
30c. environment variable. Automatically setting ‘LC_CTYPE’ this way
means that both the core interpreter and locale-aware C extensions (such
as *note readline: de.) will assume the use of UTF-8 as the default text
encoding, rather than ASCII.
The platform support definition in PEP 11(2) has also been updated to
limit full text handling support to suitably configured non-ASCII based
locales.
As part of this change, the default error handler for *note stdin: 30d.
and *note stdout: 30e. is now ‘surrogateescape’ (rather than ‘strict’)
when using any of the defined coercion target locales (currently
‘C.UTF-8’, ‘C.utf8’, and ‘UTF-8’). The default error handler for *note
stderr: 30f. continues to be ‘backslashreplace’, regardless of locale.
Locale coercion is silent by default, but to assist in debugging
potentially locale related integration problems, explicit warnings
(emitted directly on *note stderr: 30f.) can be requested by setting
‘PYTHONCOERCECLOCALE=warn’. This setting will also cause the Python
runtime to emit a warning if the legacy C locale remains active when the
core interpreter is initialized.
While PEP 538(3)’s locale coercion has the benefit of also affecting
extension modules (such as GNU ‘readline’), as well as child processes
(including those running non-Python applications and older versions of
Python), it has the downside of requiring that a suitable target locale
be present on the running system. To better handle the case where no
suitable target locale is available (as occurs on RHEL/CentOS 7, for
example), Python 3.7 also implements *note PEP 540; Forced UTF-8 Runtime
Mode: 300.
See also
........
PEP 538(4) – Coercing the legacy C locale to a UTF-8 based locale
PEP written and implemented by Nick Coghlan.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0538
(2) https://www.python.org/dev/peps/pep-0011
(3) https://www.python.org/dev/peps/pep-0538
(4) https://www.python.org/dev/peps/pep-0538
File: python.info, Node: PEP 540 Forced UTF-8 Runtime Mode, Next: PEP 553 Built-in breakpoint, Prev: PEP 538 Legacy C Locale Coercion, Up: New Features<2>
1.2.2.3 PEP 540: Forced UTF-8 Runtime Mode
..........................................
The new *note -X: 155. ‘utf8’ command line option and *note PYTHONUTF8:
311. environment variable can be used to enable the CPython `UTF-8
mode'.
When in UTF-8 mode, CPython ignores the locale settings, and uses the
UTF-8 encoding by default. The error handlers for *note sys.stdin: 30d.
and *note sys.stdout: 30e. streams are set to ‘surrogateescape’.
The forced UTF-8 mode can be used to change the text handling behavior
in an embedded Python interpreter without changing the locale settings
of an embedding application.
While PEP 540(1)’s UTF-8 mode has the benefit of working regardless of
which locales are available on the running system, it has the downside
of having no effect on extension modules (such as GNU ‘readline’), child
processes running non-Python applications, and child processes running
older versions of Python. To reduce the risk of corrupting text data
when communicating with such components, Python 3.7 also implements
*note PEP 540; Forced UTF-8 Runtime Mode: 300.).
The UTF-8 mode is enabled by default when the locale is ‘C’ or ‘POSIX’,
and the PEP 538(2) locale coercion feature fails to change it to a UTF-8
based alternative (whether that failure is due to
‘PYTHONCOERCECLOCALE=0’ being set, ‘LC_ALL’ being set, or the lack of a
suitable target locale).
See also
........
PEP 540(3) – Add a new UTF-8 mode
PEP written and implemented by Victor Stinner
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0540
(2) https://www.python.org/dev/peps/pep-0538
(3) https://www.python.org/dev/peps/pep-0540
File: python.info, Node: PEP 553 Built-in breakpoint, Next: PEP 539 New C API for Thread-Local Storage, Prev: PEP 540 Forced UTF-8 Runtime Mode, Up: New Features<2>
1.2.2.4 PEP 553: Built-in ‘breakpoint()’
........................................
Python 3.7 includes the new built-in *note breakpoint(): 2f9. function
as an easy and consistent way to enter the Python debugger.
Built-in ‘breakpoint()’ calls *note sys.breakpointhook(): 313. By
default, the latter imports *note pdb: c9. and then calls
‘pdb.set_trace()’, but by binding ‘sys.breakpointhook()’ to the function
of your choosing, ‘breakpoint()’ can enter any debugger. Additionally,
the environment variable *note PYTHONBREAKPOINT: 314. can be set to the
callable of your debugger of choice. Set ‘PYTHONBREAKPOINT=0’ to
completely disable built-in ‘breakpoint()’.
See also
........
PEP 553(1) – Built-in breakpoint()
PEP written and implemented by Barry Warsaw
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0553
File: python.info, Node: PEP 539 New C API for Thread-Local Storage, Next: PEP 562 Customization of Access to Module Attributes, Prev: PEP 553 Built-in breakpoint, Up: New Features<2>
1.2.2.5 PEP 539: New C API for Thread-Local Storage
...................................................
While Python provides a C API for thread-local storage support; the
existing *note Thread Local Storage (TLS) API: 316. has used ‘int’ to
represent TLS keys across all platforms. This has not generally been a
problem for officially-support platforms, but that is neither
POSIX-compliant, nor portable in any practical sense.
PEP 539(1) changes this by providing a new *note Thread Specific Storage
(TSS) API: 317. to CPython which supersedes use of the existing TLS API
within the CPython interpreter, while deprecating the existing API. The
TSS API uses a new type *note Py_tss_t: 318. instead of ‘int’ to
represent TSS keys–an opaque type the definition of which may depend on
the underlying TLS implementation. Therefore, this will allow to build
CPython on platforms where the native TLS key is defined in a way that
cannot be safely cast to ‘int’.
Note that on platforms where the native TLS key is defined in a way that
cannot be safely cast to ‘int’, all functions of the existing TLS API
will be no-op and immediately return failure. This indicates clearly
that the old API is not supported on platforms where it cannot be used
reliably, and that no effort will be made to add such support.
See also
........
PEP 539(2) – A New C-API for Thread-Local Storage in CPython
PEP written by Erik M. Bray; implementation by Masayuki Yamamoto.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0539
(2) https://www.python.org/dev/peps/pep-0539
File: python.info, Node: PEP 562 Customization of Access to Module Attributes, Next: PEP 564 New Time Functions With Nanosecond Resolution, Prev: PEP 539 New C API for Thread-Local Storage, Up: New Features<2>
1.2.2.6 PEP 562: Customization of Access to Module Attributes
.............................................................
Python 3.7 allows defining *note __getattr__(): 31a. on modules and will
call it whenever a module attribute is otherwise not found. Defining
*note __dir__(): 31b. on modules is now also allowed.
A typical example of where this may be useful is module attribute
deprecation and lazy loading.
See also
........
PEP 562(1) – Module ‘__getattr__’ and ‘__dir__’
PEP written and implemented by Ivan Levkivskyi
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0562
File: python.info, Node: PEP 564 New Time Functions With Nanosecond Resolution, Next: PEP 565 Show DeprecationWarning in __main__, Prev: PEP 562 Customization of Access to Module Attributes, Up: New Features<2>
1.2.2.7 PEP 564: New Time Functions With Nanosecond Resolution
..............................................................
The resolution of clocks in modern systems can exceed the limited
precision of a floating point number returned by the *note time.time():
31d. function and its variants. To avoid loss of precision, PEP 564(1)
adds six new “nanosecond” variants of the existing timer functions to
the *note time: 10a. module:
* *note time.clock_gettime_ns(): 31e.
* *note time.clock_settime_ns(): 31f.
* *note time.monotonic_ns(): 320.
* *note time.perf_counter_ns(): 321.
* *note time.process_time_ns(): 322.
* *note time.time_ns(): 323.
The new functions return the number of nanoseconds as an integer value.
Measurements(2) show that on Linux and Windows the resolution of *note
time.time_ns(): 323. is approximately 3 times better than that of *note
time.time(): 31d.
See also
........
PEP 564(3) – Add new time functions with nanosecond resolution
PEP written and implemented by Victor Stinner
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0564
(2)
https://www.python.org/dev/peps/pep-0564/#annex-clocks-resolution-in-python
(3) https://www.python.org/dev/peps/pep-0564
File: python.info, Node: PEP 565 Show DeprecationWarning in __main__, Next: PEP 560 Core Support for typing module and Generic Types, Prev: PEP 564 New Time Functions With Nanosecond Resolution, Up: New Features<2>
1.2.2.8 PEP 565: Show DeprecationWarning in ‘__main__’
......................................................
The default handling of *note DeprecationWarning: 278. has been changed
such that these warnings are once more shown by default, but only when
the code triggering them is running directly in the *note __main__: 1.
module. As a result, developers of single file scripts and those using
Python interactively should once again start seeing deprecation warnings
for the APIs they use, but deprecation warnings triggered by imported
application, library and framework modules will continue to be hidden by
default.
As a result of this change, the standard library now allows developers
to choose between three different deprecation warning behaviours:
* *note FutureWarning: 325.: always displayed by default, recommended
for warnings intended to be seen by application end users (e.g.
for deprecated application configuration settings).
* *note DeprecationWarning: 278.: displayed by default only in *note
__main__: 1. and when running tests, recommended for warnings
intended to be seen by other Python developers where a version
upgrade may result in changed behaviour or an error.
* *note PendingDeprecationWarning: 279.: displayed by default only
when running tests, intended for cases where a future version
upgrade will change the warning category to *note
DeprecationWarning: 278. or *note FutureWarning: 325.
Previously both *note DeprecationWarning: 278. and *note
PendingDeprecationWarning: 279. were only visible when running tests,
which meant that developers primarily writing single file scripts or
using Python interactively could be surprised by breaking changes in the
APIs they used.
See also
........
PEP 565(1) – Show DeprecationWarning in ‘__main__’
PEP written and implemented by Nick Coghlan
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0565
File: python.info, Node: PEP 560 Core Support for typing module and Generic Types, Next: PEP 552 Hash-based pyc Files, Prev: PEP 565 Show DeprecationWarning in __main__, Up: New Features<2>
1.2.2.9 PEP 560: Core Support for ‘typing’ module and Generic Types
...................................................................
Initially PEP 484(1) was designed in such way that it would not
introduce `any' changes to the core CPython interpreter. Now type hints
and the *note typing: 119. module are extensively used by the community,
so this restriction is removed. The PEP introduces two special methods
*note __class_getitem__(): 327. and ‘__mro_entries__’, these methods are
now used by most classes and special constructs in *note typing: 119.
As a result, the speed of various operations with types increased up to
7 times, the generic types can be used without metaclass conflicts, and
several long standing bugs in *note typing: 119. module are fixed.
See also
........
PEP 560(2) – Core support for typing module and generic types
PEP written and implemented by Ivan Levkivskyi
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0484
(2) https://www.python.org/dev/peps/pep-0560
File: python.info, Node: PEP 552 Hash-based pyc Files, Next: PEP 545 Python Documentation Translations, Prev: PEP 560 Core Support for typing module and Generic Types, Up: New Features<2>
1.2.2.10 PEP 552: Hash-based .pyc Files
.......................................
Python has traditionally checked the up-to-dateness of bytecode cache
files (i.e., ‘.pyc’ files) by comparing the source metadata
(last-modified timestamp and size) with source metadata saved in the
cache file header when it was generated. While effective, this
invalidation method has its drawbacks. When filesystem timestamps are
too coarse, Python can miss source updates, leading to user confusion.
Additionally, having a timestamp in the cache file is problematic for
build reproducibility(1) and content-based build systems.
PEP 552(2) extends the pyc format to allow the hash of the source file
to be used for invalidation instead of the source timestamp. Such
‘.pyc’ files are called “hash-based”. By default, Python still uses
timestamp-based invalidation and does not generate hash-based ‘.pyc’
files at runtime. Hash-based ‘.pyc’ files may be generated with *note
py_compile: d7. or *note compileall: 21.
Hash-based ‘.pyc’ files come in two variants: checked and unchecked.
Python validates checked hash-based ‘.pyc’ files against the
corresponding source files at runtime but doesn’t do so for unchecked
hash-based pycs. Unchecked hash-based ‘.pyc’ files are a useful
performance optimization for environments where a system external to
Python (e.g., the build system) is responsible for keeping ‘.pyc’ files
up-to-date.
See *note Cached bytecode invalidation: 329. for more information.
See also
........
PEP 552(3) – Deterministic pycs
PEP written and implemented by Benjamin Peterson
---------- Footnotes ----------
(1) https://reproducible-builds.org/
(2) https://www.python.org/dev/peps/pep-0552
(3) https://www.python.org/dev/peps/pep-0552
File: python.info, Node: PEP 545 Python Documentation Translations, Next: Development Runtime Mode -X dev, Prev: PEP 552 Hash-based pyc Files, Up: New Features<2>
1.2.2.11 PEP 545: Python Documentation Translations
...................................................
PEP 545(1) describes the process of creating and maintaining Python
documentation translations.
Three new translations have been added:
- Japanese: ‘https://docs.python.org/ja/’
- French: ‘https://docs.python.org/fr/’
- Korean: ‘https://docs.python.org/ko/’
See also
........
PEP 545(2) – Python Documentation Translations
PEP written and implemented by Julien Palard, Inada Naoki, and
Victor Stinner.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0545
(2) https://www.python.org/dev/peps/pep-0545
File: python.info, Node: Development Runtime Mode -X dev, Prev: PEP 545 Python Documentation Translations, Up: New Features<2>
1.2.2.12 Development Runtime Mode: -X dev
.........................................
The new *note -X: 155. ‘dev’ command line option or the new *note
PYTHONDEVMODE: 32c. environment variable can be used to enable CPython’s
`development mode'. When in development mode, CPython performs
additional runtime checks that are too expensive to be enabled by
default. See *note -X: 155. ‘dev’ documentation for the full
description of the effects of this mode.
File: python.info, Node: Other Language Changes<2>, Next: New Modules<2>, Prev: New Features<2>, Up: What’s New In Python 3 7
1.2.3 Other Language Changes
----------------------------
* An *note await: 1a3. expression and comprehensions containing an
*note async for: 2e3. clause were illegal in the expressions in
*note formatted string literals: 15c. due to a problem with the
implementation. In Python 3.7 this restriction was lifted.
* More than 255 arguments can now be passed to a function, and a
function can now have more than 255 parameters. (Contributed by
Serhiy Storchaka in bpo-12844(1) and bpo-18896(2).)
* *note bytes.fromhex(): 32e. and *note bytearray.fromhex(): 32f. now
ignore all ASCII whitespace, not only spaces. (Contributed by
Robert Xiao in bpo-28927(3).)
* *note str: 330, *note bytes: 331, and *note bytearray: 332. gained
support for the new *note isascii(): 333. method, which can be used
to test if a string or bytes contain only the ASCII characters.
(Contributed by INADA Naoki in bpo-32677(4).)
* *note ImportError: 334. now displays module name and module
‘__file__’ path when ‘from ... import ...’ fails. (Contributed by
Matthias Bussonnier in bpo-29546(5).)
* Circular imports involving absolute imports with binding a
submodule to a name are now supported. (Contributed by Serhiy
Storchaka in bpo-30024(6).)
* ‘object.__format__(x, '')’ is now equivalent to ‘str(x)’ rather
than ‘format(str(self), '')’. (Contributed by Serhiy Storchaka in
bpo-28974(7).)
* In order to better support dynamic creation of stack traces, *note
types.TracebackType: 335. can now be instantiated from Python code,
and the ‘tb_next’ attribute on *note tracebacks: 336. is now
writable. (Contributed by Nathaniel J. Smith in bpo-30579(8).)
* When using the *note -m: 337. switch, ‘sys.path[0]’ is now eagerly
expanded to the full starting directory path, rather than being
left as the empty directory (which allows imports from the
`current' working directory at the time when an import occurs)
(Contributed by Nick Coghlan in bpo-33053(9).)
* The new *note -X: 155. ‘importtime’ option or the *note
PYTHONPROFILEIMPORTTIME: 338. environment variable can be used to
show the timing of each module import. (Contributed by Victor
Stinner in bpo-31415(10).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12844
(2) https://bugs.python.org/issue18896
(3) https://bugs.python.org/issue28927
(4) https://bugs.python.org/issue32677
(5) https://bugs.python.org/issue29546
(6) https://bugs.python.org/issue30024
(7) https://bugs.python.org/issue28974
(8) https://bugs.python.org/issue30579
(9) https://bugs.python.org/issue33053
(10) https://bugs.python.org/issue31415
File: python.info, Node: New Modules<2>, Next: Improved Modules<2>, Prev: Other Language Changes<2>, Up: What’s New In Python 3 7
1.2.4 New Modules
-----------------
* Menu:
* contextvars::
* dataclasses::
* importlib.resources: importlib resources.
File: python.info, Node: contextvars, Next: dataclasses, Up: New Modules<2>
1.2.4.1 contextvars
...................
The new *note contextvars: 25. module and a set of *note new C APIs:
33b. introduce support for `context variables'. Context variables are
conceptually similar to thread-local variables. Unlike TLS, context
variables support asynchronous code correctly.
The *note asyncio: a. and *note decimal: 36. modules have been updated
to use and support context variables out of the box. Particularly the
active decimal context is now stored in a context variable, which allows
decimal operations to work with the correct context in asynchronous
code.
See also
........
PEP 567(1) – Context Variables
PEP written and implemented by Yury Selivanov
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0567
File: python.info, Node: dataclasses, Next: importlib resources, Prev: contextvars, Up: New Modules<2>
1.2.4.2 dataclasses
...................
The new *note dataclass(): 33d. decorator provides a way to declare
`data classes'. A data class describes its attributes using class
variable annotations. Its constructor and other magic methods, such as
*note __repr__(): 33e, *note __eq__(): 33f, and *note __hash__(): 340.
are generated automatically.
Example:
@dataclass
class Point:
x: float
y: float
z: float = 0.0
p = Point(1.5, 2.5)
print(p) # produces "Point(x=1.5, y=2.5, z=0.0)"
See also
........
PEP 557(1) – Data Classes
PEP written and implemented by Eric V. Smith
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0557
File: python.info, Node: importlib resources, Prev: dataclasses, Up: New Modules<2>
1.2.4.3 importlib.resources
...........................
The new *note importlib.resources: 9e. module provides several new APIs
and one new ABC for access to, opening, and reading `resources' inside
packages. Resources are roughly similar to files inside packages, but
they needn’t be actual files on the physical file system. Module
loaders can provide a ‘get_resource_reader()’ function which returns a
*note importlib.abc.ResourceReader: 342. instance to support this new
API. Built-in file path loaders and zip file loaders both support this.
Contributed by Barry Warsaw and Brett Cannon in bpo-32248(1).
See also
........
importlib_resources(2) – a PyPI backport for earlier Python versions.
---------- Footnotes ----------
(1) https://bugs.python.org/issue32248
(2) http://importlib-resources.readthedocs.io/en/latest/
File: python.info, Node: Improved Modules<2>, Next: C API Changes, Prev: New Modules<2>, Up: What’s New In Python 3 7
1.2.5 Improved Modules
----------------------
* Menu:
* argparse::
* asyncio: asyncio<2>.
* binascii::
* calendar::
* collections: collections<2>.
* compileall::
* concurrent.futures: concurrent futures.
* contextlib::
* cProfile: cProfile<2>.
* crypt::
* datetime: datetime<2>.
* dbm::
* decimal::
* dis::
* distutils::
* enum::
* functools: functools<2>.
* gc: gc<2>.
* hmac::
* http.client: http client.
* http.server: http server.
* idlelib and IDLE::
* importlib::
* io: io<2>.
* ipaddress::
* itertools: itertools<2>.
* locale::
* logging: logging<2>.
* math: math<2>.
* mimetypes::
* msilib::
* multiprocessing: multiprocessing<2>.
* os: os<2>.
* pathlib: pathlib<2>.
* pdb::
* py_compile: py_compile<2>.
* pydoc::
* queue::
* re::
* signal::
* socket: socket<2>.
* socketserver::
* sqlite3::
* ssl: ssl<2>.
* string::
* subprocess::
* sys: sys<2>.
* time: time<2>.
* tkinter: tkinter<2>.
* tracemalloc::
* types::
* unicodedata: unicodedata<2>.
* unittest: unittest<2>.
* unittest.mock: unittest mock.
* urllib.parse: urllib parse.
* uu::
* uuid::
* warnings::
* xml.etree: xml etree.
* xmlrpc.server: xmlrpc server.
* zipapp::
* zipfile::
File: python.info, Node: argparse, Next: asyncio<2>, Up: Improved Modules<2>
1.2.5.1 argparse
................
The new *note ArgumentParser.parse_intermixed_args(): 345. method allows
intermixing options and positional arguments. (Contributed by paul.j3
in bpo-14191(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14191
File: python.info, Node: asyncio<2>, Next: binascii, Prev: argparse, Up: Improved Modules<2>
1.2.5.2 asyncio
...............
The *note asyncio: a. module has received many new features, usability
and *note performance improvements: 347. Notable changes include:
* The new *note provisional: 348. *note asyncio.run(): 1a5. function
can be used to run a coroutine from synchronous code by
automatically creating and destroying the event loop. (Contributed
by Yury Selivanov in bpo-32314(1).)
* asyncio gained support for *note contextvars: 25. *note
loop.call_soon(): 349, *note loop.call_soon_threadsafe(): 34a,
*note loop.call_later(): 34b, *note loop.call_at(): 34c, and *note
Future.add_done_callback(): 34d. have a new optional keyword-only
`context' parameter. *note Tasks: 1ad. now track their context
automatically. See PEP 567(2) for more details. (Contributed by
Yury Selivanov in bpo-32436(3).)
* The new *note asyncio.create_task(): 1ae. function has been added
as a shortcut to ‘asyncio.get_event_loop().create_task()’.
(Contributed by Andrew Svetlov in bpo-32311(4).)
* The new *note loop.start_tls(): 34e. method can be used to upgrade
an existing connection to TLS. (Contributed by Yury Selivanov in
bpo-23749(5).)
* The new *note loop.sock_recv_into(): 34f. method allows reading
data from a socket directly into a provided buffer making it
possible to reduce data copies. (Contributed by Antoine Pitrou in
bpo-31819(6).)
* The new *note asyncio.current_task(): 350. function returns the
currently running *note Task: 1ad. instance, and the new *note
asyncio.all_tasks(): 351. function returns a set of all existing
‘Task’ instances in a given loop. The *note Task.current_task():
352. and *note Task.all_tasks(): 353. methods have been deprecated.
(Contributed by Andrew Svetlov in bpo-32250(7).)
* The new `provisional' *note BufferedProtocol: 2c9. class allows
implementing streaming protocols with manual control over the
receive buffer. (Contributed by Yury Selivanov in bpo-32251(8).)
* The new *note asyncio.get_running_loop(): 354. function returns the
currently running loop, and raises a *note RuntimeError: 2ba. if no
loop is running. This is in contrast with *note
asyncio.get_event_loop(): 355, which will `create' a new event loop
if none is running. (Contributed by Yury Selivanov in
bpo-32269(9).)
* The new *note StreamWriter.wait_closed(): 356. coroutine method
allows waiting until the stream writer is closed. The new *note
StreamWriter.is_closing(): 357. method can be used to determine if
the writer is closing. (Contributed by Andrew Svetlov in
bpo-32391(10).)
* The new *note loop.sock_sendfile(): 358. coroutine method allows
sending files using *note os.sendfile: 359. when possible.
(Contributed by Andrew Svetlov in bpo-32410(11).)
* The new *note Future.get_loop(): 35a. and ‘Task.get_loop()’ methods
return the instance of the loop on which a task or a future were
created. *note Server.get_loop(): 35b. allows doing the same for
*note asyncio.Server: 35c. objects. (Contributed by Yury Selivanov
in bpo-32415(12) and Srinivas Reddy Thatiparthy in bpo-32418(13).)
* It is now possible to control how instances of *note
asyncio.Server: 35c. begin serving. Previously, the server would
start serving immediately when created. The new `start_serving'
keyword argument to *note loop.create_server(): 35d. and *note
loop.create_unix_server(): 35e, as well as *note
Server.start_serving(): 35f, and *note Server.serve_forever(): 360.
can be used to decouple server instantiation and serving. The new
*note Server.is_serving(): 361. method returns ‘True’ if the server
is serving. *note Server: 35c. objects are now asynchronous
context managers:
srv = await loop.create_server(...)
async with srv:
# some code
# At this point, srv is closed and no longer accepts new connections.
(Contributed by Yury Selivanov in bpo-32662(14).)
* Callback objects returned by *note loop.call_later(): 34b. gained
the new *note when(): 362. method which returns an absolute
scheduled callback timestamp. (Contributed by Andrew Svetlov in
bpo-32741(15).)
* The *note loop.create_datagram_endpoint(): 2e7. method gained
support for Unix sockets. (Contributed by Quentin Dawans in
bpo-31245(16).)
* The *note asyncio.open_connection(): 363, *note
asyncio.start_server(): 364. functions, *note
loop.create_connection(): 1b2, *note loop.create_server(): 35d,
*note loop.create_accepted_socket(): 365. methods and their
corresponding UNIX socket variants now accept the
`ssl_handshake_timeout' keyword argument. (Contributed by Neil
Aspinall in bpo-29970(17).)
* The new *note Handle.cancelled(): 366. method returns ‘True’ if the
callback was cancelled. (Contributed by Marat Sharafutdinov in
bpo-31943(18).)
* The asyncio source has been converted to use the *note async:
1a2./*note await: 1a3. syntax. (Contributed by Andrew Svetlov in
bpo-32193(19).)
* The new *note ReadTransport.is_reading(): 367. method can be used
to determine the reading state of the transport. Additionally,
calls to *note ReadTransport.resume_reading(): 368. and *note
ReadTransport.pause_reading(): 369. are now idempotent.
(Contributed by Yury Selivanov in bpo-32356(20).)
* Loop methods which accept socket paths now support passing *note
path-like objects: 36a. (Contributed by Yury Selivanov in
bpo-32066(21).)
* In *note asyncio: a. TCP sockets on Linux are now created with
‘TCP_NODELAY’ flag set by default. (Contributed by Yury Selivanov
and Victor Stinner in bpo-27456(22).)
* Exceptions occurring in cancelled tasks are no longer logged.
(Contributed by Yury Selivanov in bpo-30508(23).)
* New ‘WindowsSelectorEventLoopPolicy’ and
‘WindowsProactorEventLoopPolicy’ classes. (Contributed by Yury
Selivanov in bpo-33792(24).)
Several ‘asyncio’ APIs have been *note deprecated: 36b.
---------- Footnotes ----------
(1) https://bugs.python.org/issue32314
(2) https://www.python.org/dev/peps/pep-0567
(3) https://bugs.python.org/issue32436
(4) https://bugs.python.org/issue32311
(5) https://bugs.python.org/issue23749
(6) https://bugs.python.org/issue31819
(7) https://bugs.python.org/issue32250
(8) https://bugs.python.org/issue32251
(9) https://bugs.python.org/issue32269
(10) https://bugs.python.org/issue32391
(11) https://bugs.python.org/issue32410
(12) https://bugs.python.org/issue32415
(13) https://bugs.python.org/issue32418
(14) https://bugs.python.org/issue32662
(15) https://bugs.python.org/issue32741
(16) https://bugs.python.org/issue31245
(17) https://bugs.python.org/issue29970
(18) https://bugs.python.org/issue31943
(19) https://bugs.python.org/issue32193
(20) https://bugs.python.org/issue32356
(21) https://bugs.python.org/issue32066
(22) https://bugs.python.org/issue27456
(23) https://bugs.python.org/issue30508
(24) https://bugs.python.org/issue33792
File: python.info, Node: binascii, Next: calendar, Prev: asyncio<2>, Up: Improved Modules<2>
1.2.5.3 binascii
................
The *note b2a_uu(): 36d. function now accepts an optional `backtick'
keyword argument. When it’s true, zeros are represented by ‘'`'’
instead of spaces. (Contributed by Xiang Zhang in bpo-30103(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30103
File: python.info, Node: calendar, Next: collections<2>, Prev: binascii, Up: Improved Modules<2>
1.2.5.4 calendar
................
The *note HTMLCalendar: 36f. class has new class attributes which ease
the customization of CSS classes in the produced HTML calendar.
(Contributed by Oz Tiram in bpo-30095(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30095
File: python.info, Node: collections<2>, Next: compileall, Prev: calendar, Up: Improved Modules<2>
1.2.5.5 collections
...................
‘collections.namedtuple()’ now supports default values. (Contributed by
Raymond Hettinger in bpo-32320(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32320
File: python.info, Node: compileall, Next: concurrent futures, Prev: collections<2>, Up: Improved Modules<2>
1.2.5.6 compileall
..................
*note compileall.compile_dir(): 372. learned the new `invalidation_mode'
parameter, which can be used to enable *note hash-based .pyc
invalidation: 301. The invalidation mode can also be specified on the
command line using the new ‘--invalidation-mode’ argument. (Contributed
by Benjamin Peterson in bpo-31650(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31650
File: python.info, Node: concurrent futures, Next: contextlib, Prev: compileall, Up: Improved Modules<2>
1.2.5.7 concurrent.futures
..........................
*note ProcessPoolExecutor: 2a4. and *note ThreadPoolExecutor: 27a. now
support the new `initializer' and `initargs' constructor arguments.
(Contributed by Antoine Pitrou in bpo-21423(1).)
The *note ProcessPoolExecutor: 2a4. can now take the multiprocessing
context via the new `mp_context' argument. (Contributed by Thomas
Moreau in bpo-31540(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21423
(2) https://bugs.python.org/issue31540
File: python.info, Node: contextlib, Next: cProfile<2>, Prev: concurrent futures, Up: Improved Modules<2>
1.2.5.8 contextlib
..................
The new *note nullcontext(): 375. is a simpler and faster no-op context
manager than *note ExitStack: 376. (Contributed by Jesse-Bakker in
bpo-10049(1).)
The new *note asynccontextmanager(): 377, *note
AbstractAsyncContextManager: 378, and *note AsyncExitStack: 379. have
been added to complement their synchronous counterparts. (Contributed
by Jelle Zijlstra in bpo-29679(2) and bpo-30241(3), and by Alexander
Mohr and Ilya Kulakov in bpo-29302(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10049
(2) https://bugs.python.org/issue29679
(3) https://bugs.python.org/issue30241
(4) https://bugs.python.org/issue29302
File: python.info, Node: cProfile<2>, Next: crypt, Prev: contextlib, Up: Improved Modules<2>
1.2.5.9 cProfile
................
The *note cProfile: 28. command line now accepts ‘-m module_name’ as an
alternative to script path. (Contributed by Sanyam Khurana in
bpo-21862(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21862
File: python.info, Node: crypt, Next: datetime<2>, Prev: cProfile<2>, Up: Improved Modules<2>
1.2.5.10 crypt
..............
The *note crypt: 29. module now supports the Blowfish hashing method.
(Contributed by Serhiy Storchaka in bpo-31664(1).)
The *note mksalt(): 37c. function now allows specifying the number of
rounds for hashing. (Contributed by Serhiy Storchaka in bpo-31702(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31664
(2) https://bugs.python.org/issue31702
File: python.info, Node: datetime<2>, Next: dbm, Prev: crypt, Up: Improved Modules<2>
1.2.5.11 datetime
.................
The new *note datetime.fromisoformat(): 37e. method constructs a *note
datetime: 194. object from a string in one of the formats output by
*note datetime.isoformat(): 37f. (Contributed by Paul Ganssle in
bpo-15873(1).)
The *note tzinfo: 380. class now supports sub-minute offsets.
(Contributed by Alexander Belopolsky in bpo-5288(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15873
(2) https://bugs.python.org/issue5288
File: python.info, Node: dbm, Next: decimal, Prev: datetime<2>, Up: Improved Modules<2>
1.2.5.12 dbm
............
*note dbm.dumb: 33. now supports reading read-only files and no longer
writes the index file when it is not changed.
File: python.info, Node: decimal, Next: dis, Prev: dbm, Up: Improved Modules<2>
1.2.5.13 decimal
................
The *note decimal: 36. module now uses *note context variables: 2f5. to
store the decimal context. (Contributed by Yury Selivanov in
bpo-32630(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32630
File: python.info, Node: dis, Next: distutils, Prev: decimal, Up: Improved Modules<2>
1.2.5.14 dis
............
The *note dis(): 384. function is now able to disassemble nested code
objects (the code of comprehensions, generator expressions and nested
functions, and the code used for building nested classes). The maximum
depth of disassembly recursion is controlled by the new `depth'
parameter. (Contributed by Serhiy Storchaka in bpo-11822(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11822
File: python.info, Node: distutils, Next: enum, Prev: dis, Up: Improved Modules<2>
1.2.5.15 distutils
..................
‘README.rst’ is now included in the list of distutils standard READMEs
and therefore included in source distributions. (Contributed by Ryan
Gonzalez in bpo-11913(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11913
File: python.info, Node: enum, Next: functools<2>, Prev: distutils, Up: Improved Modules<2>
1.2.5.16 enum
.............
The *note Enum: 387. learned the new ‘_ignore_’ class property, which
allows listing the names of properties which should not become enum
members. (Contributed by Ethan Furman in bpo-31801(1).)
In Python 3.8, attempting to check for non-Enum objects in ‘Enum’
classes will raise a *note TypeError: 192. (e.g. ‘1 in Color’);
similarly, attempting to check for non-Flag objects in a ‘Flag’ member
will raise *note TypeError: 192. (e.g. ‘1 in Perm.RW’); currently, both
operations return *note False: 388. instead and are deprecated.
(Contributed by Ethan Furman in bpo-33217(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31801
(2) https://bugs.python.org/issue33217
File: python.info, Node: functools<2>, Next: gc<2>, Prev: enum, Up: Improved Modules<2>
1.2.5.17 functools
..................
*note functools.singledispatch(): 38a. now supports registering
implementations using type annotations. (Contributed by Łukasz Langa in
bpo-32227(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32227
File: python.info, Node: gc<2>, Next: hmac, Prev: functools<2>, Up: Improved Modules<2>
1.2.5.18 gc
...........
The new *note gc.freeze(): 38c. function allows freezing all objects
tracked by the garbage collector and excluding them from future
collections. This can be used before a POSIX ‘fork()’ call to make the
GC copy-on-write friendly or to speed up collection. The new *note
gc.unfreeze(): 38d. functions reverses this operation. Additionally,
*note gc.get_freeze_count(): 38e. can be used to obtain the number of
frozen objects. (Contributed by Li Zekun in bpo-31558(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31558
File: python.info, Node: hmac, Next: http client, Prev: gc<2>, Up: Improved Modules<2>
1.2.5.19 hmac
.............
The *note hmac: 8f. module now has an optimized one-shot *note digest():
390. function, which is up to three times faster than ‘HMAC()’.
(Contributed by Christian Heimes in bpo-32433(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32433
File: python.info, Node: http client, Next: http server, Prev: hmac, Up: Improved Modules<2>
1.2.5.20 http.client
....................
*note HTTPConnection: 392. and *note HTTPSConnection: 393. now support
the new `blocksize' argument for improved upload throughput.
(Contributed by Nir Soffer in bpo-31945(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31945
File: python.info, Node: http server, Next: idlelib and IDLE, Prev: http client, Up: Improved Modules<2>
1.2.5.21 http.server
....................
*note SimpleHTTPRequestHandler: 395. now supports the HTTP
‘If-Modified-Since’ header. The server returns the 304 response status
if the target file was not modified after the time specified in the
header. (Contributed by Pierre Quentel in bpo-29654(1).)
*note SimpleHTTPRequestHandler: 395. accepts the new `directory'
argument, in addition to the new ‘--directory’ command line argument.
With this parameter, the server serves the specified directory, by
default it uses the current working directory. (Contributed by Stéphane
Wirtel and Julien Palard in bpo-28707(2).)
The new *note ThreadingHTTPServer: 396. class uses threads to handle
requests using ‘ThreadingMixin’. It is used when ‘http.server’ is run
with ‘-m’. (Contributed by Julien Palard in bpo-31639(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue29654
(2) https://bugs.python.org/issue28707
(3) https://bugs.python.org/issue31639
File: python.info, Node: idlelib and IDLE, Next: importlib, Prev: http server, Up: Improved Modules<2>
1.2.5.22 idlelib and IDLE
.........................
Multiple fixes for autocompletion. (Contributed by Louie Lu in
bpo-15786(1).)
Module Browser (on the File menu, formerly called Class Browser), now
displays nested functions and classes in addition to top-level functions
and classes. (Contributed by Guilherme Polo, Cheryl Sabella, and Terry
Jan Reedy in bpo-1612262(2).)
The Settings dialog (Options, Configure IDLE) has been partly rewritten
to improve both appearance and function. (Contributed by Cheryl Sabella
and Terry Jan Reedy in multiple issues.)
The font sample now includes a selection of non-Latin characters so that
users can better see the effect of selecting a particular font.
(Contributed by Terry Jan Reedy in bpo-13802(3).) The sample can be
edited to include other characters. (Contributed by Serhiy Storchaka in
bpo-31860(4).)
The IDLE features formerly implemented as extensions have been
reimplemented as normal features. Their settings have been moved from
the Extensions tab to other dialog tabs. (Contributed by Charles
Wohlganger and Terry Jan Reedy in bpo-27099(5).)
Editor code context option revised. Box displays all context lines up
to maxlines. Clicking on a context line jumps the editor to that line.
Context colors for custom themes is added to Highlights tab of Settings
dialog. (Contributed by Cheryl Sabella and Terry Jan Reedy in
bpo-33642(6), bpo-33768(7), and bpo-33679(8).)
On Windows, a new API call tells Windows that tk scales for DPI. On
Windows 8.1+ or 10, with DPI compatibility properties of the Python
binary unchanged, and a monitor resolution greater than 96 DPI, this
should make text and lines sharper. It should otherwise have no effect.
(Contributed by Terry Jan Reedy in bpo-33656(9).)
New in 3.7.1:
Output over N lines (50 by default) is squeezed down to a button. N can
be changed in the PyShell section of the General page of the Settings
dialog. Fewer, but possibly extra long, lines can be squeezed by right
clicking on the output. Squeezed output can be expanded in place by
double-clicking the button or into the clipboard or a separate window by
right-clicking the button. (Contributed by Tal Einat in
bpo-1529353(10).)
The changes above have been backported to 3.6 maintenance releases.
NEW in 3.7.4:
Add “Run Customized” to the Run menu to run a module with customized
settings. Any command line arguments entered are added to sys.argv.
They re-appear in the box for the next customized run. One can also
suppress the normal Shell main module restart. (Contributed by Cheryl
Sabella, Terry Jan Reedy, and others in bpo-5680(11) and bpo-37627(12).)
New in 3.7.5:
Add optional line numbers for IDLE editor windows. Windows open without
line numbers unless set otherwise in the General tab of the
configuration dialog. Line numbers for an existing window are shown and
hidden in the Options menu. (Contributed by Tal Einat and Saimadhav
Heblikar in bpo-17535(13).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15786
(2) https://bugs.python.org/issue1612262
(3) https://bugs.python.org/issue13802
(4) https://bugs.python.org/issue31860
(5) https://bugs.python.org/issue27099
(6) https://bugs.python.org/issue33642
(7) https://bugs.python.org/issue33768
(8) https://bugs.python.org/issue33679
(9) https://bugs.python.org/issue33656
(10) https://bugs.python.org/issue1529353
(11) https://bugs.python.org/issue5680
(12) https://bugs.python.org/issue37627
(13) https://bugs.python.org/issue17535
File: python.info, Node: importlib, Next: io<2>, Prev: idlelib and IDLE, Up: Improved Modules<2>
1.2.5.23 importlib
..................
The *note importlib.abc.ResourceReader: 342. ABC was introduced to
support the loading of resources from packages. See also *note
importlib.resources: 2f7. (Contributed by Barry Warsaw, Brett Cannon in
bpo-32248(1).)
*note importlib.reload(): 399. now raises *note ModuleNotFoundError:
39a. if the module lacks a spec. (Contributed by Garvit Khatri in
bpo-29851(2).)
‘importlib.find_spec()’ now raises *note ModuleNotFoundError: 39a.
instead of *note AttributeError: 39b. if the specified parent module is
not a package (i.e. lacks a ‘__path__’ attribute). (Contributed by
Milan Oberkirch in bpo-30436(3).)
The new ‘importlib.source_hash()’ can be used to compute the hash of the
passed source. A *note hash-based .pyc file: 301. embeds the value
returned by this function.
---------- Footnotes ----------
(1) https://bugs.python.org/issue32248
(2) https://bugs.python.org/issue29851
(3) https://bugs.python.org/issue30436
File: python.info, Node: io<2>, Next: ipaddress, Prev: importlib, Up: Improved Modules<2>
1.2.5.24 io
...........
The new *note TextIOWrapper.reconfigure(): 39d. method can be used to
reconfigure the text stream with the new settings. (Contributed by
Antoine Pitrou in bpo-30526(1) and INADA Naoki in bpo-15216(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30526
(2) https://bugs.python.org/issue15216
File: python.info, Node: ipaddress, Next: itertools<2>, Prev: io<2>, Up: Improved Modules<2>
1.2.5.25 ipaddress
..................
The new ‘subnet_of()’ and ‘supernet_of()’ methods of *note
ipaddress.IPv6Network: 39f. and *note ipaddress.IPv4Network: 3a0. can be
used for network containment tests. (Contributed by Michel Albert and
Cheryl Sabella in bpo-20825(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20825
File: python.info, Node: itertools<2>, Next: locale, Prev: ipaddress, Up: Improved Modules<2>
1.2.5.26 itertools
..................
*note itertools.islice(): 3a2. now accepts *note integer-like objects:
18a. as start, stop, and slice arguments. (Contributed by Will Roberts
in bpo-30537(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30537
File: python.info, Node: locale, Next: logging<2>, Prev: itertools<2>, Up: Improved Modules<2>
1.2.5.27 locale
...............
The new `monetary' argument to *note locale.format_string(): 3a4. can be
used to make the conversion use monetary thousands separators and
grouping strings. (Contributed by Garvit in bpo-10379(1).)
The *note locale.getpreferredencoding(): 3a5. function now always
returns ‘'UTF-8'’ on Android or when in the *note forced UTF-8 mode:
300.
---------- Footnotes ----------
(1) https://bugs.python.org/issue10379
File: python.info, Node: logging<2>, Next: math<2>, Prev: locale, Up: Improved Modules<2>
1.2.5.28 logging
................
*note Logger: 3a7. instances can now be pickled. (Contributed by Vinay
Sajip in bpo-30520(1).)
The new *note StreamHandler.setStream(): 3a8. method can be used to
replace the logger stream after handler creation. (Contributed by Vinay
Sajip in bpo-30522(2).)
It is now possible to specify keyword arguments to handler constructors
in configuration passed to *note logging.config.fileConfig(): 3a9.
(Contributed by Preston Landers in bpo-31080(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30520
(2) https://bugs.python.org/issue30522
(3) https://bugs.python.org/issue31080
File: python.info, Node: math<2>, Next: mimetypes, Prev: logging<2>, Up: Improved Modules<2>
1.2.5.29 math
.............
The new *note math.remainder(): 3ab. function implements the IEEE
754-style remainder operation. (Contributed by Mark Dickinson in
bpo-29962(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue29962
File: python.info, Node: mimetypes, Next: msilib, Prev: math<2>, Up: Improved Modules<2>
1.2.5.30 mimetypes
..................
The MIME type of .bmp has been changed from ‘'image/x-ms-bmp'’ to
‘'image/bmp'’. (Contributed by Nitish Chandra in bpo-22589(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22589
File: python.info, Node: msilib, Next: multiprocessing<2>, Prev: mimetypes, Up: Improved Modules<2>
1.2.5.31 msilib
...............
The new *note Database.Close(): 3ae. method can be used to close the MSI
(last-in, first-out) database. (Contributed by Berker Peksag in
bpo-20486(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20486
File: python.info, Node: multiprocessing<2>, Next: os<2>, Prev: msilib, Up: Improved Modules<2>
1.2.5.32 multiprocessing
........................
The new *note Process.close(): 3b0. method explicitly closes the process
object and releases all resources associated with it. *note ValueError:
1fb. is raised if the underlying process is still running. (Contributed
by Antoine Pitrou in bpo-30596(1).)
The new *note Process.kill(): 3b1. method can be used to terminate the
process using the ‘SIGKILL’ signal on Unix. (Contributed by Vitor
Pereira in bpo-30794(2).)
Non-daemonic threads created by *note Process: 3b2. are now joined on
process exit. (Contributed by Antoine Pitrou in bpo-18966(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30596
(2) https://bugs.python.org/issue30794
(3) https://bugs.python.org/issue18966
File: python.info, Node: os<2>, Next: pathlib<2>, Prev: multiprocessing<2>, Up: Improved Modules<2>
1.2.5.33 os
...........
*note os.fwalk(): 3b4. now accepts the `path' argument as *note bytes:
331. (Contributed by Serhiy Storchaka in bpo-28682(1).)
*note os.scandir(): 25f. gained support for *note file descriptors: 3b5.
(Contributed by Serhiy Storchaka in bpo-25996(2).)
The new *note register_at_fork(): 3b6. function allows registering
Python callbacks to be executed at process fork. (Contributed by
Antoine Pitrou in bpo-16500(3).)
Added *note os.preadv(): 3b7. (combine the functionality of *note
os.readv(): 3b8. and *note os.pread(): 3b9.) and *note os.pwritev():
3ba. functions (combine the functionality of *note os.writev(): 3bb. and
*note os.pwrite(): 3bc.). (Contributed by Pablo Galindo in
bpo-31368(4).)
The mode argument of *note os.makedirs(): 3bd. no longer affects the
file permission bits of newly-created intermediate-level directories.
(Contributed by Serhiy Storchaka in bpo-19930(5).)
*note os.dup2(): 3be. now returns the new file descriptor. Previously,
‘None’ was always returned. (Contributed by Benjamin Peterson in
bpo-32441(6).)
The structure returned by *note os.stat(): 1f1. now contains the *note
st_fstype: 3bf. attribute on Solaris and its derivatives. (Contributed
by Jesús Cea Avión in bpo-32659(7).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28682
(2) https://bugs.python.org/issue25996
(3) https://bugs.python.org/issue16500
(4) https://bugs.python.org/issue31368
(5) https://bugs.python.org/issue19930
(6) https://bugs.python.org/issue32441
(7) https://bugs.python.org/issue32659
File: python.info, Node: pathlib<2>, Next: pdb, Prev: os<2>, Up: Improved Modules<2>
1.2.5.34 pathlib
................
The new *note Path.is_mount(): 205. method is now available on POSIX
systems and can be used to determine whether a path is a mount point.
(Contributed by Cooper Ry Lees in bpo-30897(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30897
File: python.info, Node: pdb, Next: py_compile<2>, Prev: pathlib<2>, Up: Improved Modules<2>
1.2.5.35 pdb
............
*note pdb.set_trace(): 3c2. now takes an optional `header' keyword-only
argument. If given, it is printed to the console just before debugging
begins. (Contributed by Barry Warsaw in bpo-31389(1).)
*note pdb: c9. command line now accepts ‘-m module_name’ as an
alternative to script file. (Contributed by Mario Corchero in
bpo-32206(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31389
(2) https://bugs.python.org/issue32206
File: python.info, Node: py_compile<2>, Next: pydoc, Prev: pdb, Up: Improved Modules<2>
1.2.5.36 py_compile
...................
*note py_compile.compile(): 215. – and by extension, *note compileall:
21. – now respects the ‘SOURCE_DATE_EPOCH’ environment variable by
unconditionally creating ‘.pyc’ files for hash-based validation. This
allows for guaranteeing reproducible builds(1) of ‘.pyc’ files when they
are created eagerly. (Contributed by Bernhard M. Wiedemann in
bpo-29708(2).)
---------- Footnotes ----------
(1) https://reproducible-builds.org/
(2) https://bugs.python.org/issue29708
File: python.info, Node: pydoc, Next: queue, Prev: py_compile<2>, Up: Improved Modules<2>
1.2.5.37 pydoc
..............
The pydoc server can now bind to an arbitrary hostname specified by the
new ‘-n’ command-line argument. (Contributed by Feanil Patel in
bpo-31128(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31128
File: python.info, Node: queue, Next: re, Prev: pydoc, Up: Improved Modules<2>
1.2.5.38 queue
..............
The new *note SimpleQueue: 3c6. class is an unbounded FIFO (last-in,
first-out) queue. (Contributed by Antoine Pitrou in bpo-14976(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14976
File: python.info, Node: re, Next: signal, Prev: queue, Up: Improved Modules<2>
1.2.5.39 re
...........
The flags *note re.ASCII: 3c8, *note re.LOCALE: 3c9. and ‘re.UNICODE’
can be set within the scope of a group. (Contributed by Serhiy
Storchaka in bpo-31690(1).)
*note re.split(): 3ca. now supports splitting on a pattern like ‘r'\b'’,
‘'^$'’ or ‘(?=-)’ that matches an empty string. (Contributed by Serhiy
Storchaka in bpo-25054(2).)
Regular expressions compiled with the *note re.LOCALE: 3c9. flag no
longer depend on the locale at compile time. Locale settings are
applied only when the compiled regular expression is used. (Contributed
by Serhiy Storchaka in bpo-30215(3).)
*note FutureWarning: 325. is now emitted if a regular expression
contains character set constructs that will change semantically in the
future, such as nested sets and set operations. (Contributed by Serhiy
Storchaka in bpo-30349(4).)
Compiled regular expression and match objects can now be copied using
*note copy.copy(): 3cb. and *note copy.deepcopy(): 3cc. (Contributed by
Serhiy Storchaka in bpo-10076(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31690
(2) https://bugs.python.org/issue25054
(3) https://bugs.python.org/issue30215
(4) https://bugs.python.org/issue30349
(5) https://bugs.python.org/issue10076
File: python.info, Node: signal, Next: socket<2>, Prev: re, Up: Improved Modules<2>
1.2.5.40 signal
...............
The new `warn_on_full_buffer' argument to the *note
signal.set_wakeup_fd(): 3ce. function makes it possible to specify
whether Python prints a warning on stderr when the wakeup buffer
overflows. (Contributed by Nathaniel J. Smith in bpo-30050(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30050
File: python.info, Node: socket<2>, Next: socketserver, Prev: signal, Up: Improved Modules<2>
1.2.5.41 socket
...............
The new *note socket.getblocking(): 3d0. method returns ‘True’ if the
socket is in blocking mode and ‘False’ otherwise. (Contributed by Yury
Selivanov in bpo-32373(1).)
The new *note socket.close(): 3d1. function closes the passed socket
file descriptor. This function should be used instead of *note
os.close(): 3d2. for better compatibility across platforms.
(Contributed by Christian Heimes in bpo-32454(2).)
The *note socket: ef. module now exposes the ‘socket.TCP_CONGESTION’
(Linux 2.6.13), ‘socket.TCP_USER_TIMEOUT’ (Linux 2.6.37), and
‘socket.TCP_NOTSENT_LOWAT’ (Linux 3.12) constants. (Contributed by Omar
Sandoval in bpo-26273(3) and Nathaniel J. Smith in bpo-29728(4).)
Support for *note socket.AF_VSOCK: 3d3. sockets has been added to allow
communication between virtual machines and their hosts. (Contributed by
Cathy Avery in bpo-27584(5).)
Sockets now auto-detect family, type and protocol from file descriptor
by default. (Contributed by Christian Heimes in bpo-28134(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32373
(2) https://bugs.python.org/issue32454
(3) https://bugs.python.org/issue26273
(4) https://bugs.python.org/issue29728
(5) https://bugs.python.org/issue27584
(6) https://bugs.python.org/issue28134
File: python.info, Node: socketserver, Next: sqlite3, Prev: socket<2>, Up: Improved Modules<2>
1.2.5.42 socketserver
.....................
‘socketserver.ThreadingMixIn.server_close()’ now waits until all
non-daemon threads complete. ‘socketserver.ForkingMixIn.server_close()’
now waits until all child processes complete.
Add a new ‘socketserver.ForkingMixIn.block_on_close’ class attribute to
*note socketserver.ForkingMixIn: 3d5. and *note
socketserver.ThreadingMixIn: 3d6. classes. Set the class attribute to
‘False’ to get the pre-3.7 behaviour.
File: python.info, Node: sqlite3, Next: ssl<2>, Prev: socketserver, Up: Improved Modules<2>
1.2.5.43 sqlite3
................
*note sqlite3.Connection: 3d8. now exposes the *note backup(): 3d9.
method when the underlying SQLite library is at version 3.6.11 or
higher. (Contributed by Lele Gaifax in bpo-27645(1).)
The `database' argument of *note sqlite3.connect(): 3da. now accepts any
*note path-like object: 36a, instead of just a string. (Contributed by
Anders Lorentsen in bpo-31843(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27645
(2) https://bugs.python.org/issue31843
File: python.info, Node: ssl<2>, Next: string, Prev: sqlite3, Up: Improved Modules<2>
1.2.5.44 ssl
............
The *note ssl: f3. module now uses OpenSSL’s builtin API instead of
*note match_hostname(): 3dc. to check a host name or an IP address.
Values are validated during TLS handshake. Any certificate validation
error including failing the host name check now raises *note
SSLCertVerificationError: 3dd. and aborts the handshake with a proper
TLS Alert message. The new exception contains additional information.
Host name validation can be customized with *note
SSLContext.hostname_checks_common_name: 3de. (Contributed by Christian
Heimes in bpo-31399(1).)
Note: The improved host name check requires a `libssl'
implementation compatible with OpenSSL 1.0.2 or 1.1. Consequently,
OpenSSL 0.9.8 and 1.0.1 are no longer supported (see *note Platform
Support Removals: 3df. for more details). The ssl module is mostly
compatible with LibreSSL 2.7.2 and newer.
The ‘ssl’ module no longer sends IP addresses in SNI TLS extension.
(Contributed by Christian Heimes in bpo-32185(2).)
*note match_hostname(): 3dc. no longer supports partial wildcards like
‘www*.example.org’. (Contributed by Mandeep Singh in bpo-23033(3) and
Christian Heimes in bpo-31399(4).)
The default cipher suite selection of the ‘ssl’ module now uses a
blacklist approach rather than a hard-coded whitelist. Python no longer
re-enables ciphers that have been blocked by OpenSSL security updates.
Default cipher suite selection can be configured at compile time.
(Contributed by Christian Heimes in bpo-31429(5).)
Validation of server certificates containing internationalized domain
names (IDNs) is now supported. As part of this change, the *note
SSLSocket.server_hostname: 3e0. attribute now stores the expected
hostname in A-label form (‘"xn--pythn-mua.org"’), rather than the
U-label form (‘"pythön.org"’). (Contributed by Nathaniel J. Smith and
Christian Heimes in bpo-28414(6).)
The ‘ssl’ module has preliminary and experimental support for TLS 1.3
and OpenSSL 1.1.1. At the time of Python 3.7.0 release, OpenSSL 1.1.1
is still under development and TLS 1.3 hasn’t been finalized yet. The
TLS 1.3 handshake and protocol behaves slightly differently than TLS 1.2
and earlier, see *note TLS 1.3: 3e1. (Contributed by Christian Heimes
in bpo-32947(7), bpo-20995(8), bpo-29136(9), bpo-30622(10) and
bpo-33618(11))
*note SSLSocket: 3e2. and *note SSLObject: 3e3. no longer have a public
constructor. Direct instantiation was never a documented and supported
feature. Instances must be created with *note SSLContext: 3e4. methods
*note wrap_socket(): 3e5. and *note wrap_bio(): 3e6. (Contributed by
Christian Heimes in bpo-32951(12))
OpenSSL 1.1 APIs for setting the minimum and maximum TLS protocol
version are available as *note SSLContext.minimum_version: 3e7. and
*note SSLContext.maximum_version: 3e8. Supported protocols are
indicated by several new flags, such as *note HAS_TLSv1_1: 3e9.
(Contributed by Christian Heimes in bpo-32609(13).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31399
(2) https://bugs.python.org/issue32185
(3) https://bugs.python.org/issue23033
(4) https://bugs.python.org/issue31399
(5) https://bugs.python.org/issue31429
(6) https://bugs.python.org/issue28414
(7) https://bugs.python.org/issue32947
(8) https://bugs.python.org/issue20995
(9) https://bugs.python.org/issue29136
(10) https://bugs.python.org/issue30622
(11) https://bugs.python.org/issue33618
(12) https://bugs.python.org/issue32951
(13) https://bugs.python.org/issue32609
File: python.info, Node: string, Next: subprocess, Prev: ssl<2>, Up: Improved Modules<2>
1.2.5.45 string
...............
*note string.Template: 3eb. now lets you to optionally modify the
regular expression pattern for braced placeholders and non-braced
placeholders separately. (Contributed by Barry Warsaw in
bpo-1198569(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1198569
File: python.info, Node: subprocess, Next: sys<2>, Prev: string, Up: Improved Modules<2>
1.2.5.46 subprocess
...................
The *note subprocess.run(): 3ed. function accepts the new
`capture_output' keyword argument. When true, stdout and stderr will be
captured. This is equivalent to passing *note subprocess.PIPE: 3ee. as
`stdout' and `stderr' arguments. (Contributed by Bo Bayles in
bpo-32102(1).)
The ‘subprocess.run’ function and the *note subprocess.Popen: 2b9.
constructor now accept the `text' keyword argument as an alias to
`universal_newlines'. (Contributed by Andrew Clegg in bpo-31756(2).)
On Windows the default for `close_fds' was changed from ‘False’ to
‘True’ when redirecting the standard handles. It’s now possible to set
`close_fds' to true when redirecting the standard handles. See *note
subprocess.Popen: 2b9. This means that `close_fds' now defaults to
‘True’ on all supported platforms. (Contributed by Segev Finer in
bpo-19764(3).)
The subprocess module is now more graceful when handling *note
KeyboardInterrupt: 197. during *note subprocess.call(): 3ef, *note
subprocess.run(): 3ed, or in a *note Popen: 2b9. context manager. It
now waits a short amount of time for the child to exit, before
continuing the handling of the ‘KeyboardInterrupt’ exception.
(Contributed by Gregory P. Smith in bpo-25942(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32102
(2) https://bugs.python.org/issue31756
(3) https://bugs.python.org/issue19764
(4) https://bugs.python.org/issue25942
File: python.info, Node: sys<2>, Next: time<2>, Prev: subprocess, Up: Improved Modules<2>
1.2.5.47 sys
............
The new *note sys.breakpointhook(): 313. hook function is called by the
built-in *note breakpoint(): 2f9. (Contributed by Barry Warsaw in
bpo-31353(1).)
On Android, the new *note sys.getandroidapilevel(): 3f1. returns the
build-time Android API version. (Contributed by Victor Stinner in
bpo-28740(2).)
The new *note sys.get_coroutine_origin_tracking_depth(): 3f2. function
returns the current coroutine origin tracking depth, as set by the new
*note sys.set_coroutine_origin_tracking_depth(): 3f3. *note asyncio: a.
has been converted to use this new API instead of the deprecated
‘sys.set_coroutine_wrapper()’. (Contributed by Nathaniel J. Smith in
bpo-32591(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31353
(2) https://bugs.python.org/issue28740
(3) https://bugs.python.org/issue32591
File: python.info, Node: time<2>, Next: tkinter<2>, Prev: sys<2>, Up: Improved Modules<2>
1.2.5.48 time
.............
PEP 564(1) adds six new functions with nanosecond resolution to the
*note time: 10a. module:
* *note time.clock_gettime_ns(): 31e.
* *note time.clock_settime_ns(): 31f.
* *note time.monotonic_ns(): 320.
* *note time.perf_counter_ns(): 321.
* *note time.process_time_ns(): 322.
* *note time.time_ns(): 323.
New clock identifiers have been added:
* *note time.CLOCK_BOOTTIME: 3f5. (Linux): Identical to *note
time.CLOCK_MONOTONIC: 3f6, except it also includes any time that
the system is suspended.
* *note time.CLOCK_PROF: 3f7. (FreeBSD, NetBSD and OpenBSD):
High-resolution per-process CPU timer.
* *note time.CLOCK_UPTIME: 3f8. (FreeBSD, OpenBSD): Time whose
absolute value is the time the system has been running and not
suspended, providing accurate uptime measurement.
The new *note time.thread_time(): 3f9. and *note time.thread_time_ns():
3fa. functions can be used to get per-thread CPU time measurements.
(Contributed by Antoine Pitrou in bpo-32025(2).)
The new *note time.pthread_getcpuclockid(): 3fb. function returns the
clock ID of the thread-specific CPU-time clock.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0564
(2) https://bugs.python.org/issue32025
File: python.info, Node: tkinter<2>, Next: tracemalloc, Prev: time<2>, Up: Improved Modules<2>
1.2.5.49 tkinter
................
The new *note tkinter.ttk.Spinbox: 3fd. class is now available.
(Contributed by Alan Moore in bpo-32585(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32585
File: python.info, Node: tracemalloc, Next: types, Prev: tkinter<2>, Up: Improved Modules<2>
1.2.5.50 tracemalloc
....................
*note tracemalloc.Traceback: 3ff. behaves more like regular tracebacks,
sorting the frames from oldest to most recent. *note
Traceback.format(): 400. now accepts negative `limit', truncating the
result to the ‘abs(limit)’ oldest frames. To get the old behaviour, use
the new `most_recent_first' argument to ‘Traceback.format()’.
(Contributed by Jesse Bakker in bpo-32121(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32121
File: python.info, Node: types, Next: unicodedata<2>, Prev: tracemalloc, Up: Improved Modules<2>
1.2.5.51 types
..............
The new *note WrapperDescriptorType: 402, *note MethodWrapperType: 403,
*note MethodDescriptorType: 404, and *note ClassMethodDescriptorType:
405. classes are now available. (Contributed by Manuel Krebber and
Guido van Rossum in bpo-29377(1), and Serhiy Storchaka in bpo-32265(2).)
The new *note types.resolve_bases(): 406. function resolves MRO entries
dynamically as specified by PEP 560(3). (Contributed by Ivan Levkivskyi
in bpo-32717(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue29377
(2) https://bugs.python.org/issue32265
(3) https://www.python.org/dev/peps/pep-0560
(4) https://bugs.python.org/issue32717
File: python.info, Node: unicodedata<2>, Next: unittest<2>, Prev: types, Up: Improved Modules<2>
1.2.5.52 unicodedata
....................
The internal *note unicodedata: 11a. database has been upgraded to use
Unicode 11(1). (Contributed by Benjamin Peterson.)
---------- Footnotes ----------
(1) http://www.unicode.org/versions/Unicode11.0.0/
File: python.info, Node: unittest<2>, Next: unittest mock, Prev: unicodedata<2>, Up: Improved Modules<2>
1.2.5.53 unittest
.................
The new ‘-k’ command-line option allows filtering tests by a name
substring or a Unix shell-like pattern. For example, ‘python -m
unittest -k foo’ runs ‘foo_tests.SomeTest.test_something’,
‘bar_tests.SomeTest.test_foo’, but not
‘bar_tests.FooTest.test_something’. (Contributed by Jonas Haag in
bpo-32071(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32071
File: python.info, Node: unittest mock, Next: urllib parse, Prev: unittest<2>, Up: Improved Modules<2>
1.2.5.54 unittest.mock
......................
The *note sentinel: 40a. attributes now preserve their identity when
they are *note copied: 26. or *note pickled: ca. (Contributed by Serhiy
Storchaka in bpo-20804(1).)
The new *note seal(): 40b. function allows sealing *note Mock: 249.
instances, which will disallow further creation of attribute mocks. The
seal is applied recursively to all attributes that are themselves mocks.
(Contributed by Mario Corchero in bpo-30541(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20804
(2) https://bugs.python.org/issue30541
File: python.info, Node: urllib parse, Next: uu, Prev: unittest mock, Up: Improved Modules<2>
1.2.5.55 urllib.parse
.....................
*note urllib.parse.quote(): 40d. has been updated from RFC 2396(1) to
RFC 3986(2), adding ‘~’ to the set of characters that are never quoted
by default. (Contributed by Christian Theune and Ratnadeep Debnath in
bpo-16285(3).)
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc2396.html
(2) https://tools.ietf.org/html/rfc3986.html
(3) https://bugs.python.org/issue16285
File: python.info, Node: uu, Next: uuid, Prev: urllib parse, Up: Improved Modules<2>
1.2.5.56 uu
...........
The *note uu.encode(): 40f. function now accepts an optional `backtick'
keyword argument. When it’s true, zeros are represented by ‘'`'’
instead of spaces. (Contributed by Xiang Zhang in bpo-30103(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30103
File: python.info, Node: uuid, Next: warnings, Prev: uu, Up: Improved Modules<2>
1.2.5.57 uuid
.............
The new *note UUID.is_safe: 411. attribute relays information from the
platform about whether generated UUIDs are generated with a
multiprocessing-safe method. (Contributed by Barry Warsaw in
bpo-22807(1).)
*note uuid.getnode(): 412. now prefers universally administered MAC
addresses over locally administered MAC addresses. This makes a better
guarantee for global uniqueness of UUIDs returned from *note
uuid.uuid1(): 413. If only locally administered MAC addresses are
available, the first such one found is returned. (Contributed by Barry
Warsaw in bpo-32107(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22807
(2) https://bugs.python.org/issue32107
File: python.info, Node: warnings, Next: xml etree, Prev: uuid, Up: Improved Modules<2>
1.2.5.58 warnings
.................
The initialization of the default warnings filters has changed as
follows:
* warnings enabled via command line options (including those for
*note -b: 415. and the new CPython-specific *note -X: 155. ‘dev’
option) are always passed to the warnings machinery via the *note
sys.warnoptions: 416. attribute.
* warnings filters enabled via the command line or the environment
now have the following order of precedence:
* the ‘BytesWarning’ filter for *note -b: 415. (or ‘-bb’)
* any filters specified with the *note -W: 417. option
* any filters specified with the *note PYTHONWARNINGS: 418.
environment variable
* any other CPython specific filters (e.g. the ‘default’
filter added for the new ‘-X dev’ mode)
* any implicit filters defined directly by the warnings
machinery
* in CPython debug builds, all warnings are now displayed by default
(the implicit filter list is empty)
(Contributed by Nick Coghlan and Victor Stinner in bpo-20361(1),
bpo-32043(2), and bpo-32230(3).)
Deprecation warnings are once again shown by default in single-file
scripts and at the interactive prompt. See *note PEP 565; Show
DeprecationWarning in __main__: 303. for details. (Contributed by Nick
Coghlan in bpo-31975(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20361
(2) https://bugs.python.org/issue32043
(3) https://bugs.python.org/issue32230
(4) https://bugs.python.org/issue31975
File: python.info, Node: xml etree, Next: xmlrpc server, Prev: warnings, Up: Improved Modules<2>
1.2.5.59 xml.etree
..................
*note ElementPath: 41a. predicates in the ‘find()’ methods can now
compare text of the current node with ‘[. = "text"]’, not only text in
children. Predicates also allow adding spaces for better readability.
(Contributed by Stefan Behnel in bpo-31648(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31648
File: python.info, Node: xmlrpc server, Next: zipapp, Prev: xml etree, Up: Improved Modules<2>
1.2.5.60 xmlrpc.server
......................
‘SimpleXMLRPCDispatcher.register_function’ can now be used as a
decorator. (Contributed by Xiang Zhang in bpo-7769(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue7769
File: python.info, Node: zipapp, Next: zipfile, Prev: xmlrpc server, Up: Improved Modules<2>
1.2.5.61 zipapp
...............
Function *note create_archive(): 41d. now accepts an optional `filter'
argument to allow the user to select which files should be included in
the archive. (Contributed by Irmen de Jong in bpo-31072(1).)
Function *note create_archive(): 41d. now accepts an optional
`compressed' argument to generate a compressed archive. A command line
option ‘--compress’ has also been added to support compression.
(Contributed by Zhiming Wang in bpo-31638(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31072
(2) https://bugs.python.org/issue31638
File: python.info, Node: zipfile, Prev: zipapp, Up: Improved Modules<2>
1.2.5.62 zipfile
................
*note ZipFile: 41f. now accepts the new `compresslevel' parameter to
control the compression level. (Contributed by Bo Bayles in
bpo-21417(1).)
Subdirectories in archives created by ‘ZipFile’ are now stored in
alphabetical order. (Contributed by Bernhard M. Wiedemann in
bpo-30693(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21417
(2) https://bugs.python.org/issue30693
File: python.info, Node: C API Changes, Next: Build Changes, Prev: Improved Modules<2>, Up: What’s New In Python 3 7
1.2.6 C API Changes
-------------------
A new API for thread-local storage has been implemented. See *note PEP
539; New C API for Thread-Local Storage: 304. for an overview and *note
Thread Specific Storage (TSS) API: 317. for a complete reference.
(Contributed by Masayuki Yamamoto in bpo-25658(1).)
The new *note context variables: 2f5. functionality exposes a number of
*note new C APIs: 33b.
The new *note PyImport_GetModule(): 421. function returns the previously
imported module with the given name. (Contributed by Eric Snow in
bpo-28411(2).)
The new *note Py_RETURN_RICHCOMPARE: 422. macro eases writing rich
comparison functions. (Contributed by Petr Victorin in bpo-23699(3).)
The new *note Py_UNREACHABLE: 423. macro can be used to mark unreachable
code paths. (Contributed by Barry Warsaw in bpo-31338(4).)
The *note tracemalloc: 114. now exposes a C API through the new *note
PyTraceMalloc_Track(): 424. and *note PyTraceMalloc_Untrack(): 425.
functions. (Contributed by Victor Stinner in bpo-30054(5).)
The new ‘import__find__load__start()’ and ‘import__find__load__done()’
static markers can be used to trace module imports. (Contributed by
Christian Heimes in bpo-31574(6).)
The fields ‘name’ and ‘doc’ of structures *note PyMemberDef: 426, *note
PyGetSetDef: 427, *note PyStructSequence_Field: 428, *note
PyStructSequence_Desc: 429, and ‘wrapperbase’ are now of type ‘const
char *’ rather of ‘char *’. (Contributed by Serhiy Storchaka in
bpo-28761(7).)
The result of *note PyUnicode_AsUTF8AndSize(): 42a. and *note
PyUnicode_AsUTF8(): 42b. is now of type ‘const char *’ rather of ‘char
*’. (Contributed by Serhiy Storchaka in bpo-28769(8).)
The result of *note PyMapping_Keys(): 42c, *note PyMapping_Values():
42d. and *note PyMapping_Items(): 42e. is now always a list, rather than
a list or a tuple. (Contributed by Oren Milman in bpo-28280(9).)
Added functions *note PySlice_Unpack(): 42f. and *note
PySlice_AdjustIndices(): 430. (Contributed by Serhiy Storchaka in
bpo-27867(10).)
*note PyOS_AfterFork(): 431. is deprecated in favour of the new
functions *note PyOS_BeforeFork(): 432, *note PyOS_AfterFork_Parent():
433. and *note PyOS_AfterFork_Child(): 2cd. (Contributed by Antoine
Pitrou in bpo-16500(11).)
The ‘PyExc_RecursionErrorInst’ singleton that was part of the public API
has been removed as its members being never cleared may cause a segfault
during finalization of the interpreter. Contributed by Xavier de Gaye
in bpo-22898(12) and bpo-30697(13).
Added C API support for timezones with timezone constructors *note
PyTimeZone_FromOffset(): 434. and *note PyTimeZone_FromOffsetAndName():
435, and access to the UTC singleton with *note PyDateTime_TimeZone_UTC:
436. Contributed by Paul Ganssle in bpo-10381(14).
The type of results of ‘PyThread_start_new_thread()’ and
‘PyThread_get_thread_ident()’, and the `id' parameter of *note
PyThreadState_SetAsyncExc(): 437. changed from ‘long’ to ‘unsigned
long’. (Contributed by Serhiy Storchaka in bpo-6532(15).)
*note PyUnicode_AsWideCharString(): 438. now raises a *note ValueError:
1fb. if the second argument is ‘NULL’ and the ‘wchar_t*’ string contains
null characters. (Contributed by Serhiy Storchaka in bpo-30708(16).)
Changes to the startup sequence and the management of dynamic memory
allocators mean that the long documented requirement to call *note
Py_Initialize(): 439. before calling most C API functions is now relied
on more heavily, and failing to abide by it may lead to segfaults in
embedding applications. See the *note Porting to Python 3.7: 307.
section in this document and the *note Before Python Initialization:
43a. section in the C API documentation for more details.
The new *note PyInterpreterState_GetID(): 43b. returns the unique ID for
a given interpreter. (Contributed by Eric Snow in bpo-29102(17).)
*note Py_DecodeLocale(): 43c, *note Py_EncodeLocale(): 43d. now use the
UTF-8 encoding when the *note UTF-8 mode: 300. is enabled. (Contributed
by Victor Stinner in bpo-29240(18).)
*note PyUnicode_DecodeLocaleAndSize(): 43e. and *note
PyUnicode_EncodeLocale(): 43f. now use the current locale encoding for
‘surrogateescape’ error handler. (Contributed by Victor Stinner in
bpo-29240(19).)
The `start' and `end' parameters of *note PyUnicode_FindChar(): 440. are
now adjusted to behave like string slices. (Contributed by Xiang Zhang
in bpo-28822(20).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25658
(2) https://bugs.python.org/issue28411
(3) https://bugs.python.org/issue23699
(4) https://bugs.python.org/issue31338
(5) https://bugs.python.org/issue30054
(6) https://bugs.python.org/issue31574
(7) https://bugs.python.org/issue28761
(8) https://bugs.python.org/issue28769
(9) https://bugs.python.org/issue28280
(10) https://bugs.python.org/issue27867
(11) https://bugs.python.org/issue16500
(12) https://bugs.python.org/issue22898
(13) https://bugs.python.org/issue30697
(14) https://bugs.python.org/issue10381
(15) https://bugs.python.org/issue6532
(16) https://bugs.python.org/issue30708
(17) https://bugs.python.org/issue29102
(18) https://bugs.python.org/issue29240
(19) https://bugs.python.org/issue29240
(20) https://bugs.python.org/issue28822
File: python.info, Node: Build Changes, Next: Optimizations<2>, Prev: C API Changes, Up: What’s New In Python 3 7
1.2.7 Build Changes
-------------------
Support for building ‘--without-threads’ has been removed. The *note
threading: 109. module is now always available. (Contributed by Antoine
Pitrou in bpo-31370(1).).
A full copy of libffi is no longer bundled for use when building the
*note _ctypes: 2b. module on non-OSX UNIX platforms. An installed copy
of libffi is now required when building ‘_ctypes’ on such platforms.
(Contributed by Zachary Ware in bpo-27979(2).)
The Windows build process no longer depends on Subversion to pull in
external sources, a Python script is used to download zipfiles from
GitHub instead. If Python 3.6 is not found on the system (via ‘py
-3.6’), NuGet is used to download a copy of 32-bit Python for this
purpose. (Contributed by Zachary Ware in bpo-30450(3).)
The *note ssl: f3. module requires OpenSSL 1.0.2 or 1.1 compatible
libssl. OpenSSL 1.0.1 has reached end of lifetime on 2016-12-31 and is
no longer supported. LibreSSL is temporarily not supported as well.
LibreSSL releases up to version 2.6.4 are missing required OpenSSL 1.0.2
APIs.
---------- Footnotes ----------
(1) https://bugs.python.org/issue31370
(2) https://bugs.python.org/issue27979
(3) https://bugs.python.org/issue30450
File: python.info, Node: Optimizations<2>, Next: Other CPython Implementation Changes, Prev: Build Changes, Up: What’s New In Python 3 7
1.2.8 Optimizations
-------------------
The overhead of calling many methods of various standard library classes
implemented in C has been significantly reduced by porting more code to
use the ‘METH_FASTCALL’ convention. (Contributed by Victor Stinner in
bpo-29300(1), bpo-29507(2), bpo-29452(3), and bpo-29286(4).)
Various optimizations have reduced Python startup time by 10% on Linux
and up to 30% on macOS. (Contributed by Victor Stinner, INADA Naoki in
bpo-29585(5), and Ivan Levkivskyi in bpo-31333(6).)
Method calls are now up to 20% faster due to the bytecode changes which
avoid creating bound method instances. (Contributed by Yury Selivanov
and INADA Naoki in bpo-26110(7).) The *note asyncio: a. module received
a number of notable optimizations for commonly used functions:
* The *note asyncio.get_event_loop(): 355. function has been
reimplemented in C to make it up to 15 times faster. (Contributed
by Yury Selivanov in bpo-32296(8).)
* *note asyncio.Future: 443. callback management has been optimized.
(Contributed by Yury Selivanov in bpo-32348(9).)
* *note asyncio.gather(): 286. is now up to 15% faster. (Contributed
by Yury Selivanov in bpo-32355(10).)
* *note asyncio.sleep(): 285. is now up to 2 times faster when the
`delay' argument is zero or negative. (Contributed by Andrew
Svetlov in bpo-32351(11).)
* The performance overhead of asyncio debug mode has been reduced.
(Contributed by Antoine Pitrou in bpo-31970(12).)
As a result of *note PEP 560 work: 2fb, the import time of *note typing:
119. has been reduced by a factor of 7, and many typing operations are
now faster. (Contributed by Ivan Levkivskyi in bpo-32226(13).)
*note sorted(): 444. and *note list.sort(): 445. have been optimized for
common cases to be up to 40-75% faster. (Contributed by Elliot
Gorokhovsky in bpo-28685(14).)
*note dict.copy(): 446. is now up to 5.5 times faster. (Contributed by
Yury Selivanov in bpo-31179(15).)
*note hasattr(): 447. and *note getattr(): 448. are now about 4 times
faster when `name' is not found and `obj' does not override *note
object.__getattr__(): 31a. or *note object.__getattribute__(): 449.
(Contributed by INADA Naoki in bpo-32544(16).)
Searching for certain Unicode characters (like Ukrainian capital “Є”) in
a string was up to 25 times slower than searching for other characters.
It is now only 3 times slower in the worst case. (Contributed by Serhiy
Storchaka in bpo-24821(17).)
The *note collections.namedtuple(): 1b7. factory has been reimplemented
to make the creation of named tuples 4 to 6 times faster. (Contributed
by Jelle Zijlstra with further improvements by INADA Naoki, Serhiy
Storchaka, and Raymond Hettinger in bpo-28638(18).)
‘date.fromordinal()’ and ‘date.fromtimestamp()’ are now up to 30% faster
in the common case. (Contributed by Paul Ganssle in bpo-32403(19).)
The *note os.fwalk(): 3b4. function is now up to 2 times faster thanks
to the use of *note os.scandir(): 25f. (Contributed by Serhiy Storchaka
in bpo-25996(20).)
The speed of the *note shutil.rmtree(): 21c. function has been improved
by 20–40% thanks to the use of the *note os.scandir(): 25f. function.
(Contributed by Serhiy Storchaka in bpo-28564(21).)
Optimized case-insensitive matching and searching of *note regular
expressions: dd. Searching some patterns can now be up to 20 times
faster. (Contributed by Serhiy Storchaka in bpo-30285(22).)
*note re.compile(): 44a. now converts ‘flags’ parameter to int object if
it is ‘RegexFlag’. It is now as fast as Python 3.5, and faster than
Python 3.6 by about 10% depending on the pattern. (Contributed by INADA
Naoki in bpo-31671(23).)
The *note modify(): 44b. methods of classes *note
selectors.EpollSelector: 44c, *note selectors.PollSelector: 44d. and
*note selectors.DevpollSelector: 44e. may be around 10% faster under
heavy loads. (Contributed by Giampaolo Rodola’ in bpo-30014(24))
Constant folding has been moved from the peephole optimizer to the new
AST optimizer, which is able perform optimizations more consistently.
(Contributed by Eugene Toder and INADA Naoki in bpo-29469(25) and
bpo-11549(26).)
Most functions and methods in *note abc: 4. have been rewritten in C.
This makes creation of abstract base classes, and calling *note
isinstance(): 44f. and *note issubclass(): 450. on them 1.5x faster.
This also reduces Python start-up time by up to 10%. (Contributed by
Ivan Levkivskyi and INADA Naoki in bpo-31333(27))
Significant speed improvements to alternate constructors for *note
datetime.date: 193. and *note datetime.datetime: 194. by using fast-path
constructors when not constructing subclasses. (Contributed by Paul
Ganssle in bpo-32403(28))
The speed of comparison of *note array.array: 451. instances has been
improved considerably in certain cases. It is now from 10x to 70x
faster when comparing arrays holding values of the same integer type.
(Contributed by Adrian Wielgosik in bpo-24700(29).)
The *note math.erf(): 452. and *note math.erfc(): 453. functions now use
the (faster) C library implementation on most platforms. (Contributed
by Serhiy Storchaka in bpo-26121(30).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue29300
(2) https://bugs.python.org/issue29507
(3) https://bugs.python.org/issue29452
(4) https://bugs.python.org/issue29286
(5) https://bugs.python.org/issue29585
(6) https://bugs.python.org/issue31333
(7) https://bugs.python.org/issue26110
(8) https://bugs.python.org/issue32296
(9) https://bugs.python.org/issue32348
(10) https://bugs.python.org/issue32355
(11) https://bugs.python.org/issue32351
(12) https://bugs.python.org/issue31970
(13) https://bugs.python.org/issue32226
(14) https://bugs.python.org/issue28685
(15) https://bugs.python.org/issue31179
(16) https://bugs.python.org/issue32544
(17) https://bugs.python.org/issue24821
(18) https://bugs.python.org/issue28638
(19) https://bugs.python.org/issue32403
(20) https://bugs.python.org/issue25996
(21) https://bugs.python.org/issue28564
(22) https://bugs.python.org/issue30285
(23) https://bugs.python.org/issue31671
(24) https://bugs.python.org/issue30014
(25) https://bugs.python.org/issue29469
(26) https://bugs.python.org/issue11549
(27) https://bugs.python.org/issue31333
(28) https://bugs.python.org/issue32403
(29) https://bugs.python.org/issue24700
(30) https://bugs.python.org/issue26121
File: python.info, Node: Other CPython Implementation Changes, Next: Deprecated Python Behavior, Prev: Optimizations<2>, Up: What’s New In Python 3 7
1.2.9 Other CPython Implementation Changes
------------------------------------------
* Trace hooks may now opt out of receiving the ‘line’ and opt into
receiving the ‘opcode’ events from the interpreter by setting the
corresponding new ‘f_trace_lines’ and ‘f_trace_opcodes’ attributes
on the frame being traced. (Contributed by Nick Coghlan in
bpo-31344(1).)
* Fixed some consistency problems with namespace package module
attributes. Namespace module objects now have an ‘__file__’ that
is set to ‘None’ (previously unset), and their ‘__spec__.origin’ is
also set to ‘None’ (previously the string ‘"namespace"’). See
bpo-32305(2). Also, the namespace module object’s
‘__spec__.loader’ is set to the same value as ‘__loader__’
(previously, the former was set to ‘None’). See bpo-32303(3).
* The *note locals(): 455. dictionary now displays in the lexical
order that variables were defined. Previously, the order was
undefined. (Contributed by Raymond Hettinger in bpo-32690(4).)
* The *note distutils: 39. ‘upload’ command no longer tries to change
CR end-of-line characters to CRLF. This fixes a corruption issue
with sdists that ended with a byte equivalent to CR. (Contributed
by Bo Bayles in bpo-32304(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31344
(2) https://bugs.python.org/issue32305
(3) https://bugs.python.org/issue32303
(4) https://bugs.python.org/issue32690
(5) https://bugs.python.org/issue32304
File: python.info, Node: Deprecated Python Behavior, Next: Deprecated Python modules functions and methods, Prev: Other CPython Implementation Changes, Up: What’s New In Python 3 7
1.2.10 Deprecated Python Behavior
---------------------------------
Yield expressions (both ‘yield’ and ‘yield from’ clauses) are now
deprecated in comprehensions and generator expressions (aside from the
iterable expression in the leftmost ‘for’ clause). This ensures that
comprehensions always immediately return a container of the appropriate
type (rather than potentially returning a *note generator iterator: 457.
object), while generator expressions won’t attempt to interleave their
implicit output with the output from any explicit yield expressions. In
Python 3.7, such expressions emit *note DeprecationWarning: 278. when
compiled, in Python 3.8 this will be a *note SyntaxError: 458.
(Contributed by Serhiy Storchaka in bpo-10544(1).)
Returning a subclass of *note complex: 189. from *note
object.__complex__(): 18d. is deprecated and will be an error in future
Python versions. This makes ‘__complex__()’ consistent with *note
object.__int__(): 18b. and *note object.__float__(): 18c. (Contributed
by Serhiy Storchaka in bpo-28894(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10544
(2) https://bugs.python.org/issue28894
File: python.info, Node: Deprecated Python modules functions and methods, Next: Deprecated functions and types of the C API, Prev: Deprecated Python Behavior, Up: What’s New In Python 3 7
1.2.11 Deprecated Python modules, functions and methods
-------------------------------------------------------
* Menu:
* aifc::
* asyncio: asyncio<3>.
* collections: collections<3>.
* dbm: dbm<2>.
* enum: enum<2>.
* gettext: gettext<2>.
* importlib: importlib<2>.
* locale: locale<2>.
* macpath::
* threading: threading<2>.
* socket: socket<3>.
* ssl: ssl<3>.
* sunau::
* sys: sys<3>.
* wave::
File: python.info, Node: aifc, Next: asyncio<3>, Up: Deprecated Python modules functions and methods
1.2.11.1 aifc
.............
‘aifc.openfp()’ has been deprecated and will be removed in Python 3.9.
Use *note aifc.open(): 45b. instead. (Contributed by Brian Curtin in
bpo-31985(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31985
File: python.info, Node: asyncio<3>, Next: collections<3>, Prev: aifc, Up: Deprecated Python modules functions and methods
1.2.11.2 asyncio
................
Support for directly ‘await’-ing instances of *note asyncio.Lock: 28b.
and other asyncio synchronization primitives has been deprecated. An
asynchronous context manager must be used in order to acquire and
release the synchronization resource. (Contributed by Andrew Svetlov in
bpo-32253(1).)
The *note asyncio.Task.current_task(): 352. and *note
asyncio.Task.all_tasks(): 353. methods have been deprecated.
(Contributed by Andrew Svetlov in bpo-32250(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue32253
(2) https://bugs.python.org/issue32250
File: python.info, Node: collections<3>, Next: dbm<2>, Prev: asyncio<3>, Up: Deprecated Python modules functions and methods
1.2.11.3 collections
....................
In Python 3.8, the abstract base classes in *note collections.abc: 1f.
will no longer be exposed in the regular *note collections: 1e. module.
This will help create a clearer distinction between the concrete classes
and the abstract base classes. (Contributed by Serhiy Storchaka in
bpo-25988(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25988
File: python.info, Node: dbm<2>, Next: enum<2>, Prev: collections<3>, Up: Deprecated Python modules functions and methods
1.2.11.4 dbm
............
*note dbm.dumb: 33. now supports reading read-only files and no longer
writes the index file when it is not changed. A deprecation warning is
now emitted if the index file is missing and recreated in the ‘'r'’ and
‘'w'’ modes (this will be an error in future Python releases).
(Contributed by Serhiy Storchaka in bpo-28847(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28847
File: python.info, Node: enum<2>, Next: gettext<2>, Prev: dbm<2>, Up: Deprecated Python modules functions and methods
1.2.11.5 enum
.............
In Python 3.8, attempting to check for non-Enum objects in ‘Enum’
classes will raise a *note TypeError: 192. (e.g. ‘1 in Color’);
similarly, attempting to check for non-Flag objects in a ‘Flag’ member
will raise *note TypeError: 192. (e.g. ‘1 in Perm.RW’); currently, both
operations return *note False: 388. instead. (Contributed by Ethan
Furman in bpo-33217(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33217
File: python.info, Node: gettext<2>, Next: importlib<2>, Prev: enum<2>, Up: Deprecated Python modules functions and methods
1.2.11.6 gettext
................
Using non-integer value for selecting a plural form in *note gettext:
89. is now deprecated. It never correctly worked. (Contributed by
Serhiy Storchaka in bpo-28692(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28692
File: python.info, Node: importlib<2>, Next: locale<2>, Prev: gettext<2>, Up: Deprecated Python modules functions and methods
1.2.11.7 importlib
..................
Methods *note MetaPathFinder.find_module(): 462. (replaced by *note
MetaPathFinder.find_spec(): 463.) and *note
PathEntryFinder.find_loader(): 464. (replaced by *note
PathEntryFinder.find_spec(): 465.) both deprecated in Python 3.4 now
emit *note DeprecationWarning: 278. (Contributed by Matthias Bussonnier
in bpo-29576(1))
The *note importlib.abc.ResourceLoader: 466. ABC has been deprecated in
favour of *note importlib.abc.ResourceReader: 342.
---------- Footnotes ----------
(1) https://bugs.python.org/issue29576
File: python.info, Node: locale<2>, Next: macpath, Prev: importlib<2>, Up: Deprecated Python modules functions and methods
1.2.11.8 locale
...............
*note locale.format(): 468. has been deprecated, use *note
locale.format_string(): 3a4. instead. (Contributed by Garvit in
bpo-10379(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10379
File: python.info, Node: macpath, Next: threading<2>, Prev: locale<2>, Up: Deprecated Python modules functions and methods
1.2.11.9 macpath
................
The ‘macpath’ is now deprecated and will be removed in Python 3.8.
(Contributed by Chi Hsuan Yen in bpo-9850(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9850
File: python.info, Node: threading<2>, Next: socket<3>, Prev: macpath, Up: Deprecated Python modules functions and methods
1.2.11.10 threading
...................
*note dummy_threading: 68. and *note _dummy_thread: 2. have been
deprecated. It is no longer possible to build Python with threading
disabled. Use *note threading: 109. instead. (Contributed by Antoine
Pitrou in bpo-31370(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31370
File: python.info, Node: socket<3>, Next: ssl<3>, Prev: threading<2>, Up: Deprecated Python modules functions and methods
1.2.11.11 socket
................
The silent argument value truncation in *note socket.htons(): 46c. and
*note socket.ntohs(): 46d. has been deprecated. In future versions of
Python, if the passed argument is larger than 16 bits, an exception will
be raised. (Contributed by Oren Milman in bpo-28332(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28332
File: python.info, Node: ssl<3>, Next: sunau, Prev: socket<3>, Up: Deprecated Python modules functions and methods
1.2.11.12 ssl
.............
*note ssl.wrap_socket(): 46f. is deprecated. Use *note
ssl.SSLContext.wrap_socket(): 3e5. instead. (Contributed by Christian
Heimes in bpo-28124(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28124
File: python.info, Node: sunau, Next: sys<3>, Prev: ssl<3>, Up: Deprecated Python modules functions and methods
1.2.11.13 sunau
...............
*note sunau.openfp(): 471. has been deprecated and will be removed in
Python 3.9. Use *note sunau.open(): 472. instead. (Contributed by
Brian Curtin in bpo-31985(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31985
File: python.info, Node: sys<3>, Next: wave, Prev: sunau, Up: Deprecated Python modules functions and methods
1.2.11.14 sys
.............
Deprecated ‘sys.set_coroutine_wrapper()’ and
‘sys.get_coroutine_wrapper()’.
The undocumented ‘sys.callstats()’ function has been deprecated and will
be removed in a future Python version. (Contributed by Victor Stinner
in bpo-28799(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28799
File: python.info, Node: wave, Prev: sys<3>, Up: Deprecated Python modules functions and methods
1.2.11.15 wave
..............
*note wave.openfp(): 475. has been deprecated and will be removed in
Python 3.9. Use *note wave.open(): 476. instead. (Contributed by Brian
Curtin in bpo-31985(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31985
File: python.info, Node: Deprecated functions and types of the C API, Next: Platform Support Removals, Prev: Deprecated Python modules functions and methods, Up: What’s New In Python 3 7
1.2.12 Deprecated functions and types of the C API
--------------------------------------------------
Function *note PySlice_GetIndicesEx(): 478. is deprecated and replaced
with a macro if ‘Py_LIMITED_API’ is not set or set to a value in the
range between ‘0x03050400’ and ‘0x03060000’ (not inclusive), or is
‘0x03060100’ or higher. (Contributed by Serhiy Storchaka in
bpo-27867(1).)
*note PyOS_AfterFork(): 431. has been deprecated. Use *note
PyOS_BeforeFork(): 432, *note PyOS_AfterFork_Parent(): 433. or *note
PyOS_AfterFork_Child(): 2cd. instead. (Contributed by Antoine Pitrou in
bpo-16500(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27867
(2) https://bugs.python.org/issue16500
File: python.info, Node: Platform Support Removals, Next: API and Feature Removals<2>, Prev: Deprecated functions and types of the C API, Up: What’s New In Python 3 7
1.2.13 Platform Support Removals
--------------------------------
* FreeBSD 9 and older are no longer officially supported.
* For full Unicode support, including within extension modules, *nix
platforms are now expected to provide at least one of ‘C.UTF-8’
(full locale), ‘C.utf8’ (full locale) or ‘UTF-8’ (‘LC_CTYPE’-only
locale) as an alternative to the legacy ‘ASCII’-based ‘C’ locale.
* OpenSSL 0.9.8 and 1.0.1 are no longer supported, which means
building CPython 3.7 with SSL/TLS support on older platforms still
using these versions requires custom build options that link to a
more recent version of OpenSSL.
Notably, this issue affects the Debian 8 (aka “jessie”) and Ubuntu
14.04 (aka “Trusty”) LTS Linux distributions, as they still use
OpenSSL 1.0.1 by default.
Debian 9 (“stretch”) and Ubuntu 16.04 (“xenial”), as well as recent
releases of other LTS Linux releases (e.g. RHEL/CentOS 7.5, SLES
12-SP3), use OpenSSL 1.0.2 or later, and remain supported in the
default build configuration.
CPython’s own CI configuration file(1) provides an example of using
the SSL compatibility testing infrastructure(2) in CPython’s test
suite to build and link against OpenSSL 1.1.0 rather than an
outdated system provided OpenSSL.
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/.travis.yml
(2)
https://github.com/python/cpython/tree/3.8/Tools/ssl/multissltests.py
File: python.info, Node: API and Feature Removals<2>, Next: Module Removals, Prev: Platform Support Removals, Up: What’s New In Python 3 7
1.2.14 API and Feature Removals
-------------------------------
The following features and APIs have been removed from Python 3.7:
* The ‘os.stat_float_times()’ function has been removed. It was
introduced in Python 2.3 for backward compatibility with Python
2.2, and was deprecated since Python 3.1.
* Unknown escapes consisting of ‘'\'’ and an ASCII letter in
replacement templates for *note re.sub(): 47b. were deprecated in
Python 3.5, and will now cause an error.
* Removed support of the `exclude' argument in *note
tarfile.TarFile.add(): 47c. It was deprecated in Python 2.7 and
3.2. Use the `filter' argument instead.
* The ‘splitunc()’ function in the ‘ntpath’ module was deprecated in
Python 3.1, and has now been removed. Use the *note splitdrive():
47d. function instead.
* *note collections.namedtuple(): 1b7. no longer supports the
`verbose' parameter or ‘_source’ attribute which showed the
generated source code for the named tuple class. This was part of
an optimization designed to speed-up class creation. (Contributed
by Jelle Zijlstra with further improvements by INADA Naoki, Serhiy
Storchaka, and Raymond Hettinger in bpo-28638(1).)
* Functions *note bool(): 183, *note float(): 187, *note list(): 262.
and *note tuple(): 47e. no longer take keyword arguments. The
first argument of *note int(): 184. can now be passed only as
positional argument.
* Removed previously deprecated in Python 2.4 classes ‘Plist’, ‘Dict’
and ‘_InternalDict’ in the *note plistlib: cf. module. Dict values
in the result of functions *note readPlist(): 47f. and *note
readPlistFromBytes(): 480. are now normal dicts. You no longer can
use attribute access to access items of these dictionaries.
* The ‘asyncio.windows_utils.socketpair()’ function has been removed.
Use the *note socket.socketpair(): 481. function instead, it is
available on all platforms since Python 3.5.
‘asyncio.windows_utils.socketpair’ was just an alias to
‘socket.socketpair’ on Python 3.5 and newer.
* *note asyncio: a. no longer exports the *note selectors: e6. and
‘_overlapped’ modules as ‘asyncio.selectors’ and
‘asyncio._overlapped’. Replace ‘from asyncio import selectors’
with ‘import selectors’.
* Direct instantiation of *note ssl.SSLSocket: 3e2. and *note
ssl.SSLObject: 3e3. objects is now prohibited. The constructors
were never documented, tested, or designed as public constructors.
Users were supposed to use *note ssl.wrap_socket(): 46f. or *note
ssl.SSLContext: 3e4. (Contributed by Christian Heimes in
bpo-32951(2).)
* The unused *note distutils: 39. ‘install_misc’ command has been
removed. (Contributed by Eric N. Vander Weele in bpo-29218(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28638
(2) https://bugs.python.org/issue32951
(3) https://bugs.python.org/issue29218
File: python.info, Node: Module Removals, Next: Windows-only Changes, Prev: API and Feature Removals<2>, Up: What’s New In Python 3 7
1.2.15 Module Removals
----------------------
The ‘fpectl’ module has been removed. It was never enabled by default,
never worked correctly on x86-64, and it changed the Python ABI in ways
that caused unexpected breakage of C extensions. (Contributed by
Nathaniel J. Smith in bpo-29137(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue29137
File: python.info, Node: Windows-only Changes, Next: Porting to Python 3 7, Prev: Module Removals, Up: What’s New In Python 3 7
1.2.16 Windows-only Changes
---------------------------
The python launcher, (py.exe), can accept 32 & 64 bit specifiers
`without' having to specify a minor version as well. So ‘py -3-32’ and
‘py -3-64’ become valid as well as ‘py -3.7-32’, also the -`m'-64 and
-`m.n'-64 forms are now accepted to force 64 bit python even if 32 bit
would have otherwise been used. If the specified version is not
available py.exe will error exit. (Contributed by Steve Barnes in
bpo-30291(1).)
The launcher can be run as ‘py -0’ to produce a list of the installed
pythons, `with default marked with an asterisk'. Running ‘py -0p’ will
include the paths. If py is run with a version specifier that cannot be
matched it will also print the `short form' list of available
specifiers. (Contributed by Steve Barnes in bpo-30362(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue30291
(2) https://bugs.python.org/issue30362
File: python.info, Node: Porting to Python 3 7, Next: Notable changes in Python 3 7 1, Prev: Windows-only Changes, Up: What’s New In Python 3 7
1.2.17 Porting to Python 3.7
----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code.
* Menu:
* Changes in Python Behavior::
* Changes in the Python API: Changes in the Python API<2>.
* Changes in the C API: Changes in the C API<2>.
* CPython bytecode changes: CPython bytecode changes<2>.
* Windows-only Changes: Windows-only Changes<2>.
* Other CPython implementation changes::
File: python.info, Node: Changes in Python Behavior, Next: Changes in the Python API<2>, Up: Porting to Python 3 7
1.2.17.1 Changes in Python Behavior
...................................
* *note async: 1a2. and *note await: 1a3. names are now reserved
keywords. Code using these names as identifiers will now raise a
*note SyntaxError: 458. (Contributed by Jelle Zijlstra in
bpo-30406(1).)
* PEP 479(2) is enabled for all code in Python 3.7, meaning that
*note StopIteration: 486. exceptions raised directly or indirectly
in coroutines and generators are transformed into *note
RuntimeError: 2ba. exceptions. (Contributed by Yury Selivanov in
bpo-32670(3).)
* *note object.__aiter__(): 487. methods can no longer be declared as
asynchronous. (Contributed by Yury Selivanov in bpo-31709(4).)
* Due to an oversight, earlier Python versions erroneously accepted
the following syntax:
f(1 for x in [1],)
class C(1 for x in [1]):
pass
Python 3.7 now correctly raises a *note SyntaxError: 458, as a
generator expression always needs to be directly inside a set of
parentheses and cannot have a comma on either side, and the
duplication of the parentheses can be omitted only on calls.
(Contributed by Serhiy Storchaka in bpo-32012(5) and bpo-32023(6).)
* When using the *note -m: 337. switch, the initial working directory
is now added to *note sys.path: 488, rather than an empty string
(which dynamically denoted the current working directory at the
time of each import). Any programs that are checking for the empty
string, or otherwise relying on the previous behaviour, will need
to be updated accordingly (e.g. by also checking for ‘os.getcwd()’
or ‘os.path.dirname(__main__.__file__)’, depending on why the code
was checking for the empty string in the first place).
---------- Footnotes ----------
(1) https://bugs.python.org/issue30406
(2) https://www.python.org/dev/peps/pep-0479
(3) https://bugs.python.org/issue32670
(4) https://bugs.python.org/issue31709
(5) https://bugs.python.org/issue32012
(6) https://bugs.python.org/issue32023
File: python.info, Node: Changes in the Python API<2>, Next: Changes in the C API<2>, Prev: Changes in Python Behavior, Up: Porting to Python 3 7
1.2.17.2 Changes in the Python API
..................................
* ‘socketserver.ThreadingMixIn.server_close()’ now waits until all
non-daemon threads complete. Set the new
‘socketserver.ThreadingMixIn.block_on_close’ class attribute to
‘False’ to get the pre-3.7 behaviour. (Contributed by Victor
Stinner in bpo-31233(1) and bpo-33540(2).)
* ‘socketserver.ForkingMixIn.server_close()’ now waits until all
child processes complete. Set the new
‘socketserver.ForkingMixIn.block_on_close’ class attribute to
‘False’ to get the pre-3.7 behaviour. (Contributed by Victor
Stinner in bpo-31151(3) and bpo-33540(4).)
* The *note locale.localeconv(): 48a. function now temporarily sets
the ‘LC_CTYPE’ locale to the value of ‘LC_NUMERIC’ in some cases.
(Contributed by Victor Stinner in bpo-31900(5).)
* *note pkgutil.walk_packages(): 48b. now raises a *note ValueError:
1fb. if `path' is a string. Previously an empty list was returned.
(Contributed by Sanyam Khurana in bpo-24744(6).)
* A format string argument for *note string.Formatter.format(): 48c.
is now *note positional-only: 2a7. Passing it as a keyword
argument was deprecated in Python 3.5. (Contributed by Serhiy
Storchaka in bpo-29193(7).)
* Attributes *note key: 48d, *note value: 48e. and *note coded_value:
48f. of class *note http.cookies.Morsel: 490. are now read-only.
Assigning to them was deprecated in Python 3.5. Use the *note
set(): 491. method for setting them. (Contributed by Serhiy
Storchaka in bpo-29192(8).)
* The `mode' argument of *note os.makedirs(): 3bd. no longer affects
the file permission bits of newly-created intermediate-level
directories. To set their file permission bits you can set the
umask before invoking ‘makedirs()’. (Contributed by Serhiy
Storchaka in bpo-19930(9).)
* The *note struct.Struct.format: 492. type is now *note str: 330.
instead of *note bytes: 331. (Contributed by Victor Stinner in
bpo-21071(10).)
* *note parse_multipart(): 2ee. now accepts the `encoding' and
`errors' arguments and returns the same results as ‘FieldStorage’:
for non-file fields, the value associated to a key is a list of
strings, not bytes. (Contributed by Pierre Quentel in
bpo-29979(11).)
* Due to internal changes in *note socket: ef, calling *note
socket.fromshare(): 493. on a socket created by *note socket.share:
494. in older Python versions is not supported.
* ‘repr’ for *note BaseException: 1a8. has changed to not include the
trailing comma. Most exceptions are affected by this change.
(Contributed by Serhiy Storchaka in bpo-30399(12).)
* ‘repr’ for *note datetime.timedelta: 195. has changed to include
the keyword arguments in the output. (Contributed by Utkarsh
Upadhyay in bpo-30302(13).)
* Because *note shutil.rmtree(): 21c. is now implemented using the
*note os.scandir(): 25f. function, the user specified handler
`onerror' is now called with the first argument ‘os.scandir’
instead of ‘os.listdir’ when listing the directory is failed.
* Support for nested sets and set operations in regular expressions
as in Unicode Technical Standard #18(14) might be added in the
future. This would change the syntax. To facilitate this future
change a *note FutureWarning: 325. will be raised in ambiguous
cases for the time being. That include sets starting with a
literal ‘'['’ or containing literal character sequences ‘'--'’,
‘'&&'’, ‘'~~'’, and ‘'||'’. To avoid a warning, escape them with a
backslash. (Contributed by Serhiy Storchaka in bpo-30349(15).)
* The result of splitting a string on a *note regular expression: dd.
that could match an empty string has been changed. For example
splitting on ‘r'\s*'’ will now split not only on whitespaces as it
did previously, but also on empty strings before all non-whitespace
characters and just before the end of the string. The previous
behavior can be restored by changing the pattern to ‘r'\s+'’. A
*note FutureWarning: 325. was emitted for such patterns since
Python 3.5.
For patterns that match both empty and non-empty strings, the
result of searching for all matches may also be changed in other
cases. For example in the string ‘'a\n\n'’, the pattern
‘r'(?m)^\s*?$'’ will not only match empty strings at positions 2
and 3, but also the string ‘'\n'’ at positions 2–3. To match only
blank lines, the pattern should be rewritten as
‘r'(?m)^[^\S\n]*$'’.
*note re.sub(): 47b. now replaces empty matches adjacent to a
previous non-empty match. For example ‘re.sub('x*', '-', 'abxd')’
returns now ‘'-a-b--d-'’ instead of ‘'-a-b-d-'’ (the first minus
between ‘b’ and ‘d’ replaces ‘x’, and the second minus replaces an
empty string between ‘x’ and ‘d’).
(Contributed by Serhiy Storchaka in bpo-25054(16) and
bpo-32308(17).)
* Change *note re.escape(): 495. to only escape regex special
characters instead of escaping all characters other than ASCII
letters, numbers, and ‘'_'’. (Contributed by Serhiy Storchaka in
bpo-29995(18).)
* *note tracemalloc.Traceback: 3ff. frames are now sorted from oldest
to most recent to be more consistent with *note traceback: 113.
(Contributed by Jesse Bakker in bpo-32121(19).)
* On OSes that support *note socket.SOCK_NONBLOCK: 496. or *note
socket.SOCK_CLOEXEC: 497. bit flags, the *note socket.type: 498. no
longer has them applied. Therefore, checks like ‘if sock.type ==
socket.SOCK_STREAM’ work as expected on all platforms.
(Contributed by Yury Selivanov in bpo-32331(20).)
* On Windows the default for the `close_fds' argument of *note
subprocess.Popen: 2b9. was changed from *note False: 388. to *note
True: 499. when redirecting the standard handles. If you
previously depended on handles being inherited when using *note
subprocess.Popen: 2b9. with standard io redirection, you will have
to pass ‘close_fds=False’ to preserve the previous behaviour, or
use *note STARTUPINFO.lpAttributeList: 49a.
* *note importlib.machinery.PathFinder.invalidate_caches(): 49b. –
which implicitly affects *note importlib.invalidate_caches(): 49c.
– now deletes entries in *note sys.path_importer_cache: 49d. which
are set to ‘None’. (Contributed by Brett Cannon in bpo-33169(21).)
* In *note asyncio: a, *note loop.sock_recv(): 49e, *note
loop.sock_sendall(): 49f, *note loop.sock_accept(): 4a0, *note
loop.getaddrinfo(): 4a1, *note loop.getnameinfo(): 4a2. have been
changed to be proper coroutine methods to match their
documentation. Previously, these methods returned *note
asyncio.Future: 443. instances. (Contributed by Yury Selivanov in
bpo-32327(22).)
* *note asyncio.Server.sockets: 4a3. now returns a copy of the
internal list of server sockets, instead of returning it directly.
(Contributed by Yury Selivanov in bpo-32662(23).)
* *note Struct.format: 492. is now a *note str: 330. instance instead
of a *note bytes: 331. instance. (Contributed by Victor Stinner in
bpo-21071(24).)
* *note argparse: 6. subparsers can now be made mandatory by passing
‘required=True’ to *note ArgumentParser.add_subparsers(): 4a4.
(Contributed by Anthony Sottile in bpo-26510(25).)
* *note ast.literal_eval(): 4a5. is now stricter. Addition and
subtraction of arbitrary numbers are no longer allowed.
(Contributed by Serhiy Storchaka in bpo-31778(26).)
* *note Calendar.itermonthdates: 4a6. will now consistently raise an
exception when a date falls outside of the ‘0001-01-01’ through
‘9999-12-31’ range. To support applications that cannot tolerate
such exceptions, the new *note Calendar.itermonthdays3: 4a7. and
*note Calendar.itermonthdays4: 4a8. can be used. The new methods
return tuples and are not restricted by the range supported by
*note datetime.date: 193. (Contributed by Alexander Belopolsky in
bpo-28292(27).)
* *note collections.ChainMap: 4a9. now preserves the order of the
underlying mappings. (Contributed by Raymond Hettinger in
bpo-32792(28).)
* The ‘submit()’ method of *note
concurrent.futures.ThreadPoolExecutor: 27a. and *note
concurrent.futures.ProcessPoolExecutor: 2a4. now raises a *note
RuntimeError: 2ba. if called during interpreter shutdown.
(Contributed by Mark Nemec in bpo-33097(29).)
* The *note configparser.ConfigParser: 4aa. constructor now uses
‘read_dict()’ to process the default values, making its behavior
consistent with the rest of the parser. Non-string keys and values
in the defaults dictionary are now being implicitly converted to
strings. (Contributed by James Tocknell in bpo-23835(30).)
* Several undocumented internal imports were removed. One example is
that ‘os.errno’ is no longer available; use ‘import errno’ directly
instead. Note that such undocumented internal imports may be
removed any time without notice, even in micro version releases.
---------- Footnotes ----------
(1) https://bugs.python.org/issue31233
(2) https://bugs.python.org/issue33540
(3) https://bugs.python.org/issue31151
(4) https://bugs.python.org/issue33540
(5) https://bugs.python.org/issue31900
(6) https://bugs.python.org/issue24744
(7) https://bugs.python.org/issue29193
(8) https://bugs.python.org/issue29192
(9) https://bugs.python.org/issue19930
(10) https://bugs.python.org/issue21071
(11) https://bugs.python.org/issue29979
(12) https://bugs.python.org/issue30399
(13) https://bugs.python.org/issue30302
(14) https://unicode.org/reports/tr18/
(15) https://bugs.python.org/issue30349
(16) https://bugs.python.org/issue25054
(17) https://bugs.python.org/issue32308
(18) https://bugs.python.org/issue29995
(19) https://bugs.python.org/issue32121
(20) https://bugs.python.org/issue32331
(21) https://bugs.python.org/issue33169
(22) https://bugs.python.org/issue32327
(23) https://bugs.python.org/issue32662
(24) https://bugs.python.org/issue21071
(25) https://bugs.python.org/issue26510
(26) https://bugs.python.org/issue31778
(27) https://bugs.python.org/issue28292
(28) https://bugs.python.org/issue32792
(29) https://bugs.python.org/issue33097
(30) https://bugs.python.org/issue23835
File: python.info, Node: Changes in the C API<2>, Next: CPython bytecode changes<2>, Prev: Changes in the Python API<2>, Up: Porting to Python 3 7
1.2.17.3 Changes in the C API
.............................
The function *note PySlice_GetIndicesEx(): 478. is considered unsafe for
resizable sequences. If the slice indices are not instances of *note
int: 184, but objects that implement the ‘__index__()’ method, the
sequence can be resized after passing its length to
‘PySlice_GetIndicesEx()’. This can lead to returning indices out of the
length of the sequence. For avoiding possible problems use new
functions *note PySlice_Unpack(): 42f. and *note
PySlice_AdjustIndices(): 430. (Contributed by Serhiy Storchaka in
bpo-27867(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27867
File: python.info, Node: CPython bytecode changes<2>, Next: Windows-only Changes<2>, Prev: Changes in the C API<2>, Up: Porting to Python 3 7
1.2.17.4 CPython bytecode changes
.................................
There are two new opcodes: *note LOAD_METHOD: 4ad. and *note
CALL_METHOD: 4ae. (Contributed by Yury Selivanov and INADA Naoki in
bpo-26110(1).)
The ‘STORE_ANNOTATION’ opcode has been removed. (Contributed by Mark
Shannon in bpo-32550(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26110
(2) https://bugs.python.org/issue32550
File: python.info, Node: Windows-only Changes<2>, Next: Other CPython implementation changes, Prev: CPython bytecode changes<2>, Up: Porting to Python 3 7
1.2.17.5 Windows-only Changes
.............................
The file used to override *note sys.path: 488. is now called
‘._pth’ instead of ‘'sys.path'’. See *note Finding
modules: 4b0. for more information. (Contributed by Steve Dower in
bpo-28137(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28137
File: python.info, Node: Other CPython implementation changes, Prev: Windows-only Changes<2>, Up: Porting to Python 3 7
1.2.17.6 Other CPython implementation changes
.............................................
In preparation for potential future changes to the public CPython
runtime initialization API (see PEP 432(1) for an initial, but somewhat
outdated, draft), CPython’s internal startup and configuration
management logic has been significantly refactored. While these updates
are intended to be entirely transparent to both embedding applications
and users of the regular CPython CLI, they’re being mentioned here as
the refactoring changes the internal order of various operations during
interpreter startup, and hence may uncover previously latent defects,
either in embedding applications, or in CPython itself. (Initially
contributed by Nick Coghlan and Eric Snow as part of bpo-22257(2), and
further updated by Nick, Eric, and Victor Stinner in a number of other
issues). Some known details affected:
* *note PySys_AddWarnOptionUnicode(): 4b2. is not currently usable by
embedding applications due to the requirement to create a Unicode
object prior to calling ‘Py_Initialize’. Use *note
PySys_AddWarnOption(): 4b3. instead.
* warnings filters added by an embedding application with *note
PySys_AddWarnOption(): 4b3. should now more consistently take
precedence over the default filters set by the interpreter
Due to changes in the way the default warnings filters are configured,
setting *note Py_BytesWarningFlag: 4b4. to a value greater than one is
no longer sufficient to both emit *note BytesWarning: 4b5. messages and
have them converted to exceptions. Instead, the flag must be set (to
cause the warnings to be emitted in the first place), and an explicit
‘error::BytesWarning’ warnings filter added to convert them to
exceptions.
Due to a change in the way docstrings are handled by the compiler, the
implicit ‘return None’ in a function body consisting solely of a
docstring is now marked as occurring on the same line as the docstring,
not on the function’s header line.
The current exception state has been moved from the frame object to the
co-routine. This simplified the interpreter and fixed a couple of
obscure bugs caused by having swap exception state when entering or
exiting a generator. (Contributed by Mark Shannon in bpo-25612(3).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0432
(2) https://bugs.python.org/issue22257
(3) https://bugs.python.org/issue25612
File: python.info, Node: Notable changes in Python 3 7 1, Next: Notable changes in Python 3 7 2, Prev: Porting to Python 3 7, Up: What’s New In Python 3 7
1.2.18 Notable changes in Python 3.7.1
--------------------------------------
Starting in 3.7.1, *note Py_Initialize(): 439. now consistently reads
and respects all of the same environment settings as *note Py_Main():
4b7. (in earlier Python versions, it respected an ill-defined subset of
those environment variables, while in Python 3.7.0 it didn’t read any of
them due to bpo-34247(1)). If this behavior is unwanted, set *note
Py_IgnoreEnvironmentFlag: 4b8. to 1 before calling *note
Py_Initialize(): 439.
In 3.7.1 the C API for Context Variables *note was updated: 4b9. to use
*note PyObject: 4ba. pointers. See also bpo-34762(2).
In 3.7.1 the *note tokenize: 111. module now implicitly emits a
‘NEWLINE’ token when provided with input that does not have a trailing
new line. This behavior now matches what the C tokenizer does
internally. (Contributed by Ammar Askar in bpo-33899(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue34247
(2) https://bugs.python.org/issue34762
(3) https://bugs.python.org/issue33899
File: python.info, Node: Notable changes in Python 3 7 2, Next: Notable changes in Python 3 7 6, Prev: Notable changes in Python 3 7 1, Up: What’s New In Python 3 7
1.2.19 Notable changes in Python 3.7.2
--------------------------------------
In 3.7.2, *note venv: 125. on Windows no longer copies the original
binaries, but creates redirector scripts named ‘python.exe’ and
‘pythonw.exe’ instead. This resolves a long standing issue where all
virtual environments would have to be upgraded or recreated with each
Python update. However, note that this release will still require
recreation of virtual environments in order to get the new scripts.
File: python.info, Node: Notable changes in Python 3 7 6, Next: Notable changes in Python 3 7 10, Prev: Notable changes in Python 3 7 2, Up: What’s New In Python 3 7
1.2.20 Notable changes in Python 3.7.6
--------------------------------------
Due to significant security concerns, the `reuse_address' parameter of
*note asyncio.loop.create_datagram_endpoint(): 2e7. is no longer
supported. This is because of the behavior of the socket option
‘SO_REUSEADDR’ in UDP. For more details, see the documentation for
‘loop.create_datagram_endpoint()’. (Contributed by Kyle Stanley,
Antoine Pitrou, and Yury Selivanov in bpo-37228(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue37228
File: python.info, Node: Notable changes in Python 3 7 10, Prev: Notable changes in Python 3 7 6, Up: What’s New In Python 3 7
1.2.21 Notable changes in Python 3.7.10
---------------------------------------
Earlier Python versions allowed using both ‘;’ and ‘&’ as query
parameter separators in *note urllib.parse.parse_qs(): 2eb. and *note
urllib.parse.parse_qsl(): 2ec. Due to security concerns, and to conform
with newer W3C recommendations, this has been changed to allow only a
single separator key, with ‘&’ as the default. This change also affects
*note cgi.parse(): 2ed. and *note cgi.parse_multipart(): 2ee. as they
use the affected functions internally. For more details, please see
their respective documentation. (Contributed by Adam Goldschmidt,
Senthil Kumaran and Ken Jin in bpo-42967(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue42967
File: python.info, Node: What’s New In Python 3 6, Next: What’s New In Python 3 5, Prev: What’s New In Python 3 7, Up: What’s New in Python
1.3 What’s New In Python 3.6
============================
Editors: Elvis Pranskevichus <>, Yury Selivanov
<>
This article explains the new features in Python 3.6, compared to 3.5.
Python 3.6 was released on December 23, 2016. See the changelog(1) for
a full list of changes.
See also
........
PEP 494(2) - Python 3.6 Release Schedule
* Menu:
* Summary – Release highlights: Summary – Release highlights<2>.
* New Features: New Features<3>.
* Other Language Changes: Other Language Changes<3>.
* New Modules: New Modules<3>.
* Improved Modules: Improved Modules<3>.
* Optimizations: Optimizations<3>.
* Build and C API Changes: Build and C API Changes<2>.
* Other Improvements::
* Deprecated: Deprecated<2>.
* Removed::
* Porting to Python 3.6: Porting to Python 3 6.
* Notable changes in Python 3.6.2: Notable changes in Python 3 6 2.
* Notable changes in Python 3.6.4: Notable changes in Python 3 6 4.
* Notable changes in Python 3.6.5: Notable changes in Python 3 6 5.
* Notable changes in Python 3.6.7: Notable changes in Python 3 6 7.
* Notable changes in Python 3.6.10: Notable changes in Python 3 6 10.
* Notable changes in Python 3.6.13: Notable changes in Python 3 6 13.
---------- Footnotes ----------
(1) https://docs.python.org/3.6/whatsnew/changelog.html
(2) https://www.python.org/dev/peps/pep-0494
File: python.info, Node: Summary – Release highlights<2>, Next: New Features<3>, Up: What’s New In Python 3 6
1.3.1 Summary – Release highlights
----------------------------------
New syntax features:
* *note PEP 498: 4c1, formatted string literals.
* *note PEP 515: 4c2, underscores in numeric literals.
* *note PEP 526: 4c3, syntax for variable annotations.
* *note PEP 525: 4c4, asynchronous generators.
* *note PEP 530: 4c5.: asynchronous comprehensions.
New library modules:
* *note secrets: e4.: *note PEP 506 – Adding A Secrets Module To The
Standard Library: 4c6.
CPython implementation improvements:
* The *note dict: 2fc. type has been reimplemented to use a *note
more compact representation: 4c7. based on a proposal by Raymond
Hettinger(1) and similar to the PyPy dict implementation(2). This
resulted in dictionaries using 20% to 25% less memory when compared
to Python 3.5.
* Customization of class creation has been simplified with the *note
new protocol: 4c8.
* The class attribute definition order is *note now preserved: 4c9.
* The order of elements in ‘**kwargs’ now *note corresponds to the
order: 4ca. in which keyword arguments were passed to the function.
* DTrace and SystemTap *note probing support: 4cb. has been added.
* The new *note PYTHONMALLOC: 4cc. environment variable can now be
used to debug the interpreter memory allocation and access errors.
Significant improvements in the standard library:
* The *note asyncio: a. module has received new features, significant
usability and performance improvements, and a fair amount of bug
fixes. Starting with Python 3.6 the ‘asyncio’ module is no longer
provisional and its API is considered stable.
* A new *note file system path protocol: 4cd. has been implemented to
support *note path-like objects: 36a. All standard library
functions operating on paths have been updated to work with the new
protocol.
* The *note datetime: 31. module has gained support for *note Local
Time Disambiguation: 4ce.
* The *note typing: 119. module received a number of *note
improvements: 4cf.
* The *note tracemalloc: 114. module has been significantly reworked
and is now used to provide better output for *note ResourceWarning:
4d0. as well as provide better diagnostics for memory allocation
errors. See the *note PYTHONMALLOC section: 4cc. for more
information.
Security improvements:
* The new *note secrets: e4. module has been added to simplify the
generation of cryptographically strong pseudo-random numbers
suitable for managing secrets such as account authentication,
tokens, and similar.
* On Linux, *note os.urandom(): 4d1. now blocks until the system
urandom entropy pool is initialized to increase the security. See
the PEP 524(3) for the rationale.
* The *note hashlib: 8d. and *note ssl: f3. modules now support
OpenSSL 1.1.0.
* The default settings and feature set of the *note ssl: f3. module
have been improved.
* The *note hashlib: 8d. module received support for the BLAKE2,
SHA-3 and SHAKE hash algorithms and the *note scrypt(): 4d2. key
derivation function.
Windows improvements:
* *note PEP 528: 4d3. and *note PEP 529: 4d4, Windows filesystem and
console encoding changed to UTF-8.
* The ‘py.exe’ launcher, when used interactively, no longer prefers
Python 2 over Python 3 when the user doesn’t specify a version (via
command line arguments or a config file). Handling of shebang
lines remains unchanged - “python” refers to Python 2 in that case.
* ‘python.exe’ and ‘pythonw.exe’ have been marked as long-path aware,
which means that the 260 character path limit may no longer apply.
See *note removing the MAX_PATH limitation: 4d5. for details.
* A ‘._pth’ file can be added to force isolated mode and fully
specify all search paths to avoid registry and environment lookup.
See *note the documentation: 4b0. for more information.
* A ‘python36.zip’ file now works as a landmark to infer *note
PYTHONHOME: 4d6. See *note the documentation: 4b0. for more
information.
---------- Footnotes ----------
(1)
https://mail.python.org/pipermail/python-dev/2012-December/123028.html
(2)
https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
(3) https://www.python.org/dev/peps/pep-0524
File: python.info, Node: New Features<3>, Next: Other Language Changes<3>, Prev: Summary – Release highlights<2>, Up: What’s New In Python 3 6
1.3.2 New Features
------------------
* Menu:
* PEP 498; Formatted string literals: PEP 498 Formatted string literals.
* PEP 526; Syntax for variable annotations: PEP 526 Syntax for variable annotations.
* PEP 515; Underscores in Numeric Literals: PEP 515 Underscores in Numeric Literals.
* PEP 525; Asynchronous Generators: PEP 525 Asynchronous Generators.
* PEP 530; Asynchronous Comprehensions: PEP 530 Asynchronous Comprehensions.
* PEP 487; Simpler customization of class creation: PEP 487 Simpler customization of class creation.
* PEP 487; Descriptor Protocol Enhancements: PEP 487 Descriptor Protocol Enhancements.
* PEP 519; Adding a file system path protocol: PEP 519 Adding a file system path protocol.
* PEP 495; Local Time Disambiguation: PEP 495 Local Time Disambiguation.
* PEP 529; Change Windows filesystem encoding to UTF-8: PEP 529 Change Windows filesystem encoding to UTF-8.
* PEP 528; Change Windows console encoding to UTF-8: PEP 528 Change Windows console encoding to UTF-8.
* PEP 520; Preserving Class Attribute Definition Order: PEP 520 Preserving Class Attribute Definition Order.
* PEP 468; Preserving Keyword Argument Order: PEP 468 Preserving Keyword Argument Order.
* New dict implementation::
* PEP 523; Adding a frame evaluation API to CPython: PEP 523 Adding a frame evaluation API to CPython.
* PYTHONMALLOC environment variable::
* DTrace and SystemTap probing support::
File: python.info, Node: PEP 498 Formatted string literals, Next: PEP 526 Syntax for variable annotations, Up: New Features<3>
1.3.2.1 PEP 498: Formatted string literals
..........................................
PEP 498(1) introduces a new kind of string literals: `f-strings', or
*note formatted string literals: 15c.
Formatted string literals are prefixed with ‘'f'’ and are similar to the
format strings accepted by *note str.format(): 4da. They contain
replacement fields surrounded by curly braces. The replacement fields
are expressions, which are evaluated at run time, and then formatted
using the *note format(): 4db. protocol:
>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
'result: 12.35'
See also
........
PEP 498(2) – Literal String Interpolation.
PEP written and implemented by Eric V. Smith.
*note Feature documentation: 15c.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0498
(2) https://www.python.org/dev/peps/pep-0498
File: python.info, Node: PEP 526 Syntax for variable annotations, Next: PEP 515 Underscores in Numeric Literals, Prev: PEP 498 Formatted string literals, Up: New Features<3>
1.3.2.2 PEP 526: Syntax for variable annotations
................................................
PEP 484(1) introduced the standard for type annotations of function
parameters, a.k.a. type hints. This PEP adds syntax to Python for
annotating the types of variables including class variables and instance
variables:
primes: List[int] = []
captain: str # Note: no initial value!
class Starship:
stats: Dict[str, int] = {}
Just as for function annotations, the Python interpreter does not attach
any particular meaning to variable annotations and only stores them in
the ‘__annotations__’ attribute of a class or module.
In contrast to variable declarations in statically typed languages, the
goal of annotation syntax is to provide an easy way to specify
structured type metadata for third party tools and libraries via the
abstract syntax tree and the ‘__annotations__’ attribute.
See also
........
PEP 526(2) – Syntax for variable annotations.
PEP written by Ryan Gonzalez, Philip House, Ivan Levkivskyi, Lisa
Roach, and Guido van Rossum. Implemented by Ivan Levkivskyi.
Tools that use or will use the new syntax: mypy(3), pytype(4), PyCharm,
etc.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0484
(2) https://www.python.org/dev/peps/pep-0526
(3) http://www.mypy-lang.org/
(4) https://github.com/google/pytype
File: python.info, Node: PEP 515 Underscores in Numeric Literals, Next: PEP 525 Asynchronous Generators, Prev: PEP 526 Syntax for variable annotations, Up: New Features<3>
1.3.2.3 PEP 515: Underscores in Numeric Literals
................................................
PEP 515(1) adds the ability to use underscores in numeric literals for
improved readability. For example:
>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295
Single underscores are allowed between digits and after any base
specifier. Leading, trailing, or multiple underscores in a row are not
allowed.
The *note string formatting: 4de. language also now has support for the
‘'_'’ option to signal the use of an underscore for a thousands
separator for floating point presentation types and for integer
presentation type ‘'d'’. For integer presentation types ‘'b'’, ‘'o'’,
‘'x'’, and ‘'X'’, underscores will be inserted every 4 digits:
>>> '{:_}'.format(1000000)
'1_000_000'
>>> '{:_x}'.format(0xFFFFFFFF)
'ffff_ffff'
See also
........
PEP 515(2) – Underscores in Numeric Literals
PEP written by Georg Brandl and Serhiy Storchaka.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0515
(2) https://www.python.org/dev/peps/pep-0515
File: python.info, Node: PEP 525 Asynchronous Generators, Next: PEP 530 Asynchronous Comprehensions, Prev: PEP 515 Underscores in Numeric Literals, Up: New Features<3>
1.3.2.4 PEP 525: Asynchronous Generators
........................................
PEP 492(1) introduced support for native coroutines and ‘async’ /
‘await’ syntax to Python 3.5. A notable limitation of the Python 3.5
implementation is that it was not possible to use ‘await’ and ‘yield’ in
the same function body. In Python 3.6 this restriction has been lifted,
making it possible to define `asynchronous generators':
async def ticker(delay, to):
"""Yield numbers from 0 to *to* every *delay* seconds."""
for i in range(to):
yield i
await asyncio.sleep(delay)
The new syntax allows for faster and more concise code.
See also
........
PEP 525(2) – Asynchronous Generators
PEP written and implemented by Yury Selivanov.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0492
(2) https://www.python.org/dev/peps/pep-0525
File: python.info, Node: PEP 530 Asynchronous Comprehensions, Next: PEP 487 Simpler customization of class creation, Prev: PEP 525 Asynchronous Generators, Up: New Features<3>
1.3.2.5 PEP 530: Asynchronous Comprehensions
............................................
PEP 530(1) adds support for using ‘async for’ in list, set, dict
comprehensions and generator expressions:
result = [i async for i in aiter() if i % 2]
Additionally, ‘await’ expressions are supported in all kinds of
comprehensions:
result = [await fun() for fun in funcs if await condition()]
See also
........
PEP 530(2) – Asynchronous Comprehensions
PEP written and implemented by Yury Selivanov.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0530
(2) https://www.python.org/dev/peps/pep-0530
File: python.info, Node: PEP 487 Simpler customization of class creation, Next: PEP 487 Descriptor Protocol Enhancements, Prev: PEP 530 Asynchronous Comprehensions, Up: New Features<3>
1.3.2.6 PEP 487: Simpler customization of class creation
........................................................
It is now possible to customize subclass creation without using a
metaclass. The new ‘__init_subclass__’ classmethod will be called on
the base class whenever a new subclass is created:
class PluginBase:
subclasses = []
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.subclasses.append(cls)
class Plugin1(PluginBase):
pass
class Plugin2(PluginBase):
pass
In order to allow zero-argument *note super(): 4e2. calls to work
correctly from *note __init_subclass__(): 4e3. implementations, custom
metaclasses must ensure that the new ‘__classcell__’ namespace entry is
propagated to ‘type.__new__’ (as described in *note Creating the class
object: 4e4.).
See also
........
PEP 487(1) – Simpler customization of class creation
PEP written and implemented by Martin Teichmann.
*note Feature documentation: 4e5.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0487
File: python.info, Node: PEP 487 Descriptor Protocol Enhancements, Next: PEP 519 Adding a file system path protocol, Prev: PEP 487 Simpler customization of class creation, Up: New Features<3>
1.3.2.7 PEP 487: Descriptor Protocol Enhancements
.................................................
PEP 487(1) extends the descriptor protocol to include the new optional
*note __set_name__(): 4e8. method. Whenever a new class is defined, the
new method will be called on all descriptors included in the definition,
providing them with a reference to the class being defined and the name
given to the descriptor within the class namespace. In other words,
instances of descriptors can now know the attribute name of the
descriptor in the owner class:
class IntField:
def __get__(self, instance, owner):
return instance.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, int):
raise ValueError(f'expecting integer in {self.name}')
instance.__dict__[self.name] = value
# this is the new initializer:
def __set_name__(self, owner, name):
self.name = name
class Model:
int_field = IntField()
See also
........
PEP 487(2) – Simpler customization of class creation
PEP written and implemented by Martin Teichmann.
*note Feature documentation: 4e9.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0487
(2) https://www.python.org/dev/peps/pep-0487
File: python.info, Node: PEP 519 Adding a file system path protocol, Next: PEP 495 Local Time Disambiguation, Prev: PEP 487 Descriptor Protocol Enhancements, Up: New Features<3>
1.3.2.8 PEP 519: Adding a file system path protocol
...................................................
File system paths have historically been represented as *note str: 330.
or *note bytes: 331. objects. This has led to people who write code
which operate on file system paths to assume that such objects are only
one of those two types (an *note int: 184. representing a file
descriptor does not count as that is not a file path). Unfortunately
that assumption prevents alternative object representations of file
system paths like *note pathlib: c8. from working with pre-existing
code, including Python’s standard library.
To fix this situation, a new interface represented by *note os.PathLike:
4eb. has been defined. By implementing the *note __fspath__(): 4ec.
method, an object signals that it represents a path. An object can then
provide a low-level representation of a file system path as a *note str:
330. or *note bytes: 331. object. This means an object is considered
*note path-like: 36a. if it implements *note os.PathLike: 4eb. or is a
*note str: 330. or *note bytes: 331. object which represents a file
system path. Code can use *note os.fspath(): 4ed, *note os.fsdecode():
4ee, or *note os.fsencode(): 4ef. to explicitly get a *note str: 330.
and/or *note bytes: 331. representation of a path-like object.
The built-in *note open(): 4f0. function has been updated to accept
*note os.PathLike: 4eb. objects, as have all relevant functions in the
*note os: c4. and *note os.path: c5. modules, and most other functions
and classes in the standard library. The *note os.DirEntry: 4f1. class
and relevant classes in *note pathlib: c8. have also been updated to
implement *note os.PathLike: 4eb.
The hope is that updating the fundamental functions for operating on
file system paths will lead to third-party code to implicitly support
all *note path-like objects: 36a. without any code changes, or at least
very minimal ones (e.g. calling *note os.fspath(): 4ed. at the
beginning of code before operating on a path-like object).
Here are some examples of how the new interface allows for *note
pathlib.Path: 201. to be used more easily and transparently with
pre-existing code:
>>> import pathlib
>>> with open(pathlib.Path("README")) as f:
... contents = f.read()
...
>>> import os.path
>>> os.path.splitext(pathlib.Path("some_file.txt"))
('some_file', '.txt')
>>> os.path.join("/a/b", pathlib.Path("c"))
'/a/b/c'
>>> import os
>>> os.fspath(pathlib.Path("some_file.txt"))
'some_file.txt'
(Implemented by Brett Cannon, Ethan Furman, Dusty Phillips, and Jelle
Zijlstra.)
See also
........
PEP 519(1) – Adding a file system path protocol
PEP written by Brett Cannon and Koos Zevenhoven.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0519
File: python.info, Node: PEP 495 Local Time Disambiguation, Next: PEP 529 Change Windows filesystem encoding to UTF-8, Prev: PEP 519 Adding a file system path protocol, Up: New Features<3>
1.3.2.9 PEP 495: Local Time Disambiguation
..........................................
In most world locations, there have been and will be times when local
clocks are moved back. In those times, intervals are introduced in
which local clocks show the same time twice in the same day. In these
situations, the information displayed on a local clock (or stored in a
Python datetime instance) is insufficient to identify a particular
moment in time.
PEP 495(1) adds the new `fold' attribute to instances of *note
datetime.datetime: 194. and *note datetime.time: 4f3. classes to
differentiate between two moments in time for which local times are the
same:
>>> u0 = datetime(2016, 11, 6, 4, tzinfo=timezone.utc)
>>> for i in range(4):
... u = u0 + i*HOUR
... t = u.astimezone(Eastern)
... print(u.time(), 'UTC =', t.time(), t.tzname(), t.fold)
...
04:00:00 UTC = 00:00:00 EDT 0
05:00:00 UTC = 01:00:00 EDT 0
06:00:00 UTC = 01:00:00 EST 1
07:00:00 UTC = 02:00:00 EST 0
The values of the *note fold: 4f4. attribute have the value ‘0’ for all
instances except those that represent the second (chronologically)
moment in time in an ambiguous case.
See also
........
PEP 495(2) – Local Time Disambiguation
PEP written by Alexander Belopolsky and Tim Peters, implementation
by Alexander Belopolsky.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0495
(2) https://www.python.org/dev/peps/pep-0495
File: python.info, Node: PEP 529 Change Windows filesystem encoding to UTF-8, Next: PEP 528 Change Windows console encoding to UTF-8, Prev: PEP 495 Local Time Disambiguation, Up: New Features<3>
1.3.2.10 PEP 529: Change Windows filesystem encoding to UTF-8
.............................................................
Representing filesystem paths is best performed with str (Unicode)
rather than bytes. However, there are some situations where using bytes
is sufficient and correct.
Prior to Python 3.6, data loss could result when using bytes paths on
Windows. With this change, using bytes to represent paths is now
supported on Windows, provided those bytes are encoded with the encoding
returned by *note sys.getfilesystemencoding(): 4f6, which now defaults
to ‘'utf-8'’.
Applications that do not use str to represent paths should use *note
os.fsencode(): 4ef. and *note os.fsdecode(): 4ee. to ensure their bytes
are correctly encoded. To revert to the previous behaviour, set *note
PYTHONLEGACYWINDOWSFSENCODING: 4f7. or call *note
sys._enablelegacywindowsfsencoding(): 4f8.
See PEP 529(1) for more information and discussion of code modifications
that may be required.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0529
File: python.info, Node: PEP 528 Change Windows console encoding to UTF-8, Next: PEP 520 Preserving Class Attribute Definition Order, Prev: PEP 529 Change Windows filesystem encoding to UTF-8, Up: New Features<3>
1.3.2.11 PEP 528: Change Windows console encoding to UTF-8
..........................................................
The default console on Windows will now accept all Unicode characters
and provide correctly read str objects to Python code. ‘sys.stdin’,
‘sys.stdout’ and ‘sys.stderr’ now default to utf-8 encoding.
This change only applies when using an interactive console, and not when
redirecting files or pipes. To revert to the previous behaviour for
interactive console use, set *note PYTHONLEGACYWINDOWSSTDIO: 4fa.
See also
........
PEP 528(1) – Change Windows console encoding to UTF-8
PEP written and implemented by Steve Dower.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0528
File: python.info, Node: PEP 520 Preserving Class Attribute Definition Order, Next: PEP 468 Preserving Keyword Argument Order, Prev: PEP 528 Change Windows console encoding to UTF-8, Up: New Features<3>
1.3.2.12 PEP 520: Preserving Class Attribute Definition Order
.............................................................
Attributes in a class definition body have a natural ordering: the same
order in which the names appear in the source. This order is now
preserved in the new class’s *note __dict__: 4fc. attribute.
Also, the effective default class `execution' namespace (returned from
*note type.__prepare__(): 4fd.) is now an insertion-order-preserving
mapping.
See also
........
PEP 520(1) – Preserving Class Attribute Definition Order
PEP written and implemented by Eric Snow.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0520
File: python.info, Node: PEP 468 Preserving Keyword Argument Order, Next: New dict implementation, Prev: PEP 520 Preserving Class Attribute Definition Order, Up: New Features<3>
1.3.2.13 PEP 468: Preserving Keyword Argument Order
...................................................
‘**kwargs’ in a function signature is now guaranteed to be an
insertion-order-preserving mapping.
See also
........
PEP 468(1) – Preserving Keyword Argument Order
PEP written and implemented by Eric Snow.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0468
File: python.info, Node: New dict implementation, Next: PEP 523 Adding a frame evaluation API to CPython, Prev: PEP 468 Preserving Keyword Argument Order, Up: New Features<3>
1.3.2.14 New dict implementation
................................
The *note dict: 2fc. type now uses a “compact” representation based on a
proposal by Raymond Hettinger(1) which was first implemented by PyPy(2).
The memory usage of the new *note dict(): 1b8. is between 20% and 25%
smaller compared to Python 3.5.
The order-preserving aspect of this new implementation is considered an
implementation detail and should not be relied upon (this may change in
the future, but it is desired to have this new dict implementation in
the language for a few releases before changing the language spec to
mandate order-preserving semantics for all current and future Python
implementations; this also helps preserve backwards-compatibility with
older versions of the language where random iteration order is still in
effect, e.g. Python 3.5).
(Contributed by INADA Naoki in bpo-27350(3). Idea originally suggested
by Raymond Hettinger(4).)
---------- Footnotes ----------
(1)
https://mail.python.org/pipermail/python-dev/2012-December/123028.html
(2)
https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
(3) https://bugs.python.org/issue27350
(4)
https://mail.python.org/pipermail/python-dev/2012-December/123028.html
File: python.info, Node: PEP 523 Adding a frame evaluation API to CPython, Next: PYTHONMALLOC environment variable, Prev: New dict implementation, Up: New Features<3>
1.3.2.15 PEP 523: Adding a frame evaluation API to CPython
..........................................................
While Python provides extensive support to customize how code executes,
one place it has not done so is in the evaluation of frame objects. If
you wanted some way to intercept frame evaluation in Python there really
wasn’t any way without directly manipulating function pointers for
defined functions.
PEP 523(1) changes this by providing an API to make frame evaluation
pluggable at the C level. This will allow for tools such as debuggers
and JITs to intercept frame evaluation before the execution of Python
code begins. This enables the use of alternative evaluation
implementations for Python code, tracking frame evaluation, etc.
This API is not part of the limited C API and is marked as private to
signal that usage of this API is expected to be limited and only
applicable to very select, low-level use-cases. Semantics of the API
will change with Python as necessary.
See also
........
PEP 523(2) – Adding a frame evaluation API to CPython
PEP written by Brett Cannon and Dino Viehland.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0523
(2) https://www.python.org/dev/peps/pep-0523
File: python.info, Node: PYTHONMALLOC environment variable, Next: DTrace and SystemTap probing support, Prev: PEP 523 Adding a frame evaluation API to CPython, Up: New Features<3>
1.3.2.16 PYTHONMALLOC environment variable
..........................................
The new *note PYTHONMALLOC: 503. environment variable allows setting the
Python memory allocators and installing debug hooks.
It is now possible to install debug hooks on Python memory allocators on
Python compiled in release mode using ‘PYTHONMALLOC=debug’. Effects of
debug hooks:
* Newly allocated memory is filled with the byte ‘0xCB’
* Freed memory is filled with the byte ‘0xDB’
* Detect violations of the Python memory allocator API. For example,
*note PyObject_Free(): 504. called on a memory block allocated by
*note PyMem_Malloc(): 505.
* Detect writes before the start of a buffer (buffer underflows)
* Detect writes after the end of a buffer (buffer overflows)
* Check that the *note GIL: 506. is held when allocator functions of
*note PYMEM_DOMAIN_OBJ: 507. (ex: *note PyObject_Malloc(): 508.)
and *note PYMEM_DOMAIN_MEM: 509. (ex: *note PyMem_Malloc(): 505.)
domains are called.
Checking if the GIL is held is also a new feature of Python 3.6.
See the *note PyMem_SetupDebugHooks(): 50a. function for debug hooks on
Python memory allocators.
It is now also possible to force the usage of the ‘malloc()’ allocator
of the C library for all Python memory allocations using
‘PYTHONMALLOC=malloc’. This is helpful when using external memory
debuggers like Valgrind on a Python compiled in release mode.
On error, the debug hooks on Python memory allocators now use the *note
tracemalloc: 114. module to get the traceback where a memory block was
allocated.
Example of fatal error on buffer overflow using ‘python3.6 -X
tracemalloc=5’ (store 5 frames in traces):
Debug memory block at address p=0x7fbcd41666f8: API 'o'
4 bytes originally requested
The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
The 8 pad bytes at tail=0x7fbcd41666fc are not all FORBIDDENBYTE (0xfb):
at tail+0: 0x02 *** OUCH
at tail+1: 0xfb
at tail+2: 0xfb
at tail+3: 0xfb
at tail+4: 0xfb
at tail+5: 0xfb
at tail+6: 0xfb
at tail+7: 0xfb
The block was made by call #1233329 to debug malloc/realloc.
Data at p: 1a 2b 30 00
Memory block allocated at (most recent call first):
File "test/test_bytes.py", line 323
File "unittest/case.py", line 600
File "unittest/case.py", line 648
File "unittest/suite.py", line 122
File "unittest/suite.py", line 84
Fatal Python error: bad trailing pad byte
Current thread 0x00007fbcdbd32700 (most recent call first):
File "test/test_bytes.py", line 323 in test_hex
File "unittest/case.py", line 600 in run
File "unittest/case.py", line 648 in __call__
File "unittest/suite.py", line 122 in run
File "unittest/suite.py", line 84 in __call__
File "unittest/suite.py", line 122 in run
File "unittest/suite.py", line 84 in __call__
...
(Contributed by Victor Stinner in bpo-26516(1) and bpo-26564(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26516
(2) https://bugs.python.org/issue26564
File: python.info, Node: DTrace and SystemTap probing support, Prev: PYTHONMALLOC environment variable, Up: New Features<3>
1.3.2.17 DTrace and SystemTap probing support
.............................................
Python can now be built ‘--with-dtrace’ which enables static markers for
the following events in the interpreter:
* function call/return
* garbage collection started/finished
* line of code executed.
This can be used to instrument running interpreters in production,
without the need to recompile specific debug builds or providing
application-specific profiling/debugging code.
More details in *note Instrumenting CPython with DTrace and SystemTap:
50c.
The current implementation is tested on Linux and macOS. Additional
markers may be added in the future.
(Contributed by Łukasz Langa in bpo-21590(1), based on patches by Jesús
Cea Avión, David Malcolm, and Nikhil Benesch.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21590
File: python.info, Node: Other Language Changes<3>, Next: New Modules<3>, Prev: New Features<3>, Up: What’s New In Python 3 6
1.3.3 Other Language Changes
----------------------------
Some smaller changes made to the core Python language are:
* A ‘global’ or ‘nonlocal’ statement must now textually appear before
the first use of the affected name in the same scope. Previously
this was a *note SyntaxWarning: 191.
* It is now possible to set a *note special method: 50e. to ‘None’ to
indicate that the corresponding operation is not available. For
example, if a class sets *note __iter__(): 50f. to ‘None’, the
class is not iterable. (Contributed by Andrew Barnert and Ivan
Levkivskyi in bpo-25958(1).)
* Long sequences of repeated traceback lines are now abbreviated as
‘"[Previous line repeated {count} more times]"’ (see *note
traceback: 510. for an example). (Contributed by Emanuel Barry in
bpo-26823(2).)
* Import now raises the new exception *note ModuleNotFoundError: 39a.
(subclass of *note ImportError: 334.) when it cannot find a module.
Code that currently checks for ImportError (in try-except) will
still work. (Contributed by Eric Snow in bpo-15767(3).)
* Class methods relying on zero-argument ‘super()’ will now work
correctly when called from metaclass methods during class creation.
(Contributed by Martin Teichmann in bpo-23722(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25958
(2) https://bugs.python.org/issue26823
(3) https://bugs.python.org/issue15767
(4) https://bugs.python.org/issue23722
File: python.info, Node: New Modules<3>, Next: Improved Modules<3>, Prev: Other Language Changes<3>, Up: What’s New In Python 3 6
1.3.4 New Modules
-----------------
* Menu:
* secrets::
File: python.info, Node: secrets, Up: New Modules<3>
1.3.4.1 secrets
...............
The main purpose of the new *note secrets: e4. module is to provide an
obvious way to reliably generate cryptographically strong pseudo-random
values suitable for managing secrets, such as account authentication,
tokens, and similar.
Warning: Note that the pseudo-random generators in the *note
random: dc. module should `NOT' be used for security purposes. Use
*note secrets: e4. on Python 3.6+ and *note os.urandom(): 4d1. on
Python 3.5 and earlier.
See also
........
PEP 506(1) – Adding A Secrets Module To The Standard Library
PEP written and implemented by Steven D’Aprano.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0506
File: python.info, Node: Improved Modules<3>, Next: Optimizations<3>, Prev: New Modules<3>, Up: What’s New In Python 3 6
1.3.5 Improved Modules
----------------------
* Menu:
* array::
* ast: ast<2>.
* asyncio: asyncio<4>.
* binascii: binascii<2>.
* cmath::
* collections: collections<4>.
* concurrent.futures: concurrent futures<2>.
* contextlib: contextlib<2>.
* datetime: datetime<3>.
* decimal: decimal<2>.
* distutils: distutils<2>.
* email::
* encodings::
* enum: enum<3>.
* faulthandler::
* fileinput::
* hashlib::
* http.client: http client<2>.
* idlelib and IDLE: idlelib and IDLE<2>.
* importlib: importlib<3>.
* inspect: inspect<2>.
* json::
* logging: logging<3>.
* math: math<3>.
* multiprocessing: multiprocessing<3>.
* os: os<3>.
* pathlib: pathlib<3>.
* pdb: pdb<2>.
* pickle: pickle<2>.
* pickletools::
* pydoc: pydoc<2>.
* random::
* re: re<2>.
* readline::
* rlcompleter::
* shlex: shlex<2>.
* site::
* sqlite3: sqlite3<2>.
* socket: socket<4>.
* socketserver: socketserver<2>.
* ssl: ssl<4>.
* statistics: statistics<2>.
* struct::
* subprocess: subprocess<2>.
* sys: sys<4>.
* telnetlib::
* time: time<3>.
* timeit::
* tkinter: tkinter<3>.
* traceback::
* tracemalloc: tracemalloc<2>.
* typing: typing<2>.
* unicodedata: unicodedata<3>.
* unittest.mock: unittest mock<2>.
* urllib.request: urllib request.
* urllib.robotparser: urllib robotparser.
* venv: venv<2>.
* warnings: warnings<2>.
* winreg::
* winsound::
* xmlrpc.client: xmlrpc client.
* zipfile: zipfile<2>.
* zlib::
File: python.info, Node: array, Next: ast<2>, Up: Improved Modules<3>
1.3.5.1 array
.............
Exhausted iterators of *note array.array: 451. will now stay exhausted
even if the iterated array is extended. This is consistent with the
behavior of other mutable sequences.
Contributed by Serhiy Storchaka in bpo-26492(1).
---------- Footnotes ----------
(1) https://bugs.python.org/issue26492
File: python.info, Node: ast<2>, Next: asyncio<4>, Prev: array, Up: Improved Modules<3>
1.3.5.2 ast
...........
The new ‘ast.Constant’ AST node has been added. It can be used by
external AST optimizers for the purposes of constant folding.
Contributed by Victor Stinner in bpo-26146(1).
---------- Footnotes ----------
(1) https://bugs.python.org/issue26146
File: python.info, Node: asyncio<4>, Next: binascii<2>, Prev: ast<2>, Up: Improved Modules<3>
1.3.5.3 asyncio
...............
Starting with Python 3.6 the ‘asyncio’ module is no longer provisional
and its API is considered stable.
Notable changes in the *note asyncio: a. module since Python 3.5.0 (all
backported to 3.5.x due to the provisional status):
* The *note get_event_loop(): 355. function has been changed to
always return the currently running loop when called from
coroutines and callbacks. (Contributed by Yury Selivanov in
bpo-28613(1).)
* The *note ensure_future(): 517. function and all functions that use
it, such as *note loop.run_until_complete(): 518, now accept all
kinds of *note awaitable objects: 519. (Contributed by Yury
Selivanov.)
* New *note run_coroutine_threadsafe(): 51a. function to submit
coroutines to event loops from other threads. (Contributed by
Vincent Michel.)
* New *note Transport.is_closing(): 51b. method to check if the
transport is closing or closed. (Contributed by Yury Selivanov.)
* The *note loop.create_server(): 35d. method can now accept a list
of hosts. (Contributed by Yann Sionneau.)
* New *note loop.create_future(): 51c. method to create Future
objects. This allows alternative event loop implementations, such
as uvloop(2), to provide a faster *note asyncio.Future: 443.
implementation. (Contributed by Yury Selivanov in bpo-27041(3).)
* New *note loop.get_exception_handler(): 51d. method to get the
current exception handler. (Contributed by Yury Selivanov in
bpo-27040(4).)
* New *note StreamReader.readuntil(): 51e. method to read data from
the stream until a separator bytes sequence appears. (Contributed
by Mark Korenberg.)
* The performance of *note StreamReader.readexactly(): 51f. has been
improved. (Contributed by Mark Korenberg in bpo-28370(5).)
* The *note loop.getaddrinfo(): 4a1. method is optimized to avoid
calling the system ‘getaddrinfo’ function if the address is already
resolved. (Contributed by A. Jesse Jiryu Davis.)
* The *note loop.stop(): 520. method has been changed to stop the
loop immediately after the current iteration. Any new callbacks
scheduled as a result of the last iteration will be discarded.
(Contributed by Guido van Rossum in bpo-25593(6).)
* ‘Future.set_exception’ will now raise *note TypeError: 192. when
passed an instance of the *note StopIteration: 486. exception.
(Contributed by Chris Angelico in bpo-26221(7).)
* New *note loop.connect_accepted_socket(): 365. method to be used by
servers that accept connections outside of asyncio, but that use
asyncio to handle them. (Contributed by Jim Fulton in
bpo-27392(8).)
* ‘TCP_NODELAY’ flag is now set for all TCP transports by default.
(Contributed by Yury Selivanov in bpo-27456(9).)
* New *note loop.shutdown_asyncgens(): 521. to properly close pending
asynchronous generators before closing the loop. (Contributed by
Yury Selivanov in bpo-28003(10).)
* *note Future: 443. and *note Task: 1ad. classes now have an
optimized C implementation which makes asyncio code up to 30%
faster. (Contributed by Yury Selivanov and INADA Naoki in
bpo-26081(11) and bpo-28544(12).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28613
(2) https://github.com/MagicStack/uvloop
(3) https://bugs.python.org/issue27041
(4) https://bugs.python.org/issue27040
(5) https://bugs.python.org/issue28370
(6) https://bugs.python.org/issue25593
(7) https://bugs.python.org/issue26221
(8) https://bugs.python.org/issue27392
(9) https://bugs.python.org/issue27456
(10) https://bugs.python.org/issue28003
(11) https://bugs.python.org/issue26081
(12) https://bugs.python.org/issue28544
File: python.info, Node: binascii<2>, Next: cmath, Prev: asyncio<4>, Up: Improved Modules<3>
1.3.5.4 binascii
................
The *note b2a_base64(): 523. function now accepts an optional `newline'
keyword argument to control whether the newline character is appended to
the return value. (Contributed by Victor Stinner in bpo-25357(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25357
File: python.info, Node: cmath, Next: collections<4>, Prev: binascii<2>, Up: Improved Modules<3>
1.3.5.5 cmath
.............
The new *note cmath.tau: 525. (`τ') constant has been added.
(Contributed by Lisa Roach in bpo-12345(1), see PEP 628(2) for details.)
New constants: *note cmath.inf: 526. and *note cmath.nan: 527. to match
*note math.inf: 528. and *note math.nan: 529, and also *note cmath.infj:
52a. and *note cmath.nanj: 52b. to match the format used by complex
repr. (Contributed by Mark Dickinson in bpo-23229(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12345
(2) https://www.python.org/dev/peps/pep-0628
(3) https://bugs.python.org/issue23229
File: python.info, Node: collections<4>, Next: concurrent futures<2>, Prev: cmath, Up: Improved Modules<3>
1.3.5.6 collections
...................
The new *note Collection: 52d. abstract base class has been added to
represent sized iterable container classes. (Contributed by Ivan
Levkivskyi, docs by Neil Girdhar in bpo-27598(1).)
The new *note Reversible: 52e. abstract base class represents iterable
classes that also provide the *note __reversed__(): 52f. method.
(Contributed by Ivan Levkivskyi in bpo-25987(2).)
The new *note AsyncGenerator: 530. abstract base class represents
asynchronous generators. (Contributed by Yury Selivanov in
bpo-28720(3).)
The *note namedtuple(): 1b7. function now accepts an optional keyword
argument `module', which, when specified, is used for the ‘__module__’
attribute of the returned named tuple class. (Contributed by Raymond
Hettinger in bpo-17941(4).)
The `verbose' and `rename' arguments for *note namedtuple(): 1b7. are
now keyword-only. (Contributed by Raymond Hettinger in bpo-25628(5).)
Recursive *note collections.deque: 531. instances can now be pickled.
(Contributed by Serhiy Storchaka in bpo-26482(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27598
(2) https://bugs.python.org/issue25987
(3) https://bugs.python.org/issue28720
(4) https://bugs.python.org/issue17941
(5) https://bugs.python.org/issue25628
(6) https://bugs.python.org/issue26482
File: python.info, Node: concurrent futures<2>, Next: contextlib<2>, Prev: collections<4>, Up: Improved Modules<3>
1.3.5.7 concurrent.futures
..........................
The *note ThreadPoolExecutor: 27a. class constructor now accepts an
optional `thread_name_prefix' argument to make it possible to customize
the names of the threads created by the pool. (Contributed by Gregory
P. Smith in bpo-27664(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27664
File: python.info, Node: contextlib<2>, Next: datetime<3>, Prev: concurrent futures<2>, Up: Improved Modules<3>
1.3.5.8 contextlib
..................
The *note contextlib.AbstractContextManager: 534. class has been added
to provide an abstract base class for context managers. It provides a
sensible default implementation for ‘__enter__()’ which returns ‘self’
and leaves ‘__exit__()’ an abstract method. A matching class has been
added to the *note typing: 119. module as *note typing.ContextManager:
535. (Contributed by Brett Cannon in bpo-25609(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25609
File: python.info, Node: datetime<3>, Next: decimal<2>, Prev: contextlib<2>, Up: Improved Modules<3>
1.3.5.9 datetime
................
The *note datetime: 194. and *note time: 4f3. classes have the new
‘fold’ attribute used to disambiguate local time when necessary. Many
functions in the *note datetime: 31. have been updated to support local
time disambiguation. See *note Local Time Disambiguation: 4ce. section
for more information. (Contributed by Alexander Belopolsky in
bpo-24773(1).)
The *note datetime.strftime(): 537. and *note date.strftime(): 538.
methods now support ISO 8601 date directives ‘%G’, ‘%u’ and ‘%V’.
(Contributed by Ashley Anderson in bpo-12006(2).)
The *note datetime.isoformat(): 37f. function now accepts an optional
`timespec' argument that specifies the number of additional components
of the time value to include. (Contributed by Alessandro Cucci and
Alexander Belopolsky in bpo-19475(3).)
The *note datetime.combine(): 539. now accepts an optional `tzinfo'
argument. (Contributed by Alexander Belopolsky in bpo-27661(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24773
(2) https://bugs.python.org/issue12006
(3) https://bugs.python.org/issue19475
(4) https://bugs.python.org/issue27661
File: python.info, Node: decimal<2>, Next: distutils<2>, Prev: datetime<3>, Up: Improved Modules<3>
1.3.5.10 decimal
................
New *note Decimal.as_integer_ratio(): 53b. method that returns a pair
‘(n, d)’ of integers that represent the given *note Decimal: 188.
instance as a fraction, in lowest terms and with a positive denominator:
>>> Decimal('-3.14').as_integer_ratio()
(-157, 50)
(Contributed by Stefan Krah amd Mark Dickinson in bpo-25928(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25928
File: python.info, Node: distutils<2>, Next: email, Prev: decimal<2>, Up: Improved Modules<3>
1.3.5.11 distutils
..................
The ‘default_format’ attribute has been removed from
‘distutils.command.sdist.sdist’ and the ‘formats’ attribute defaults to
‘['gztar']’. Although not anticipated, any code relying on the presence
of ‘default_format’ may need to be adapted. See bpo-27819(1) for more
details.
---------- Footnotes ----------
(1) https://bugs.python.org/issue27819
File: python.info, Node: email, Next: encodings, Prev: distutils<2>, Up: Improved Modules<3>
1.3.5.12 email
..............
The new email API, enabled via the `policy' keyword to various
constructors, is no longer provisional. The *note email: 69.
documentation has been reorganized and rewritten to focus on the new
API, while retaining the old documentation for the legacy API.
(Contributed by R. David Murray in bpo-24277(1).)
The *note email.mime: 73. classes now all accept an optional `policy'
keyword. (Contributed by Berker Peksag in bpo-27331(2).)
The *note DecodedGenerator: 53e. now supports the `policy' keyword.
There is a new *note policy: 75. attribute, *note message_factory: 53f,
that controls what class is used by default when the parser creates new
message objects. For the *note email.policy.compat32: 540. policy this
is *note Message: 541, for the new policies it is *note EmailMessage:
542. (Contributed by R. David Murray in bpo-20476(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24277
(2) https://bugs.python.org/issue27331
(3) https://bugs.python.org/issue20476
File: python.info, Node: encodings, Next: enum<3>, Prev: email, Up: Improved Modules<3>
1.3.5.13 encodings
..................
On Windows, added the ‘'oem'’ encoding to use ‘CP_OEMCP’, and the
‘'ansi'’ alias for the existing ‘'mbcs'’ encoding, which uses the
‘CP_ACP’ code page. (Contributed by Steve Dower in bpo-27959(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27959
File: python.info, Node: enum<3>, Next: faulthandler, Prev: encodings, Up: Improved Modules<3>
1.3.5.14 enum
.............
Two new enumeration base classes have been added to the *note enum: 7b.
module: *note Flag: 545. and ‘IntFlags’. Both are used to define
constants that can be combined using the bitwise operators.
(Contributed by Ethan Furman in bpo-23591(1).)
Many standard library modules have been updated to use the ‘IntFlags’
class for their constants.
The new *note enum.auto: 546. value can be used to assign values to enum
members automatically:
>>> from enum import Enum, auto
>>> class Color(Enum):
... red = auto()
... blue = auto()
... green = auto()
...
>>> list(Color)
[, , ]
---------- Footnotes ----------
(1) https://bugs.python.org/issue23591
File: python.info, Node: faulthandler, Next: fileinput, Prev: enum<3>, Up: Improved Modules<3>
1.3.5.15 faulthandler
.....................
On Windows, the *note faulthandler: 7d. module now installs a handler
for Windows exceptions: see *note faulthandler.enable(): 548.
(Contributed by Victor Stinner in bpo-23848(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23848
File: python.info, Node: fileinput, Next: hashlib, Prev: faulthandler, Up: Improved Modules<3>
1.3.5.16 fileinput
..................
*note hook_encoded(): 54a. now supports the `errors' argument.
(Contributed by Joseph Hackman in bpo-25788(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25788
File: python.info, Node: hashlib, Next: http client<2>, Prev: fileinput, Up: Improved Modules<3>
1.3.5.17 hashlib
................
*note hashlib: 8d. supports OpenSSL 1.1.0. The minimum recommend
version is 1.0.2. (Contributed by Christian Heimes in bpo-26470(1).)
BLAKE2 hash functions were added to the module. *note blake2b(): 54c.
and *note blake2s(): 54d. are always available and support the full
feature set of BLAKE2. (Contributed by Christian Heimes in bpo-26798(2)
based on code by Dmitry Chestnykh and Samuel Neves. Documentation
written by Dmitry Chestnykh.)
The SHA-3 hash functions ‘sha3_224()’, ‘sha3_256()’, ‘sha3_384()’,
‘sha3_512()’, and SHAKE hash functions ‘shake_128()’ and ‘shake_256()’
were added. (Contributed by Christian Heimes in bpo-16113(3). Keccak
Code Package by Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van
Assche, and Ronny Van Keer.)
The password-based key derivation function *note scrypt(): 4d2. is now
available with OpenSSL 1.1.0 and newer. (Contributed by Christian
Heimes in bpo-27928(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26470
(2) https://bugs.python.org/issue26798
(3) https://bugs.python.org/issue16113
(4) https://bugs.python.org/issue27928
File: python.info, Node: http client<2>, Next: idlelib and IDLE<2>, Prev: hashlib, Up: Improved Modules<3>
1.3.5.18 http.client
....................
*note HTTPConnection.request(): 54f. and *note endheaders(): 550. both
now support chunked encoding request bodies. (Contributed by Demian
Brecht and Rolf Krahl in bpo-12319(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12319
File: python.info, Node: idlelib and IDLE<2>, Next: importlib<3>, Prev: http client<2>, Up: Improved Modules<3>
1.3.5.19 idlelib and IDLE
.........................
The idlelib package is being modernized and refactored to make IDLE look
and work better and to make the code easier to understand, test, and
improve. Part of making IDLE look better, especially on Linux and Mac,
is using ttk widgets, mostly in the dialogs. As a result, IDLE no
longer runs with tcl/tk 8.4. It now requires tcl/tk 8.5 or 8.6. We
recommend running the latest release of either.
‘Modernizing’ includes renaming and consolidation of idlelib modules.
The renaming of files with partial uppercase names is similar to the
renaming of, for instance, Tkinter and TkFont to tkinter and
tkinter.font in 3.0. As a result, imports of idlelib files that worked
in 3.5 will usually not work in 3.6. At least a module name change will
be needed (see idlelib/README.txt), sometimes more. (Name changes
contributed by Al Swiegart and Terry Reedy in bpo-24225(1). Most
idlelib patches since have been and will be part of the process.)
In compensation, the eventual result with be that some idlelib classes
will be easier to use, with better APIs and docstrings explaining them.
Additional useful information will be added to idlelib when available.
New in 3.6.2:
Multiple fixes for autocompletion. (Contributed by Louie Lu in
bpo-15786(2).)
New in 3.6.3:
Module Browser (on the File menu, formerly called Class Browser), now
displays nested functions and classes in addition to top-level functions
and classes. (Contributed by Guilherme Polo, Cheryl Sabella, and Terry
Jan Reedy in bpo-1612262(3).)
The IDLE features formerly implemented as extensions have been
reimplemented as normal features. Their settings have been moved from
the Extensions tab to other dialog tabs. (Contributed by Charles
Wohlganger and Terry Jan Reedy in bpo-27099(4).)
The Settings dialog (Options, Configure IDLE) has been partly rewritten
to improve both appearance and function. (Contributed by Cheryl Sabella
and Terry Jan Reedy in multiple issues.)
New in 3.6.4:
The font sample now includes a selection of non-Latin characters so that
users can better see the effect of selecting a particular font.
(Contributed by Terry Jan Reedy in bpo-13802(5).) The sample can be
edited to include other characters. (Contributed by Serhiy Storchaka in
bpo-31860(6).)
New in 3.6.6:
Editor code context option revised. Box displays all context lines up
to maxlines. Clicking on a context line jumps the editor to that line.
Context colors for custom themes is added to Highlights tab of Settings
dialog. (Contributed by Cheryl Sabella and Terry Jan Reedy in
bpo-33642(7), bpo-33768(8), and bpo-33679(9).)
On Windows, a new API call tells Windows that tk scales for DPI. On
Windows 8.1+ or 10, with DPI compatibility properties of the Python
binary unchanged, and a monitor resolution greater than 96 DPI, this
should make text and lines sharper. It should otherwise have no effect.
(Contributed by Terry Jan Reedy in bpo-33656(10).)
New in 3.6.7:
Output over N lines (50 by default) is squeezed down to a button. N can
be changed in the PyShell section of the General page of the Settings
dialog. Fewer, but possibly extra long, lines can be squeezed by right
clicking on the output. Squeezed output can be expanded in place by
double-clicking the button or into the clipboard or a separate window by
right-clicking the button. (Contributed by Tal Einat in
bpo-1529353(11).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24225
(2) https://bugs.python.org/issue15786
(3) https://bugs.python.org/issue1612262
(4) https://bugs.python.org/issue27099
(5) https://bugs.python.org/issue13802
(6) https://bugs.python.org/issue31860
(7) https://bugs.python.org/issue33642
(8) https://bugs.python.org/issue33768
(9) https://bugs.python.org/issue33679
(10) https://bugs.python.org/issue33656
(11) https://bugs.python.org/issue1529353
File: python.info, Node: importlib<3>, Next: inspect<2>, Prev: idlelib and IDLE<2>, Up: Improved Modules<3>
1.3.5.20 importlib
..................
Import now raises the new exception *note ModuleNotFoundError: 39a.
(subclass of *note ImportError: 334.) when it cannot find a module.
Code that current checks for ‘ImportError’ (in try-except) will still
work. (Contributed by Eric Snow in bpo-15767(1).)
*note importlib.util.LazyLoader: 553. now calls *note create_module():
554. on the wrapped loader, removing the restriction that *note
importlib.machinery.BuiltinImporter: 555. and *note
importlib.machinery.ExtensionFileLoader: 556. couldn’t be used with
*note importlib.util.LazyLoader: 553.
*note importlib.util.cache_from_source(): 557, *note
importlib.util.source_from_cache(): 558, and *note
importlib.util.spec_from_file_location(): 559. now accept a *note
path-like object: 36a.
---------- Footnotes ----------
(1) https://bugs.python.org/issue15767
File: python.info, Node: inspect<2>, Next: json, Prev: importlib<3>, Up: Improved Modules<3>
1.3.5.21 inspect
................
The *note inspect.signature(): 55b. function now reports the implicit
‘.0’ parameters generated by the compiler for comprehension and
generator expression scopes as if they were positional-only parameters
called ‘implicit0’. (Contributed by Jelle Zijlstra in bpo-19611(1).)
To reduce code churn when upgrading from Python 2.7 and the legacy *note
inspect.getargspec(): 55c. API, the previously documented deprecation of
*note inspect.getfullargspec(): 55d. has been reversed. While this
function is convenient for single/source Python 2/3 code bases, the
richer *note inspect.signature(): 55b. interface remains the recommended
approach for new code. (Contributed by Nick Coghlan in bpo-27172(2))
---------- Footnotes ----------
(1) https://bugs.python.org/issue19611
(2) https://bugs.python.org/issue27172
File: python.info, Node: json, Next: logging<3>, Prev: inspect<2>, Up: Improved Modules<3>
1.3.5.22 json
.............
*note json.load(): 55f. and *note json.loads(): 560. now support binary
input. Encoded JSON should be represented using either UTF-8, UTF-16,
or UTF-32. (Contributed by Serhiy Storchaka in bpo-17909(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17909
File: python.info, Node: logging<3>, Next: math<3>, Prev: json, Up: Improved Modules<3>
1.3.5.23 logging
................
The new *note WatchedFileHandler.reopenIfNeeded(): 562. method has been
added to add the ability to check if the log file needs to be reopened.
(Contributed by Marian Horban in bpo-24884(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24884
File: python.info, Node: math<3>, Next: multiprocessing<3>, Prev: logging<3>, Up: Improved Modules<3>
1.3.5.24 math
.............
The tau (`τ') constant has been added to the *note math: b1. and *note
cmath: 19. modules. (Contributed by Lisa Roach in bpo-12345(1), see PEP
628(2) for details.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12345
(2) https://www.python.org/dev/peps/pep-0628
File: python.info, Node: multiprocessing<3>, Next: os<3>, Prev: math<3>, Up: Improved Modules<3>
1.3.5.25 multiprocessing
........................
*note Proxy Objects: 565. returned by ‘multiprocessing.Manager()’ can
now be nested. (Contributed by Davin Potts in bpo-6766(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6766
File: python.info, Node: os<3>, Next: pathlib<3>, Prev: multiprocessing<3>, Up: Improved Modules<3>
1.3.5.26 os
...........
See the summary of *note PEP 519: 4cd. for details on how the *note os:
c4. and *note os.path: c5. modules now support *note path-like objects:
36a.
*note scandir(): 25f. now supports *note bytes: 331. paths on Windows.
A new *note close(): 567. method allows explicitly closing a *note
scandir(): 25f. iterator. The *note scandir(): 25f. iterator now
supports the *note context manager: 568. protocol. If a ‘scandir()’
iterator is neither exhausted nor explicitly closed a *note
ResourceWarning: 4d0. will be emitted in its destructor. (Contributed
by Serhiy Storchaka in bpo-25994(1).)
On Linux, *note os.urandom(): 4d1. now blocks until the system urandom
entropy pool is initialized to increase the security. See the PEP
524(2) for the rationale.
The Linux ‘getrandom()’ syscall (get random bytes) is now exposed as the
new *note os.getrandom(): 569. function. (Contributed by Victor
Stinner, part of the PEP 524(3))
---------- Footnotes ----------
(1) https://bugs.python.org/issue25994
(2) https://www.python.org/dev/peps/pep-0524
(3) https://www.python.org/dev/peps/pep-0524
File: python.info, Node: pathlib<3>, Next: pdb<2>, Prev: os<3>, Up: Improved Modules<3>
1.3.5.27 pathlib
................
*note pathlib: c8. now supports *note path-like objects: 36a.
(Contributed by Brett Cannon in bpo-27186(1).)
See the summary of *note PEP 519: 4cd. for details.
---------- Footnotes ----------
(1) https://bugs.python.org/issue27186
File: python.info, Node: pdb<2>, Next: pickle<2>, Prev: pathlib<3>, Up: Improved Modules<3>
1.3.5.28 pdb
............
The *note Pdb: 56c. class constructor has a new optional `readrc'
argument to control whether ‘.pdbrc’ files should be read.
File: python.info, Node: pickle<2>, Next: pickletools, Prev: pdb<2>, Up: Improved Modules<3>
1.3.5.29 pickle
...............
Objects that need ‘__new__’ called with keyword arguments can now be
pickled using *note pickle protocols: 56e. older than protocol version
4. Protocol version 4 already supports this case. (Contributed by
Serhiy Storchaka in bpo-24164(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24164
File: python.info, Node: pickletools, Next: pydoc<2>, Prev: pickle<2>, Up: Improved Modules<3>
1.3.5.30 pickletools
....................
*note pickletools.dis(): 570. now outputs the implicit memo index for
the ‘MEMOIZE’ opcode. (Contributed by Serhiy Storchaka in
bpo-25382(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25382
File: python.info, Node: pydoc<2>, Next: random, Prev: pickletools, Up: Improved Modules<3>
1.3.5.31 pydoc
..............
The *note pydoc: d9. module has learned to respect the ‘MANPAGER’
environment variable. (Contributed by Matthias Klose in bpo-8637(1).)
*note help(): 572. and *note pydoc: d9. can now list named tuple fields
in the order they were defined rather than alphabetically. (Contributed
by Raymond Hettinger in bpo-24879(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8637
(2) https://bugs.python.org/issue24879
File: python.info, Node: random, Next: re<2>, Prev: pydoc<2>, Up: Improved Modules<3>
1.3.5.32 random
...............
The new *note choices(): 574. function returns a list of elements of
specified size from the given population with optional weights.
(Contributed by Raymond Hettinger in bpo-18844(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18844
File: python.info, Node: re<2>, Next: readline, Prev: random, Up: Improved Modules<3>
1.3.5.33 re
...........
Added support of modifier spans in regular expressions. Examples:
‘'(?i:p)ython'’ matches ‘'python'’ and ‘'Python'’, but not ‘'PYTHON'’;
‘'(?i)g(?-i:v)r'’ matches ‘'GvR'’ and ‘'gvr'’, but not ‘'GVR'’.
(Contributed by Serhiy Storchaka in bpo-433028(1).)
Match object groups can be accessed by ‘__getitem__’, which is
equivalent to ‘group()’. So ‘mo['name']’ is now equivalent to
‘mo.group('name')’. (Contributed by Eric Smith in bpo-24454(2).)
‘Match’ objects now support *note index-like objects: 18a. as group
indices. (Contributed by Jeroen Demeyer and Xiang Zhang in
bpo-27177(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue433028
(2) https://bugs.python.org/issue24454
(3) https://bugs.python.org/issue27177
File: python.info, Node: readline, Next: rlcompleter, Prev: re<2>, Up: Improved Modules<3>
1.3.5.34 readline
.................
Added *note set_auto_history(): 577. to enable or disable automatic
addition of input to the history list. (Contributed by Tyler Crompton
in bpo-26870(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26870
File: python.info, Node: rlcompleter, Next: shlex<2>, Prev: readline, Up: Improved Modules<3>
1.3.5.35 rlcompleter
....................
Private and special attribute names now are omitted unless the prefix
starts with underscores. A space or a colon is added after some
completed keywords. (Contributed by Serhiy Storchaka in bpo-25011(1)
and bpo-25209(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25011
(2) https://bugs.python.org/issue25209
File: python.info, Node: shlex<2>, Next: site, Prev: rlcompleter, Up: Improved Modules<3>
1.3.5.36 shlex
..............
The *note shlex: 57a. has much *note improved shell compatibility: 57b.
through the new `punctuation_chars' argument to control which characters
are treated as punctuation. (Contributed by Vinay Sajip in
bpo-1521950(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1521950
File: python.info, Node: site, Next: sqlite3<2>, Prev: shlex<2>, Up: Improved Modules<3>
1.3.5.37 site
.............
When specifying paths to add to *note sys.path: 488. in a ‘.pth’ file,
you may now specify file paths on top of directories (e.g. zip files).
(Contributed by Wolfgang Langner in bpo-26587(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue26587
File: python.info, Node: sqlite3<2>, Next: socket<4>, Prev: site, Up: Improved Modules<3>
1.3.5.38 sqlite3
................
*note sqlite3.Cursor.lastrowid: 57e. now supports the ‘REPLACE’
statement. (Contributed by Alex LordThorsen in bpo-16864(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16864
File: python.info, Node: socket<4>, Next: socketserver<2>, Prev: sqlite3<2>, Up: Improved Modules<3>
1.3.5.39 socket
...............
The *note ioctl(): 580. function now supports the *note
SIO_LOOPBACK_FAST_PATH: 581. control code. (Contributed by Daniel
Stokes in bpo-26536(1).)
The *note getsockopt(): 582. constants ‘SO_DOMAIN’, ‘SO_PROTOCOL’,
‘SO_PEERSEC’, and ‘SO_PASSSEC’ are now supported. (Contributed by
Christian Heimes in bpo-26907(2).)
The *note setsockopt(): 583. now supports the ‘setsockopt(level,
optname, None, optlen: int)’ form. (Contributed by Christian Heimes in
bpo-27744(3).)
The socket module now supports the address family *note AF_ALG: 584. to
interface with Linux Kernel crypto API. ‘ALG_*’, ‘SOL_ALG’ and *note
sendmsg_afalg(): 585. were added. (Contributed by Christian Heimes in
bpo-27744(4) with support from Victor Stinner.)
New Linux constants ‘TCP_USER_TIMEOUT’ and ‘TCP_CONGESTION’ were added.
(Contributed by Omar Sandoval, issue:‘26273’).
---------- Footnotes ----------
(1) https://bugs.python.org/issue26536
(2) https://bugs.python.org/issue26907
(3) https://bugs.python.org/issue27744
(4) https://bugs.python.org/issue27744
File: python.info, Node: socketserver<2>, Next: ssl<4>, Prev: socket<4>, Up: Improved Modules<3>
1.3.5.40 socketserver
.....................
Servers based on the *note socketserver: f0. module, including those
defined in *note http.server: 97, *note xmlrpc.server: 140. and *note
wsgiref.simple_server: 12f, now support the *note context manager: 568.
protocol. (Contributed by Aviv Palivoda in bpo-26404(1).)
The ‘wfile’ attribute of *note StreamRequestHandler: 587. classes now
implements the *note io.BufferedIOBase: 588. writable interface. In
particular, calling *note write(): 589. is now guaranteed to send the
data in full. (Contributed by Martin Panter in bpo-26721(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26404
(2) https://bugs.python.org/issue26721
File: python.info, Node: ssl<4>, Next: statistics<2>, Prev: socketserver<2>, Up: Improved Modules<3>
1.3.5.41 ssl
............
*note ssl: f3. supports OpenSSL 1.1.0. The minimum recommend version is
1.0.2. (Contributed by Christian Heimes in bpo-26470(1).)
3DES has been removed from the default cipher suites and ChaCha20
Poly1305 cipher suites have been added. (Contributed by Christian
Heimes in bpo-27850(2) and bpo-27766(3).)
*note SSLContext: 3e4. has better default configuration for options and
ciphers. (Contributed by Christian Heimes in bpo-28043(4).)
SSL session can be copied from one client-side connection to another
with the new *note SSLSession: 58b. class. TLS session resumption can
speed up the initial handshake, reduce latency and improve performance
(Contributed by Christian Heimes in bpo-19500(5) based on a draft by
Alex Warhawk.)
The new *note get_ciphers(): 58c. method can be used to get a list of
enabled ciphers in order of cipher priority.
All constants and flags have been converted to *note IntEnum: 58d. and
‘IntFlags’. (Contributed by Christian Heimes in bpo-28025(6).)
Server and client-side specific TLS protocols for *note SSLContext: 3e4.
were added. (Contributed by Christian Heimes in bpo-28085(7).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26470
(2) https://bugs.python.org/issue27850
(3) https://bugs.python.org/issue27766
(4) https://bugs.python.org/issue28043
(5) https://bugs.python.org/issue19500
(6) https://bugs.python.org/issue28025
(7) https://bugs.python.org/issue28085
File: python.info, Node: statistics<2>, Next: struct, Prev: ssl<4>, Up: Improved Modules<3>
1.3.5.42 statistics
...................
A new *note harmonic_mean(): 58f. function has been added. (Contributed
by Steven D’Aprano in bpo-27181(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27181
File: python.info, Node: struct, Next: subprocess<2>, Prev: statistics<2>, Up: Improved Modules<3>
1.3.5.43 struct
...............
*note struct: f8. now supports IEEE 754 half-precision floats via the
‘'e'’ format specifier. (Contributed by Eli Stevens, Mark Dickinson in
bpo-11734(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11734
File: python.info, Node: subprocess<2>, Next: sys<4>, Prev: struct, Up: Improved Modules<3>
1.3.5.44 subprocess
...................
*note subprocess.Popen: 2b9. destructor now emits a *note
ResourceWarning: 4d0. warning if the child process is still running.
Use the context manager protocol (‘with proc: ...’) or explicitly call
the *note wait(): 592. method to read the exit status of the child
process. (Contributed by Victor Stinner in bpo-26741(1).)
The *note subprocess.Popen: 2b9. constructor and all functions that pass
arguments through to it now accept `encoding' and `errors' arguments.
Specifying either of these will enable text mode for the `stdin',
`stdout' and `stderr' streams. (Contributed by Steve Dower in
bpo-6135(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26741
(2) https://bugs.python.org/issue6135
File: python.info, Node: sys<4>, Next: telnetlib, Prev: subprocess<2>, Up: Improved Modules<3>
1.3.5.45 sys
............
The new *note getfilesystemencodeerrors(): 594. function returns the
name of the error mode used to convert between Unicode filenames and
bytes filenames. (Contributed by Steve Dower in bpo-27781(1).)
On Windows the return value of the *note getwindowsversion(): 595.
function now includes the `platform_version' field which contains the
accurate major version, minor version and build number of the current
operating system, rather than the version that is being emulated for the
process (Contributed by Steve Dower in bpo-27932(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27781
(2) https://bugs.python.org/issue27932
File: python.info, Node: telnetlib, Next: time<3>, Prev: sys<4>, Up: Improved Modules<3>
1.3.5.46 telnetlib
..................
*note Telnet: 597. is now a context manager (contributed by Stéphane
Wirtel in bpo-25485(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue25485
File: python.info, Node: time<3>, Next: timeit, Prev: telnetlib, Up: Improved Modules<3>
1.3.5.47 time
.............
The *note struct_time: 599. attributes ‘tm_gmtoff’ and ‘tm_zone’ are now
available on all platforms.
File: python.info, Node: timeit, Next: tkinter<3>, Prev: time<3>, Up: Improved Modules<3>
1.3.5.48 timeit
...............
The new *note Timer.autorange(): 59b. convenience method has been added
to call *note Timer.timeit(): 59c. repeatedly so that the total run time
is greater or equal to 200 milliseconds. (Contributed by Steven
D’Aprano in bpo-6422(1).)
*note timeit: 10b. now warns when there is substantial (4x) variance
between best and worst times. (Contributed by Serhiy Storchaka in
bpo-23552(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6422
(2) https://bugs.python.org/issue23552
File: python.info, Node: tkinter<3>, Next: traceback, Prev: timeit, Up: Improved Modules<3>
1.3.5.49 tkinter
................
Added methods ‘trace_add()’, ‘trace_remove()’ and ‘trace_info()’ in the
‘tkinter.Variable’ class. They replace old methods ‘trace_variable()’,
‘trace()’, ‘trace_vdelete()’ and ‘trace_vinfo()’ that use obsolete Tcl
commands and might not work in future versions of Tcl. (Contributed by
Serhiy Storchaka in bpo-22115(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue22115
File: python.info, Node: traceback, Next: tracemalloc<2>, Prev: tkinter<3>, Up: Improved Modules<3>
1.3.5.50 traceback
..................
Both the traceback module and the interpreter’s builtin exception
display now abbreviate long sequences of repeated lines in tracebacks as
shown in the following example:
>>> def f(): f()
...
>>> f()
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in f
File "", line 1, in f
File "", line 1, in f
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded
(Contributed by Emanuel Barry in bpo-26823(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26823
File: python.info, Node: tracemalloc<2>, Next: typing<2>, Prev: traceback, Up: Improved Modules<3>
1.3.5.51 tracemalloc
....................
The *note tracemalloc: 114. module now supports tracing memory
allocations in multiple different address spaces.
The new *note DomainFilter: 5a0. filter class has been added to filter
block traces by their address space (domain).
(Contributed by Victor Stinner in bpo-26588(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26588
File: python.info, Node: typing<2>, Next: unicodedata<3>, Prev: tracemalloc<2>, Up: Improved Modules<3>
1.3.5.52 typing
...............
Since the *note typing: 119. module is *note provisional: 348, all
changes introduced in Python 3.6 have also been backported to Python
3.5.x.
The *note typing: 119. module has a much improved support for generic
type aliases. For example ‘Dict[str, Tuple[S, T]]’ is now a valid type
annotation. (Contributed by Guido van Rossum in Github #195(1).)
The *note typing.ContextManager: 535. class has been added for
representing *note contextlib.AbstractContextManager: 534. (Contributed
by Brett Cannon in bpo-25609(2).)
The *note typing.Collection: 5a2. class has been added for representing
*note collections.abc.Collection: 52d. (Contributed by Ivan Levkivskyi
in bpo-27598(3).)
The *note typing.ClassVar: 5a3. type construct has been added to mark
class variables. As introduced in PEP 526(4), a variable annotation
wrapped in ClassVar indicates that a given attribute is intended to be
used as a class variable and should not be set on instances of that
class. (Contributed by Ivan Levkivskyi in Github #280(5).)
A new *note TYPE_CHECKING: 5a4. constant that is assumed to be ‘True’ by
the static type checkers, but is ‘False’ at runtime. (Contributed by
Guido van Rossum in Github #230(6).)
A new *note NewType(): 5a5. helper function has been added to create
lightweight distinct types for annotations:
from typing import NewType
UserId = NewType('UserId', int)
some_id = UserId(524313)
The static type checker will treat the new type as if it were a subclass
of the original type. (Contributed by Ivan Levkivskyi in Github
#189(7).)
---------- Footnotes ----------
(1) https://github.com/python/typing/pull/195
(2) https://bugs.python.org/issue25609
(3) https://bugs.python.org/issue27598
(4) https://www.python.org/dev/peps/pep-0526
(5) https://github.com/python/typing/pull/280
(6) https://github.com/python/typing/issues/230
(7) https://github.com/python/typing/issues/189
File: python.info, Node: unicodedata<3>, Next: unittest mock<2>, Prev: typing<2>, Up: Improved Modules<3>
1.3.5.53 unicodedata
....................
The *note unicodedata: 11a. module now uses data from Unicode 9.0.0(1).
(Contributed by Benjamin Peterson.)
---------- Footnotes ----------
(1) http://unicode.org/versions/Unicode9.0.0/
File: python.info, Node: unittest mock<2>, Next: urllib request, Prev: unicodedata<3>, Up: Improved Modules<3>
1.3.5.54 unittest.mock
......................
The *note Mock: 249. class has the following improvements:
* Two new methods, *note Mock.assert_called(): 5a8. and *note
Mock.assert_called_once(): 5a9. to check if the mock object was
called. (Contributed by Amit Saha in bpo-26323(1).)
* The *note Mock.reset_mock(): 5aa. method now has two optional
keyword only arguments: `return_value' and `side_effect'.
(Contributed by Kushal Das in bpo-21271(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26323
(2) https://bugs.python.org/issue21271
File: python.info, Node: urllib request, Next: urllib robotparser, Prev: unittest mock<2>, Up: Improved Modules<3>
1.3.5.55 urllib.request
.......................
If a HTTP request has a file or iterable body (other than a bytes
object) but no ‘Content-Length’ header, rather than throwing an error,
‘AbstractHTTPHandler’ now falls back to use chunked transfer encoding.
(Contributed by Demian Brecht and Rolf Krahl in bpo-12319(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12319
File: python.info, Node: urllib robotparser, Next: venv<2>, Prev: urllib request, Up: Improved Modules<3>
1.3.5.56 urllib.robotparser
...........................
*note RobotFileParser: 5ad. now supports the ‘Crawl-delay’ and
‘Request-rate’ extensions. (Contributed by Nikolay Bogoychev in
bpo-16099(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16099
File: python.info, Node: venv<2>, Next: warnings<2>, Prev: urllib robotparser, Up: Improved Modules<3>
1.3.5.57 venv
.............
*note venv: 125. accepts a new parameter ‘--prompt’. This parameter
provides an alternative prefix for the virtual environment. (Proposed
by Łukasz Balcerzak and ported to 3.6 by Stéphane Wirtel in
bpo-22829(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22829
File: python.info, Node: warnings<2>, Next: winreg, Prev: venv<2>, Up: Improved Modules<3>
1.3.5.58 warnings
.................
A new optional `source' parameter has been added to the *note
warnings.warn_explicit(): 5b0. function: the destroyed object which
emitted a *note ResourceWarning: 4d0. A `source' attribute has also
been added to ‘warnings.WarningMessage’ (contributed by Victor Stinner
in bpo-26568(1) and bpo-26567(2)).
When a *note ResourceWarning: 4d0. warning is logged, the *note
tracemalloc: 114. module is now used to try to retrieve the traceback
where the destroyed object was allocated.
Example with the script ‘example.py’:
import warnings
def func():
return open(__file__)
f = func()
f = None
Output of the command ‘python3.6 -Wd -X tracemalloc=5 example.py’:
example.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='example.py' mode='r' encoding='UTF-8'>
f = None
Object allocated at (most recent call first):
File "example.py", lineno 4
return open(__file__)
File "example.py", lineno 6
f = func()
The “Object allocated at” traceback is new and is only displayed if
*note tracemalloc: 114. is tracing Python memory allocations and if the
*note warnings: 126. module was already imported.
---------- Footnotes ----------
(1) https://bugs.python.org/issue26568
(2) https://bugs.python.org/issue26567
File: python.info, Node: winreg, Next: winsound, Prev: warnings<2>, Up: Improved Modules<3>
1.3.5.59 winreg
...............
Added the 64-bit integer type *note REG_QWORD: 5b2. (Contributed by
Clement Rouault in bpo-23026(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23026
File: python.info, Node: winsound, Next: xmlrpc client, Prev: winreg, Up: Improved Modules<3>
1.3.5.60 winsound
.................
Allowed keyword arguments to be passed to *note Beep: 5b4, *note
MessageBeep: 5b5, and *note PlaySound: 5b6. (bpo-27982(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue27982
File: python.info, Node: xmlrpc client, Next: zipfile<2>, Prev: winsound, Up: Improved Modules<3>
1.3.5.61 xmlrpc.client
......................
The *note xmlrpc.client: 13f. module now supports unmarshalling
additional data types used by the Apache XML-RPC implementation for
numerics and ‘None’. (Contributed by Serhiy Storchaka in bpo-26885(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26885
File: python.info, Node: zipfile<2>, Next: zlib, Prev: xmlrpc client, Up: Improved Modules<3>
1.3.5.62 zipfile
................
A new *note ZipInfo.from_file(): 5b9. class method allows making a *note
ZipInfo: 5ba. instance from a filesystem file. A new *note
ZipInfo.is_dir(): 5bb. method can be used to check if the *note ZipInfo:
5ba. instance represents a directory. (Contributed by Thomas Kluyver in
bpo-26039(1).)
The *note ZipFile.open(): 5bc. method can now be used to write data into
a ZIP file, as well as for extracting data. (Contributed by Thomas
Kluyver in bpo-26039(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26039
(2) https://bugs.python.org/issue26039
File: python.info, Node: zlib, Prev: zipfile<2>, Up: Improved Modules<3>
1.3.5.63 zlib
.............
The *note compress(): 5be. and *note decompress(): 5bf. functions now
accept keyword arguments. (Contributed by Aviv Palivoda in bpo-26243(1)
and Xiang Zhang in bpo-16764(2) respectively.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26243
(2) https://bugs.python.org/issue16764
File: python.info, Node: Optimizations<3>, Next: Build and C API Changes<2>, Prev: Improved Modules<3>, Up: What’s New In Python 3 6
1.3.6 Optimizations
-------------------
* The Python interpreter now uses a 16-bit wordcode instead of
bytecode which made a number of opcode optimizations possible.
(Contributed by Demur Rumed with input and reviews from Serhiy
Storchaka and Victor Stinner in bpo-26647(1) and bpo-28050(2).)
* The *note asyncio.Future: 443. class now has an optimized C
implementation. (Contributed by Yury Selivanov and INADA Naoki in
bpo-26081(3).)
* The *note asyncio.Task: 1ad. class now has an optimized C
implementation. (Contributed by Yury Selivanov in bpo-28544(4).)
* Various implementation improvements in the *note typing: 119.
module (such as caching of generic types) allow up to 30 times
performance improvements and reduced memory footprint.
* The ASCII decoder is now up to 60 times as fast for error handlers
‘surrogateescape’, ‘ignore’ and ‘replace’ (Contributed by Victor
Stinner in bpo-24870(5)).
* The ASCII and the Latin1 encoders are now up to 3 times as fast for
the error handler ‘surrogateescape’ (Contributed by Victor Stinner
in bpo-25227(6)).
* The UTF-8 encoder is now up to 75 times as fast for error handlers
‘ignore’, ‘replace’, ‘surrogateescape’, ‘surrogatepass’
(Contributed by Victor Stinner in bpo-25267(7)).
* The UTF-8 decoder is now up to 15 times as fast for error handlers
‘ignore’, ‘replace’ and ‘surrogateescape’ (Contributed by Victor
Stinner in bpo-25301(8)).
* ‘bytes % args’ is now up to 2 times faster. (Contributed by Victor
Stinner in bpo-25349(9)).
* ‘bytearray % args’ is now between 2.5 and 5 times faster.
(Contributed by Victor Stinner in bpo-25399(10)).
* Optimize *note bytes.fromhex(): 32e. and *note bytearray.fromhex():
32f.: they are now between 2x and 3.5x faster. (Contributed by
Victor Stinner in bpo-25401(11)).
* Optimize ‘bytes.replace(b'', b'.')’ and ‘bytearray.replace(b'',
b'.')’: up to 80% faster. (Contributed by Josh Snider in
bpo-26574(12)).
* Allocator functions of the *note PyMem_Malloc(): 505. domain (*note
PYMEM_DOMAIN_MEM: 509.) now use the *note pymalloc memory
allocator: 5c1. instead of ‘malloc()’ function of the C library.
The pymalloc allocator is optimized for objects smaller or equal to
512 bytes with a short lifetime, and use ‘malloc()’ for larger
memory blocks. (Contributed by Victor Stinner in bpo-26249(13)).
* *note pickle.load(): 5c2. and *note pickle.loads(): 5c3. are now up
to 10% faster when deserializing many small objects (Contributed by
Victor Stinner in bpo-27056(14)).
* Passing *note keyword arguments: 5c4. to a function has an overhead
in comparison with passing *note positional arguments: 5c5. Now in
extension functions implemented with using Argument Clinic this
overhead is significantly decreased. (Contributed by Serhiy
Storchaka in bpo-27574(15)).
* Optimized *note glob(): 5c6. and *note iglob(): 5c7. functions in
the *note glob: 8a. module; they are now about 3–6 times faster.
(Contributed by Serhiy Storchaka in bpo-25596(16)).
* Optimized globbing in *note pathlib: c8. by using *note
os.scandir(): 25f.; it is now about 1.5–4 times faster.
(Contributed by Serhiy Storchaka in bpo-26032(17)).
* *note xml.etree.ElementTree: 137. parsing, iteration and deepcopy
performance has been significantly improved. (Contributed by
Serhiy Storchaka in bpo-25638(18), bpo-25873(19), and
bpo-25869(20).)
* Creation of *note fractions.Fraction: 185. instances from floats
and decimals is now 2 to 3 times faster. (Contributed by Serhiy
Storchaka in bpo-25971(21).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26647
(2) https://bugs.python.org/issue28050
(3) https://bugs.python.org/issue26081
(4) https://bugs.python.org/issue28544
(5) https://bugs.python.org/issue24870
(6) https://bugs.python.org/issue25227
(7) https://bugs.python.org/issue25267
(8) https://bugs.python.org/issue25301
(9) https://bugs.python.org/issue25349
(10) https://bugs.python.org/issue25399
(11) https://bugs.python.org/issue25401
(12) https://bugs.python.org/issue26574
(13) https://bugs.python.org/issue26249
(14) https://bugs.python.org/issue27056
(15) https://bugs.python.org/issue27574
(16) https://bugs.python.org/issue25596
(17) https://bugs.python.org/issue26032
(18) https://bugs.python.org/issue25638
(19) https://bugs.python.org/issue25873
(20) https://bugs.python.org/issue25869
(21) https://bugs.python.org/issue25971
File: python.info, Node: Build and C API Changes<2>, Next: Other Improvements, Prev: Optimizations<3>, Up: What’s New In Python 3 6
1.3.7 Build and C API Changes
-----------------------------
* Python now requires some C99 support in the toolchain to build.
Most notably, Python now uses standard integer types and macros in
place of custom macros like ‘PY_LONG_LONG’. For more information,
see PEP 7(1) and bpo-17884(2).
* Cross-compiling CPython with the Android NDK and the Android API
level set to 21 (Android 5.0 Lollipop) or greater runs
successfully. While Android is not yet a supported platform, the
Python test suite runs on the Android emulator with only about 16
tests failures. See the Android meta-issue bpo-26865(3).
* The ‘--enable-optimizations’ configure flag has been added.
Turning it on will activate expensive optimizations like PGO.
(Original patch by Alecsandru Patrascu of Intel in bpo-26359(4).)
* The *note GIL: 506. must now be held when allocator functions of
*note PYMEM_DOMAIN_OBJ: 507. (ex: *note PyObject_Malloc(): 508.)
and *note PYMEM_DOMAIN_MEM: 509. (ex: *note PyMem_Malloc(): 505.)
domains are called.
* New *note Py_FinalizeEx(): 5c9. API which indicates if flushing
buffered data failed. (Contributed by Martin Panter in
bpo-5319(5).)
* *note PyArg_ParseTupleAndKeywords(): 5ca. now supports *note
positional-only parameters: 2a7. Positional-only parameters are
defined by empty names. (Contributed by Serhiy Storchaka in
bpo-26282(6)).
* ‘PyTraceback_Print’ method now abbreviates long sequences of
repeated lines as ‘"[Previous line repeated {count} more times]"’.
(Contributed by Emanuel Barry in bpo-26823(7).)
* The new *note PyErr_SetImportErrorSubclass(): 5cb. function allows
for specifying a subclass of *note ImportError: 334. to raise.
(Contributed by Eric Snow in bpo-15767(8).)
* The new *note PyErr_ResourceWarning(): 5cc. function can be used to
generate a *note ResourceWarning: 4d0. providing the source of the
resource allocation. (Contributed by Victor Stinner in
bpo-26567(9).)
* The new *note PyOS_FSPath(): 5cd. function returns the file system
representation of a *note path-like object: 36a. (Contributed by
Brett Cannon in bpo-27186(10).)
* The *note PyUnicode_FSConverter(): 5ce. and *note
PyUnicode_FSDecoder(): 5cf. functions will now accept *note
path-like objects: 36a.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0007
(2) https://bugs.python.org/issue17884
(3) https://bugs.python.org/issue26865
(4) https://bugs.python.org/issue26359
(5) https://bugs.python.org/issue5319
(6) https://bugs.python.org/issue26282
(7) https://bugs.python.org/issue26823
(8) https://bugs.python.org/issue15767
(9) https://bugs.python.org/issue26567
(10) https://bugs.python.org/issue27186
File: python.info, Node: Other Improvements, Next: Deprecated<2>, Prev: Build and C API Changes<2>, Up: What’s New In Python 3 6
1.3.8 Other Improvements
------------------------
* When *note –version: 5d1. (short form: *note -V: 5d2.) is supplied
twice, Python prints *note sys.version: 5d3. for detailed
information.
$ ./python -VV
Python 3.6.0b4+ (3.6:223967b49e49+, Nov 21 2016, 20:55:04)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
File: python.info, Node: Deprecated<2>, Next: Removed, Prev: Other Improvements, Up: What’s New In Python 3 6
1.3.9 Deprecated
----------------
* Menu:
* New Keywords::
* Deprecated Python behavior::
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods<2>.
* Deprecated functions and types of the C API: Deprecated functions and types of the C API<2>.
* Deprecated Build Options::
File: python.info, Node: New Keywords, Next: Deprecated Python behavior, Up: Deprecated<2>
1.3.9.1 New Keywords
....................
‘async’ and ‘await’ are not recommended to be used as variable, class,
function or module names. Introduced by PEP 492(1) in Python 3.5, they
will become proper keywords in Python 3.7. Starting in Python 3.6, the
use of ‘async’ or ‘await’ as names will generate a *note
DeprecationWarning: 278.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0492
File: python.info, Node: Deprecated Python behavior, Next: Deprecated Python modules functions and methods<2>, Prev: New Keywords, Up: Deprecated<2>
1.3.9.2 Deprecated Python behavior
..................................
Raising the *note StopIteration: 486. exception inside a generator will
now generate a *note DeprecationWarning: 278, and will trigger a *note
RuntimeError: 2ba. in Python 3.7. See *note PEP 479; Change
StopIteration handling inside generators: 5d7. for details.
The *note __aiter__(): 487. method is now expected to return an
asynchronous iterator directly instead of returning an awaitable as
previously. Doing the former will trigger a *note DeprecationWarning:
278. Backward compatibility will be removed in Python 3.7.
(Contributed by Yury Selivanov in bpo-27243(1).)
A backslash-character pair that is not a valid escape sequence now
generates a *note DeprecationWarning: 278. Although this will
eventually become a *note SyntaxError: 458, that will not be for several
Python releases. (Contributed by Emanuel Barry in bpo-27364(2).)
When performing a relative import, falling back on ‘__name__’ and
‘__path__’ from the calling module when ‘__spec__’ or ‘__package__’ are
not defined now raises an *note ImportWarning: 5d8. (Contributed by
Rose Ames in bpo-25791(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue27243
(2) https://bugs.python.org/issue27364
(3) https://bugs.python.org/issue25791
File: python.info, Node: Deprecated Python modules functions and methods<2>, Next: Deprecated functions and types of the C API<2>, Prev: Deprecated Python behavior, Up: Deprecated<2>
1.3.9.3 Deprecated Python modules, functions and methods
........................................................
* Menu:
* asynchat::
* asyncore::
* dbm: dbm<3>.
* distutils: distutils<3>.
* grp::
* importlib: importlib<4>.
* os: os<4>.
* re: re<3>.
* ssl: ssl<5>.
* tkinter: tkinter<4>.
* venv: venv<3>.
File: python.info, Node: asynchat, Next: asyncore, Up: Deprecated Python modules functions and methods<2>
1.3.9.4 asynchat
................
The *note asynchat: 9. has been deprecated in favor of *note asyncio: a.
(Contributed by Mariatta in bpo-25002(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25002
File: python.info, Node: asyncore, Next: dbm<3>, Prev: asynchat, Up: Deprecated Python modules functions and methods<2>
1.3.9.5 asyncore
................
The *note asyncore: b. has been deprecated in favor of *note asyncio: a.
(Contributed by Mariatta in bpo-25002(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25002
File: python.info, Node: dbm<3>, Next: distutils<3>, Prev: asyncore, Up: Deprecated Python modules functions and methods<2>
1.3.9.6 dbm
...........
Unlike other *note dbm: 32. implementations, the *note dbm.dumb: 33.
module creates databases with the ‘'rw'’ mode and allows modifying the
database opened with the ‘'r'’ mode. This behavior is now deprecated
and will be removed in 3.8. (Contributed by Serhiy Storchaka in
bpo-21708(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21708
File: python.info, Node: distutils<3>, Next: grp, Prev: dbm<3>, Up: Deprecated Python modules functions and methods<2>
1.3.9.7 distutils
.................
The undocumented ‘extra_path’ argument to the ‘Distribution’ constructor
is now considered deprecated and will raise a warning if set. Support
for this parameter will be removed in a future Python release. See
bpo-27919(1) for details.
---------- Footnotes ----------
(1) https://bugs.python.org/issue27919
File: python.info, Node: grp, Next: importlib<4>, Prev: distutils<3>, Up: Deprecated Python modules functions and methods<2>
1.3.9.8 grp
...........
The support of non-integer arguments in *note getgrgid(): 5df. has been
deprecated. (Contributed by Serhiy Storchaka in bpo-26129(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26129
File: python.info, Node: importlib<4>, Next: os<4>, Prev: grp, Up: Deprecated Python modules functions and methods<2>
1.3.9.9 importlib
.................
The *note importlib.machinery.SourceFileLoader.load_module(): 5e1. and
*note importlib.machinery.SourcelessFileLoader.load_module(): 5e2.
methods are now deprecated. They were the only remaining
implementations of *note importlib.abc.Loader.load_module(): 5e3. in
*note importlib: 9b. that had not been deprecated in previous versions
of Python in favour of *note importlib.abc.Loader.exec_module(): 5e4.
The *note importlib.machinery.WindowsRegistryFinder: 5e5. class is now
deprecated. As of 3.6.0, it is still added to *note sys.meta_path: 5e6.
by default (on Windows), but this may change in future releases.
File: python.info, Node: os<4>, Next: re<3>, Prev: importlib<4>, Up: Deprecated Python modules functions and methods<2>
1.3.9.10 os
...........
Undocumented support of general *note bytes-like objects: 5e8. as paths
in *note os: c4. functions, *note compile(): 1b4. and similar functions
is now deprecated. (Contributed by Serhiy Storchaka in bpo-25791(1) and
bpo-26754(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25791
(2) https://bugs.python.org/issue26754
File: python.info, Node: re<3>, Next: ssl<5>, Prev: os<4>, Up: Deprecated Python modules functions and methods<2>
1.3.9.11 re
...........
Support for inline flags ‘(?letters)’ in the middle of the regular
expression has been deprecated and will be removed in a future Python
version. Flags at the start of a regular expression are still allowed.
(Contributed by Serhiy Storchaka in bpo-22493(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22493
File: python.info, Node: ssl<5>, Next: tkinter<4>, Prev: re<3>, Up: Deprecated Python modules functions and methods<2>
1.3.9.12 ssl
............
OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported.
In the future the *note ssl: f3. module will require at least OpenSSL
1.0.2 or 1.1.0.
SSL-related arguments like ‘certfile’, ‘keyfile’ and ‘check_hostname’ in
*note ftplib: 84, *note http.client: 94, *note imaplib: 98, *note
poplib: d0, and *note smtplib: ed. have been deprecated in favor of
‘context’. (Contributed by Christian Heimes in bpo-28022(1).)
A couple of protocols and functions of the *note ssl: f3. module are now
deprecated. Some features will no longer be available in future
versions of OpenSSL. Other features are deprecated in favor of a
different API. (Contributed by Christian Heimes in bpo-28022(2) and
bpo-26470(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue28022
(2) https://bugs.python.org/issue28022
(3) https://bugs.python.org/issue26470
File: python.info, Node: tkinter<4>, Next: venv<3>, Prev: ssl<5>, Up: Deprecated Python modules functions and methods<2>
1.3.9.13 tkinter
................
The *note tkinter.tix: 10e. module is now deprecated. *note tkinter:
10c. users should use *note tkinter.ttk: 10f. instead.
File: python.info, Node: venv<3>, Prev: tkinter<4>, Up: Deprecated Python modules functions and methods<2>
1.3.9.14 venv
.............
The ‘pyvenv’ script has been deprecated in favour of ‘python3 -m venv’.
This prevents confusion as to what Python interpreter ‘pyvenv’ is
connected to and thus what Python interpreter will be used by the
virtual environment. (Contributed by Brett Cannon in bpo-25154(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue25154
File: python.info, Node: Deprecated functions and types of the C API<2>, Next: Deprecated Build Options, Prev: Deprecated Python modules functions and methods<2>, Up: Deprecated<2>
1.3.9.15 Deprecated functions and types of the C API
....................................................
Undocumented functions ‘PyUnicode_AsEncodedObject()’,
‘PyUnicode_AsDecodedObject()’, ‘PyUnicode_AsEncodedUnicode()’ and
‘PyUnicode_AsDecodedUnicode()’ are deprecated now. Use the *note
generic codec based API: 5ee. instead.
File: python.info, Node: Deprecated Build Options, Prev: Deprecated functions and types of the C API<2>, Up: Deprecated<2>
1.3.9.16 Deprecated Build Options
.................................
The ‘--with-system-ffi’ configure flag is now on by default on non-macOS
UNIX platforms. It may be disabled by using ‘--without-system-ffi’, but
using the flag is deprecated and will not be accepted in Python 3.7.
macOS is unaffected by this change. Note that many OS distributors
already use the ‘--with-system-ffi’ flag when building their system
Python.
File: python.info, Node: Removed, Next: Porting to Python 3 6, Prev: Deprecated<2>, Up: What’s New In Python 3 6
1.3.10 Removed
--------------
* Menu:
* API and Feature Removals: API and Feature Removals<3>.
File: python.info, Node: API and Feature Removals<3>, Up: Removed
1.3.10.1 API and Feature Removals
.................................
* Unknown escapes consisting of ‘'\'’ and an ASCII letter in regular
expressions will now cause an error. In replacement templates for
*note re.sub(): 47b. they are still allowed, but deprecated. The
*note re.LOCALE: 3c9. flag can now only be used with binary
patterns.
* ‘inspect.getmoduleinfo()’ was removed (was deprecated since CPython
3.3). *note inspect.getmodulename(): 5f2. should be used for
obtaining the module name for a given path. (Contributed by Yury
Selivanov in bpo-13248(1).)
* ‘traceback.Ignore’ class and ‘traceback.usage’,
‘traceback.modname’, ‘traceback.fullmodname’,
‘traceback.find_lines_from_code’, ‘traceback.find_lines’,
‘traceback.find_strings’, ‘traceback.find_executable_lines’ methods
were removed from the *note traceback: 113. module. They were
undocumented methods deprecated since Python 3.2 and equivalent
functionality is available from private methods.
* The ‘tk_menuBar()’ and ‘tk_bindForTraversal()’ dummy methods in
*note tkinter: 10c. widget classes were removed (corresponding Tk
commands were obsolete since Tk 4.0).
* The *note open(): 5bc. method of the *note zipfile.ZipFile: 41f.
class no longer supports the ‘'U'’ mode (was deprecated since
Python 3.4). Use *note io.TextIOWrapper: 5f3. for reading
compressed text files in *note universal newlines: 5f4. mode.
* The undocumented ‘IN’, ‘CDROM’, ‘DLFCN’, ‘TYPES’, ‘CDIO’, and
‘STROPTS’ modules have been removed. They had been available in
the platform specific ‘Lib/plat-*/’ directories, but were
chronically out of date, inconsistently available across platforms,
and unmaintained. The script that created these modules is still
available in the source distribution at Tools/scripts/h2py.py(2).
* The deprecated ‘asynchat.fifo’ class has been removed.
---------- Footnotes ----------
(1) https://bugs.python.org/issue13248
(2) https://github.com/python/cpython/tree/3.8/Tools/scripts/h2py.py
File: python.info, Node: Porting to Python 3 6, Next: Notable changes in Python 3 6 2, Prev: Removed, Up: What’s New In Python 3 6
1.3.11 Porting to Python 3.6
----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code.
* Menu:
* Changes in ‘python’ Command Behavior::
* Changes in the Python API: Changes in the Python API<3>.
* Changes in the C API: Changes in the C API<3>.
* CPython bytecode changes: CPython bytecode changes<3>.
File: python.info, Node: Changes in ‘python’ Command Behavior, Next: Changes in the Python API<3>, Up: Porting to Python 3 6
1.3.11.1 Changes in ‘python’ Command Behavior
.............................................
* The output of a special Python build with defined ‘COUNT_ALLOCS’,
‘SHOW_ALLOC_COUNT’ or ‘SHOW_TRACK_COUNT’ macros is now off by
default. It can be re-enabled using the ‘-X showalloccount’
option. It now outputs to ‘stderr’ instead of ‘stdout’.
(Contributed by Serhiy Storchaka in bpo-23034(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23034
File: python.info, Node: Changes in the Python API<3>, Next: Changes in the C API<3>, Prev: Changes in ‘python’ Command Behavior, Up: Porting to Python 3 6
1.3.11.2 Changes in the Python API
..................................
* *note open(): 4f0. will no longer allow combining the ‘'U'’ mode
flag with ‘'+'’. (Contributed by Jeff Balogh and John O’Connor in
bpo-2091(1).)
* *note sqlite3: f2. no longer implicitly commits an open transaction
before DDL statements.
* On Linux, *note os.urandom(): 4d1. now blocks until the system
urandom entropy pool is initialized to increase the security.
* When *note importlib.abc.Loader.exec_module(): 5e4. is defined,
*note importlib.abc.Loader.create_module(): 554. must also be
defined.
* *note PyErr_SetImportError(): 5f8. now sets *note TypeError: 192.
when its `msg' argument is not set. Previously only ‘NULL’ was
returned.
* The format of the ‘co_lnotab’ attribute of code objects changed to
support a negative line number delta. By default, Python does not
emit bytecode with a negative line number delta. Functions using
‘frame.f_lineno’, ‘PyFrame_GetLineNumber()’ or ‘PyCode_Addr2Line()’
are not affected. Functions directly decoding ‘co_lnotab’ should
be updated to use a signed 8-bit integer type for the line number
delta, but this is only required to support applications using a
negative line number delta. See ‘Objects/lnotab_notes.txt’ for the
‘co_lnotab’ format and how to decode it, and see the PEP 511(2) for
the rationale.
* The functions in the *note compileall: 21. module now return
booleans instead of ‘1’ or ‘0’ to represent success or failure,
respectively. Thanks to booleans being a subclass of integers,
this should only be an issue if you were doing identity checks for
‘1’ or ‘0’. See bpo-25768(3).
* Reading the ‘port’ attribute of *note urllib.parse.urlsplit(): 5f9.
and *note urlparse(): 5fa. results now raises *note ValueError:
1fb. for out-of-range values, rather than returning *note None:
157. See bpo-20059(4).
* The *note imp: 9a. module now raises a *note DeprecationWarning:
278. instead of *note PendingDeprecationWarning: 279.
* The following modules have had missing APIs added to their
‘__all__’ attributes to match the documented APIs: *note calendar:
15, *note cgi: 16, *note csv: 2a, *note ElementTree: 137, *note
enum: 7b, *note fileinput: 80, *note ftplib: 84, *note logging: aa,
*note mailbox: ae, *note mimetypes: b2, *note optparse: c3, *note
plistlib: cf, *note smtpd: ec, *note subprocess: f9, *note tarfile:
101, *note threading: 109. and *note wave: 127. This means they
will export new symbols when ‘import *’ is used. (Contributed by
Joel Taddei and Jacek Kołodziej in bpo-23883(5).)
* When performing a relative import, if ‘__package__’ does not
compare equal to ‘__spec__.parent’ then *note ImportWarning: 5d8.
is raised. (Contributed by Brett Cannon in bpo-25791(6).)
* When a relative import is performed and no parent package is known,
then *note ImportError: 334. will be raised. Previously, *note
SystemError: 5fb. could be raised. (Contributed by Brett Cannon in
bpo-18018(7).)
* Servers based on the *note socketserver: f0. module, including
those defined in *note http.server: 97, *note xmlrpc.server: 140.
and *note wsgiref.simple_server: 12f, now only catch exceptions
derived from *note Exception: 1a9. Therefore if a request handler
raises an exception like *note SystemExit: 5fc. or *note
KeyboardInterrupt: 197, *note handle_error(): 5fd. is no longer
called, and the exception will stop a single-threaded server.
(Contributed by Martin Panter in bpo-23430(8).)
* *note spwd.getspnam(): 5fe. now raises a *note PermissionError:
5ff. instead of *note KeyError: 2c7. if the user doesn’t have
privileges.
* The *note socket.socket.close(): 600. method now raises an
exception if an error (e.g. ‘EBADF’) was reported by the
underlying system call. (Contributed by Martin Panter in
bpo-26685(9).)
* The `decode_data' argument for the *note smtpd.SMTPChannel: 601.
and *note smtpd.SMTPServer: 602. constructors is now ‘False’ by
default. This means that the argument passed to *note
process_message(): 603. is now a bytes object by default, and
‘process_message()’ will be passed keyword arguments. Code that
has already been updated in accordance with the deprecation warning
generated by 3.5 will not be affected.
* All optional arguments of the *note dump(): 604, *note dumps():
605, *note load(): 55f. and *note loads(): 560. functions and *note
JSONEncoder: 606. and *note JSONDecoder: 607. class constructors in
the *note json: a4. module are now *note keyword-only: 2ac.
(Contributed by Serhiy Storchaka in bpo-18726(10).)
* Subclasses of *note type: 608. which don’t override ‘type.__new__’
may no longer use the one-argument form to get the type of an
object.
* As part of PEP 487(11), the handling of keyword arguments passed to
*note type: 608. (other than the metaclass hint, ‘metaclass’) is
now consistently delegated to *note object.__init_subclass__():
4e3. This means that ‘type.__new__()’ and ‘type.__init__()’ both
now accept arbitrary keyword arguments, but *note
object.__init_subclass__(): 4e3. (which is called from
‘type.__new__()’) will reject them by default. Custom metaclasses
accepting additional keyword arguments will need to adjust their
calls to ‘type.__new__()’ (whether direct or via *note super: 4e2.)
accordingly.
* In ‘distutils.command.sdist.sdist’, the ‘default_format’ attribute
has been removed and is no longer honored. Instead, the gzipped
tarfile format is the default on all platforms and no
platform-specific selection is made. In environments where
distributions are built on Windows and zip distributions are
required, configure the project with a ‘setup.cfg’ file containing
the following:
[sdist]
formats=zip
This behavior has also been backported to earlier Python versions
by Setuptools 26.0.0.
* In the *note urllib.request: 120. module and the *note
http.client.HTTPConnection.request(): 54f. method, if no
Content-Length header field has been specified and the request body
is a file object, it is now sent with HTTP 1.1 chunked encoding.
If a file object has to be sent to a HTTP 1.0 server, the
Content-Length value now has to be specified by the caller.
(Contributed by Demian Brecht and Rolf Krahl with tweaks from
Martin Panter in bpo-12319(12).)
* The *note DictReader: 1bd. now returns rows of type *note
OrderedDict: 1b9. (Contributed by Steve Holden in bpo-27842(13).)
* The *note crypt.METHOD_CRYPT: 609. will no longer be added to
‘crypt.methods’ if unsupported by the platform. (Contributed by
Victor Stinner in bpo-25287(14).)
* The `verbose' and `rename' arguments for *note namedtuple(): 1b7.
are now keyword-only. (Contributed by Raymond Hettinger in
bpo-25628(15).)
* On Linux, *note ctypes.util.find_library(): 60a. now looks in
‘LD_LIBRARY_PATH’ for shared libraries. (Contributed by Vinay
Sajip in bpo-9998(16).)
* The *note imaplib.IMAP4: 60b. class now handles flags containing
the ‘']'’ character in messages sent from the server to improve
real-world compatibility. (Contributed by Lita Cho in
bpo-21815(17).)
* The ‘mmap.write()’ function now returns the number of bytes written
like other write methods. (Contributed by Jakub Stasiak in
bpo-26335(18).)
* The *note pkgutil.iter_modules(): 60c. and *note
pkgutil.walk_packages(): 48b. functions now return *note
ModuleInfo: 60d. named tuples. (Contributed by Ramchandra Apte in
bpo-17211(19).)
* *note re.sub(): 47b. now raises an error for invalid numerical
group references in replacement templates even if the pattern is
not found in the string. The error message for invalid group
references now includes the group index and the position of the
reference. (Contributed by SilentGhost, Serhiy Storchaka in
bpo-25953(20).)
* *note zipfile.ZipFile: 41f. will now raise *note
NotImplementedError: 60e. for unrecognized compression values.
Previously a plain *note RuntimeError: 2ba. was raised.
Additionally, calling *note ZipFile: 41f. methods on a closed
ZipFile or calling the *note write(): 60f. method on a ZipFile
created with mode ‘'r'’ will raise a *note ValueError: 1fb.
Previously, a *note RuntimeError: 2ba. was raised in those
scenarios.
* when custom metaclasses are combined with zero-argument *note
super(): 4e2. or direct references from methods to the implicit
‘__class__’ closure variable, the implicit ‘__classcell__’
namespace entry must now be passed up to ‘type.__new__’ for
initialisation. Failing to do so will result in a *note
DeprecationWarning: 278. in Python 3.6 and a *note RuntimeError:
2ba. in Python 3.8.
* With the introduction of *note ModuleNotFoundError: 39a, import
system consumers may start expecting import system replacements to
raise that more specific exception when appropriate, rather than
the less-specific *note ImportError: 334. To provide future
compatibility with such consumers, implementors of alternative
import systems that completely replace *note __import__(): 610.
will need to update their implementations to raise the new subclass
when a module can’t be found at all. Implementors of compliant
plugins to the default import system shouldn’t need to make any
changes, as the default import system will raise the new subclass
when appropriate.
---------- Footnotes ----------
(1) https://bugs.python.org/issue2091
(2) https://www.python.org/dev/peps/pep-0511
(3) https://bugs.python.org/issue25768
(4) https://bugs.python.org/issue20059
(5) https://bugs.python.org/issue23883
(6) https://bugs.python.org/issue25791
(7) https://bugs.python.org/issue18018
(8) https://bugs.python.org/issue23430
(9) https://bugs.python.org/issue26685
(10) https://bugs.python.org/issue18726
(11) https://www.python.org/dev/peps/pep-0487
(12) https://bugs.python.org/issue12319
(13) https://bugs.python.org/issue27842
(14) https://bugs.python.org/issue25287
(15) https://bugs.python.org/issue25628
(16) https://bugs.python.org/issue9998
(17) https://bugs.python.org/issue21815
(18) https://bugs.python.org/issue26335
(19) https://bugs.python.org/issue17211
(20) https://bugs.python.org/issue25953
File: python.info, Node: Changes in the C API<3>, Next: CPython bytecode changes<3>, Prev: Changes in the Python API<3>, Up: Porting to Python 3 6
1.3.11.3 Changes in the C API
.............................
* The *note PyMem_Malloc(): 505. allocator family now uses the *note
pymalloc allocator: 5c1. rather than the system ‘malloc()’.
Applications calling *note PyMem_Malloc(): 505. without holding the
GIL can now crash. Set the *note PYTHONMALLOC: 503. environment
variable to ‘debug’ to validate the usage of memory allocators in
your application. See bpo-26249(1).
* *note Py_Exit(): 612. (and the main interpreter) now override the
exit status with 120 if flushing buffered data failed. See
bpo-5319(2).
---------- Footnotes ----------
(1) https://bugs.python.org/issue26249
(2) https://bugs.python.org/issue5319
File: python.info, Node: CPython bytecode changes<3>, Prev: Changes in the C API<3>, Up: Porting to Python 3 6
1.3.11.4 CPython bytecode changes
.................................
There have been several major changes to the *note bytecode: 614. in
Python 3.6.
* The Python interpreter now uses a 16-bit wordcode instead of
bytecode. (Contributed by Demur Rumed with input and reviews from
Serhiy Storchaka and Victor Stinner in bpo-26647(1) and
bpo-28050(2).)
* The new *note FORMAT_VALUE: 615. and *note BUILD_STRING: 616.
opcodes as part of the *note formatted string literal: 4c1.
implementation. (Contributed by Eric Smith in bpo-25483(3) and
Serhiy Storchaka in bpo-27078(4).)
* The new *note BUILD_CONST_KEY_MAP: 617. opcode to optimize the
creation of dictionaries with constant keys. (Contributed by
Serhiy Storchaka in bpo-27140(5).)
* The function call opcodes have been heavily reworked for better
performance and simpler implementation. The *note MAKE_FUNCTION:
618, *note CALL_FUNCTION: 619, *note CALL_FUNCTION_KW: 61a. and
*note BUILD_MAP_UNPACK_WITH_CALL: 61b. opcodes have been modified,
the new *note CALL_FUNCTION_EX: 61c. and *note
BUILD_TUPLE_UNPACK_WITH_CALL: 61d. have been added, and
‘CALL_FUNCTION_VAR’, ‘CALL_FUNCTION_VAR_KW’ and ‘MAKE_CLOSURE’
opcodes have been removed. (Contributed by Demur Rumed in
bpo-27095(6), and Serhiy Storchaka in bpo-27213(7), bpo-28257(8).)
* The new *note SETUP_ANNOTATIONS: 61e. and ‘STORE_ANNOTATION’
opcodes have been added to support the new *note variable
annotation: 61f. syntax. (Contributed by Ivan Levkivskyi in
bpo-27985(9).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue26647
(2) https://bugs.python.org/issue28050
(3) https://bugs.python.org/issue25483
(4) https://bugs.python.org/issue27078
(5) https://bugs.python.org/issue27140
(6) https://bugs.python.org/issue27095
(7) https://bugs.python.org/issue27213
(8) https://bugs.python.org/issue28257
(9) https://bugs.python.org/issue27985
File: python.info, Node: Notable changes in Python 3 6 2, Next: Notable changes in Python 3 6 4, Prev: Porting to Python 3 6, Up: What’s New In Python 3 6
1.3.12 Notable changes in Python 3.6.2
--------------------------------------
* Menu:
* New make regen-all build target::
* Removal of make touch build target::
File: python.info, Node: New make regen-all build target, Next: Removal of make touch build target, Up: Notable changes in Python 3 6 2
1.3.12.1 New ‘make regen-all’ build target
..........................................
To simplify cross-compilation, and to ensure that CPython can reliably
be compiled without requiring an existing version of Python to already
be available, the autotools-based build system no longer attempts to
implicitly recompile generated files based on file modification times.
Instead, a new ‘make regen-all’ command has been added to force
regeneration of these files when desired (e.g. after an initial version
of Python has already been built based on the pregenerated versions).
More selective regeneration targets are also defined - see
Makefile.pre.in(1) for details.
(Contributed by Victor Stinner in bpo-23404(2).)
New in version 3.6.2.
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Makefile.pre.in
(2) https://bugs.python.org/issue23404
File: python.info, Node: Removal of make touch build target, Prev: New make regen-all build target, Up: Notable changes in Python 3 6 2
1.3.12.2 Removal of ‘make touch’ build target
.............................................
The ‘make touch’ build target previously used to request implicit
regeneration of generated files by updating their modification times has
been removed.
It has been replaced by the new ‘make regen-all’ target.
(Contributed by Victor Stinner in bpo-23404(1).)
Changed in version 3.6.2.
---------- Footnotes ----------
(1) https://bugs.python.org/issue23404
File: python.info, Node: Notable changes in Python 3 6 4, Next: Notable changes in Python 3 6 5, Prev: Notable changes in Python 3 6 2, Up: What’s New In Python 3 6
1.3.13 Notable changes in Python 3.6.4
--------------------------------------
The ‘PyExc_RecursionErrorInst’ singleton that was part of the public API
has been removed as its members being never cleared may cause a segfault
during finalization of the interpreter. (Contributed by Xavier de Gaye
in bpo-22898(1) and bpo-30697(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22898
(2) https://bugs.python.org/issue30697
File: python.info, Node: Notable changes in Python 3 6 5, Next: Notable changes in Python 3 6 7, Prev: Notable changes in Python 3 6 4, Up: What’s New In Python 3 6
1.3.14 Notable changes in Python 3.6.5
--------------------------------------
The *note locale.localeconv(): 48a. function now sets temporarily the
‘LC_CTYPE’ locale to the ‘LC_NUMERIC’ locale in some cases.
(Contributed by Victor Stinner in bpo-31900(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue31900
File: python.info, Node: Notable changes in Python 3 6 7, Next: Notable changes in Python 3 6 10, Prev: Notable changes in Python 3 6 5, Up: What’s New In Python 3 6
1.3.15 Notable changes in Python 3.6.7
--------------------------------------
In 3.6.7 the *note tokenize: 111. module now implicitly emits a
‘NEWLINE’ token when provided with input that does not have a trailing
new line. This behavior now matches what the C tokenizer does
internally. (Contributed by Ammar Askar in bpo-33899(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue33899
File: python.info, Node: Notable changes in Python 3 6 10, Next: Notable changes in Python 3 6 13, Prev: Notable changes in Python 3 6 7, Up: What’s New In Python 3 6
1.3.16 Notable changes in Python 3.6.10
---------------------------------------
Due to significant security concerns, the `reuse_address' parameter of
*note asyncio.loop.create_datagram_endpoint(): 2e7. is no longer
supported. This is because of the behavior of the socket option
‘SO_REUSEADDR’ in UDP. For more details, see the documentation for
‘loop.create_datagram_endpoint()’. (Contributed by Kyle Stanley,
Antoine Pitrou, and Yury Selivanov in bpo-37228(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue37228
File: python.info, Node: Notable changes in Python 3 6 13, Prev: Notable changes in Python 3 6 10, Up: What’s New In Python 3 6
1.3.17 Notable changes in Python 3.6.13
---------------------------------------
Earlier Python versions allowed using both ‘;’ and ‘&’ as query
parameter separators in *note urllib.parse.parse_qs(): 2eb. and *note
urllib.parse.parse_qsl(): 2ec. Due to security concerns, and to conform
with newer W3C recommendations, this has been changed to allow only a
single separator key, with ‘&’ as the default. This change also affects
*note cgi.parse(): 2ed. and *note cgi.parse_multipart(): 2ee. as they
use the affected functions internally. For more details, please see
their respective documentation. (Contributed by Adam Goldschmidt,
Senthil Kumaran and Ken Jin in bpo-42967(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue42967
File: python.info, Node: What’s New In Python 3 5, Next: What’s New In Python 3 4, Prev: What’s New In Python 3 6, Up: What’s New in Python
1.4 What’s New In Python 3.5
============================
Editors: Elvis Pranskevichus <>, Yury Selivanov
<>
This article explains the new features in Python 3.5, compared to 3.4.
Python 3.5 was released on September 13, 2015. See the changelog(1)
for a full list of changes.
See also
........
PEP 478(2) - Python 3.5 Release Schedule
* Menu:
* Summary – Release highlights: Summary – Release highlights<3>.
* New Features: New Features<4>.
* Other Language Changes: Other Language Changes<4>.
* New Modules: New Modules<4>.
* Improved Modules: Improved Modules<4>.
* Other module-level changes::
* Optimizations: Optimizations<4>.
* Build and C API Changes: Build and C API Changes<3>.
* Deprecated: Deprecated<3>.
* Removed: Removed<2>.
* Porting to Python 3.5: Porting to Python 3 5.
* Notable changes in Python 3.5.4: Notable changes in Python 3 5 4.
---------- Footnotes ----------
(1) https://docs.python.org/3.5/whatsnew/changelog.html
(2) https://www.python.org/dev/peps/pep-0478
File: python.info, Node: Summary – Release highlights<3>, Next: New Features<4>, Up: What’s New In Python 3 5
1.4.1 Summary – Release highlights
----------------------------------
New syntax features:
* *note PEP 492: 62b, coroutines with async and await syntax.
* *note PEP 465: 62c, a new matrix multiplication operator: ‘a @ b’.
* *note PEP 448: 62d, additional unpacking generalizations.
New library modules:
* *note typing: 119.: *note PEP 484 – Type Hints: 62e.
* *note zipapp: 141.: *note PEP 441 Improving Python ZIP Application
Support: 62f.
New built-in features:
* ‘bytes % args’, ‘bytearray % args’: *note PEP 461: 630. – Adding
‘%’ formatting to bytes and bytearray.
* New *note bytes.hex(): 631, *note bytearray.hex(): 632. and *note
memoryview.hex(): 633. methods. (Contributed by Arnon Yaari in
bpo-9951(1).)
* *note memoryview: 25c. now supports tuple indexing (including
multi-dimensional). (Contributed by Antoine Pitrou in
bpo-23632(2).)
* Generators have a new ‘gi_yieldfrom’ attribute, which returns the
object being iterated by ‘yield from’ expressions. (Contributed by
Benno Leslie and Yury Selivanov in bpo-24450(3).)
* A new *note RecursionError: 634. exception is now raised when
maximum recursion depth is reached. (Contributed by Georg Brandl
in bpo-19235(4).)
CPython implementation improvements:
* When the ‘LC_TYPE’ locale is the POSIX locale (‘C’ locale), *note
sys.stdin: 30d. and *note sys.stdout: 30e. now use the
‘surrogateescape’ error handler, instead of the ‘strict’ error
handler. (Contributed by Victor Stinner in bpo-19977(5).)
* ‘.pyo’ files are no longer used and have been replaced by a more
flexible scheme that includes the optimization level explicitly in
‘.pyc’ name. (See *note PEP 488 overview: 635.)
* Builtin and extension modules are now initialized in a multi-phase
process, which is similar to how Python modules are loaded. (See
*note PEP 489 overview: 636.)
Significant improvements in the standard library:
* *note collections.OrderedDict: 1b9. is now *note implemented in C:
637, which makes it 4 to 100 times faster.
* The *note ssl: f3. module gained *note support for Memory BIO: 638,
which decouples SSL protocol handling from network IO.
* The new *note os.scandir(): 25f. function provides a *note better
and significantly faster way: 639. of directory traversal.
* *note functools.lru_cache(): 1c7. has been mostly *note
reimplemented in C: 63a, yielding much better performance.
* The new *note subprocess.run(): 3ed. function provides a *note
streamlined way to run subprocesses: 63b.
* The *note traceback: 113. module has been significantly *note
enhanced: 63c. for improved performance and developer convenience.
Security improvements:
* SSLv3 is now disabled throughout the standard library. It can
still be enabled by instantiating a *note ssl.SSLContext: 3e4.
manually. (See bpo-22638(6) for more details; this change was
backported to CPython 3.4 and 2.7.)
* HTTP cookie parsing is now stricter, in order to protect against
potential injection attacks. (Contributed by Antoine Pitrou in
bpo-22796(7).)
Windows improvements:
* A new installer for Windows has replaced the old MSI. See *note
Using Python on Windows: 2d9. for more information.
* Windows builds now use Microsoft Visual C++ 14.0, and extension
modules should use the same.
Please read on for a comprehensive list of user-facing changes,
including many other smaller improvements, CPython optimizations,
deprecations, and potential porting issues.
---------- Footnotes ----------
(1) https://bugs.python.org/issue9951
(2) https://bugs.python.org/issue23632
(3) https://bugs.python.org/issue24450
(4) https://bugs.python.org/issue19235
(5) https://bugs.python.org/issue19977
(6) https://bugs.python.org/issue22638
(7) https://bugs.python.org/issue22796
File: python.info, Node: New Features<4>, Next: Other Language Changes<4>, Prev: Summary – Release highlights<3>, Up: What’s New In Python 3 5
1.4.2 New Features
------------------
* Menu:
* PEP 492 - Coroutines with async and await syntax::
* PEP 465 - A dedicated infix operator for matrix multiplication::
* PEP 448 - Additional Unpacking Generalizations::
* PEP 461 - percent formatting support for bytes and bytearray::
* PEP 484 - Type Hints::
* PEP 471 - os.scandir() function – a better and faster directory iterator: PEP 471 - os scandir function – a better and faster directory iterator.
* PEP 475; Retry system calls failing with EINTR: PEP 475 Retry system calls failing with EINTR.
* PEP 479; Change StopIteration handling inside generators: PEP 479 Change StopIteration handling inside generators.
* PEP 485; A function for testing approximate equality: PEP 485 A function for testing approximate equality.
* PEP 486; Make the Python Launcher aware of virtual environments: PEP 486 Make the Python Launcher aware of virtual environments.
* PEP 488; Elimination of PYO files: PEP 488 Elimination of PYO files.
* PEP 489; Multi-phase extension module initialization: PEP 489 Multi-phase extension module initialization.
File: python.info, Node: PEP 492 - Coroutines with async and await syntax, Next: PEP 465 - A dedicated infix operator for matrix multiplication, Up: New Features<4>
1.4.2.1 PEP 492 - Coroutines with async and await syntax
........................................................
PEP 492(1) greatly improves support for asynchronous programming in
Python by adding *note awaitable objects: 519, *note coroutine
functions: 63f, *note asynchronous iteration: 640, and *note
asynchronous context managers: 641.
Coroutine functions are declared using the new *note async def: 284.
syntax:
>>> async def coro():
... return 'spam'
Inside a coroutine function, the new *note await: 1a3. expression can be
used to suspend coroutine execution until the result is available. Any
object can be `awaited', as long as it implements the *note awaitable:
519. protocol by defining the *note __await__(): 642. method.
PEP 492 also adds *note async for: 2e3. statement for convenient
iteration over asynchronous iterables.
An example of a rudimentary HTTP client written using the new syntax:
import asyncio
async def http_get(domain):
reader, writer = await asyncio.open_connection(domain, 80)
writer.write(b'\r\n'.join([
b'GET / HTTP/1.1',
b'Host: %b' % domain.encode('latin-1'),
b'Connection: close',
b'', b''
]))
async for line in reader:
print('>>>', line)
writer.close()
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(http_get('example.com'))
finally:
loop.close()
Similarly to asynchronous iteration, there is a new syntax for
asynchronous context managers. The following script:
import asyncio
async def coro(name, lock):
print('coro {}: waiting for lock'.format(name))
async with lock:
print('coro {}: holding the lock'.format(name))
await asyncio.sleep(1)
print('coro {}: releasing the lock'.format(name))
loop = asyncio.get_event_loop()
lock = asyncio.Lock()
coros = asyncio.gather(coro(1, lock), coro(2, lock))
try:
loop.run_until_complete(coros)
finally:
loop.close()
will output:
coro 2: waiting for lock
coro 2: holding the lock
coro 1: waiting for lock
coro 2: releasing the lock
coro 1: holding the lock
coro 1: releasing the lock
Note that both *note async for: 2e3. and *note async with: 643. can only
be used inside a coroutine function declared with *note async def: 284.
Coroutine functions are intended to be run inside a compatible event
loop, such as the *note asyncio loop: 644.
Note:
Changed in version 3.5.2: Starting with CPython 3.5.2, ‘__aiter__’
can directly return *note asynchronous iterators: 645. Returning
an *note awaitable: 519. object will result in a *note
PendingDeprecationWarning: 279.
See more details in the *note Asynchronous Iterators: 646.
documentation section.
See also
........
PEP 492(2) – Coroutines with async and await syntax
PEP written and implemented by Yury Selivanov.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0492
(2) https://www.python.org/dev/peps/pep-0492
File: python.info, Node: PEP 465 - A dedicated infix operator for matrix multiplication, Next: PEP 448 - Additional Unpacking Generalizations, Prev: PEP 492 - Coroutines with async and await syntax, Up: New Features<4>
1.4.2.2 PEP 465 - A dedicated infix operator for matrix multiplication
......................................................................
PEP 465(1) adds the ‘@’ infix operator for matrix multiplication.
Currently, no builtin Python types implement the new operator, however,
it can be implemented by defining *note __matmul__(): 648, *note
__rmatmul__(): 649, and *note __imatmul__(): 64a. for regular,
reflected, and in-place matrix multiplication. The semantics of these
methods is similar to that of methods defining other infix arithmetic
operators.
Matrix multiplication is a notably common operation in many fields of
mathematics, science, engineering, and the addition of ‘@’ allows
writing cleaner code:
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
instead of:
S = dot((dot(H, beta) - r).T,
dot(inv(dot(dot(H, V), H.T)), dot(H, beta) - r))
NumPy 1.10 has support for the new operator:
>>> import numpy
>>> x = numpy.ones(3)
>>> x
array([ 1., 1., 1.])
>>> m = numpy.eye(3)
>>> m
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> x @ m
array([ 1., 1., 1.])
See also
........
PEP 465(2) – A dedicated infix operator for matrix multiplication
PEP written by Nathaniel J. Smith; implemented by Benjamin
Peterson.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0465
(2) https://www.python.org/dev/peps/pep-0465
File: python.info, Node: PEP 448 - Additional Unpacking Generalizations, Next: PEP 461 - percent formatting support for bytes and bytearray, Prev: PEP 465 - A dedicated infix operator for matrix multiplication, Up: New Features<4>
1.4.2.3 PEP 448 - Additional Unpacking Generalizations
......................................................
PEP 448(1) extends the allowed uses of the ‘*’ iterable unpacking
operator and ‘**’ dictionary unpacking operator. It is now possible to
use an arbitrary number of unpackings in *note function calls: 64c.:
>>> print(*[1], *[2], 3, *[4, 5])
1 2 3 4 5
>>> def fn(a, b, c, d):
... print(a, b, c, d)
...
>>> fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})
1 2 3 4
Similarly, tuple, list, set, and dictionary displays allow multiple
unpackings (see *note Expression lists: 64d. and *note Dictionary
displays: 64e.):
>>> *range(4), 4
(0, 1, 2, 3, 4)
>>> [*range(4), 4]
[0, 1, 2, 3, 4]
>>> {*range(4), 4, *(5, 6, 7)}
{0, 1, 2, 3, 4, 5, 6, 7}
>>> {'x': 1, **{'y': 2}}
{'x': 1, 'y': 2}
See also
........
PEP 448(2) – Additional Unpacking Generalizations
PEP written by Joshua Landau; implemented by Neil Girdhar, Thomas
Wouters, and Joshua Landau.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0448
(2) https://www.python.org/dev/peps/pep-0448
File: python.info, Node: PEP 461 - percent formatting support for bytes and bytearray, Next: PEP 484 - Type Hints, Prev: PEP 448 - Additional Unpacking Generalizations, Up: New Features<4>
1.4.2.4 PEP 461 - percent formatting support for bytes and bytearray
....................................................................
PEP 461(1) adds support for the ‘%’ *note interpolation operator: 650.
to *note bytes: 331. and *note bytearray: 332.
While interpolation is usually thought of as a string operation, there
are cases where interpolation on ‘bytes’ or ‘bytearrays’ makes sense,
and the work needed to make up for this missing functionality detracts
from the overall readability of the code. This issue is particularly
important when dealing with wire format protocols, which are often a
mixture of binary and ASCII compatible text.
Examples:
>>> b'Hello %b!' % b'World'
b'Hello World!'
>>> b'x=%i y=%f' % (1, 2.5)
b'x=1 y=2.500000'
Unicode is not allowed for ‘%b’, but it is accepted by ‘%a’ (equivalent
of ‘repr(obj).encode('ascii', 'backslashreplace')’):
>>> b'Hello %b!' % 'World'
Traceback (most recent call last):
File "", line 1, in
TypeError: %b requires bytes, or an object that implements __bytes__, not 'str'
>>> b'price: %a' % '10€'
b"price: '10\\u20ac'"
Note that ‘%s’ and ‘%r’ conversion types, although supported, should
only be used in codebases that need compatibility with Python 2.
See also
........
PEP 461(2) – Adding % formatting to bytes and bytearray
PEP written by Ethan Furman; implemented by Neil Schemenauer and
Ethan Furman.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0461
(2) https://www.python.org/dev/peps/pep-0461
File: python.info, Node: PEP 484 - Type Hints, Next: PEP 471 - os scandir function – a better and faster directory iterator, Prev: PEP 461 - percent formatting support for bytes and bytearray, Up: New Features<4>
1.4.2.5 PEP 484 - Type Hints
............................
Function annotation syntax has been a Python feature since version 3.0 (
PEP 3107(1)), however the semantics of annotations has been left
undefined.
Experience has shown that the majority of function annotation uses were
to provide type hints to function parameters and return values. It
became evident that it would be beneficial for Python users, if the
standard library included the base definitions and tools for type
annotations.
PEP 484(2) introduces a *note provisional module: 348. to provide these
standard definitions and tools, along with some conventions for
situations where annotations are not available.
For example, here is a simple function whose argument and return type
are declared in the annotations:
def greeting(name: str) -> str:
return 'Hello ' + name
While these annotations are available at runtime through the usual
‘__annotations__’ attribute, `no automatic type checking happens at
runtime'. Instead, it is assumed that a separate off-line type checker
(e.g. mypy(3)) will be used for on-demand source code analysis.
The type system supports unions, generic types, and a special type named
*note Any: 652. which is consistent with (i.e. assignable to and from)
all types.
See also
........
* *note typing: 119. module documentation
*
PEP 484(4) – Type Hints
PEP written by Guido van Rossum, Jukka Lehtosalo, and Łukasz
Langa; implemented by Guido van Rossum.
*
PEP 483(5) – The Theory of Type Hints
PEP written by Guido van Rossum
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3107
(2) https://www.python.org/dev/peps/pep-0484
(3) http://mypy-lang.org
(4) https://www.python.org/dev/peps/pep-0484
(5) https://www.python.org/dev/peps/pep-0483
File: python.info, Node: PEP 471 - os scandir function – a better and faster directory iterator, Next: PEP 475 Retry system calls failing with EINTR, Prev: PEP 484 - Type Hints, Up: New Features<4>
1.4.2.6 PEP 471 - os.scandir() function – a better and faster directory iterator
................................................................................
PEP 471(1) adds a new directory iteration function, *note os.scandir():
25f, to the standard library. Additionally, *note os.walk(): 654. is
now implemented using ‘scandir’, which makes it 3 to 5 times faster on
POSIX systems and 7 to 20 times faster on Windows systems. This is
largely achieved by greatly reducing the number of calls to *note
os.stat(): 1f1. required to walk a directory tree.
Additionally, ‘scandir’ returns an iterator, as opposed to returning a
list of file names, which improves memory efficiency when iterating over
very large directories.
The following example shows a simple use of *note os.scandir(): 25f. to
display all the files (excluding directories) in the given `path' that
don’t start with ‘'.'’. The *note entry.is_file(): 655. call will
generally not make an additional system call:
for entry in os.scandir(path):
if not entry.name.startswith('.') and entry.is_file():
print(entry.name)
See also
........
PEP 471(2) – os.scandir() function – a better and faster directory iterator
PEP written and implemented by Ben Hoyt with the help of Victor
Stinner.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0471
(2) https://www.python.org/dev/peps/pep-0471
File: python.info, Node: PEP 475 Retry system calls failing with EINTR, Next: PEP 479 Change StopIteration handling inside generators, Prev: PEP 471 - os scandir function – a better and faster directory iterator, Up: New Features<4>
1.4.2.7 PEP 475: Retry system calls failing with EINTR
......................................................
An *note errno.EINTR: 658. error code is returned whenever a system
call, that is waiting for I/O, is interrupted by a signal. Previously,
Python would raise *note InterruptedError: 659. in such cases. This
meant that, when writing a Python application, the developer had two
choices:
1. Ignore the ‘InterruptedError’.
2. Handle the ‘InterruptedError’ and attempt to restart the
interrupted system call at every call site.
The first option makes an application fail intermittently. The second
option adds a large amount of boilerplate that makes the code nearly
unreadable. Compare:
print("Hello World")
and:
while True:
try:
print("Hello World")
break
except InterruptedError:
continue
PEP 475(1) implements automatic retry of system calls on ‘EINTR’. This
removes the burden of dealing with ‘EINTR’ or *note InterruptedError:
659. in user code in most situations and makes Python programs,
including the standard library, more robust. Note that the system call
is only retried if the signal handler does not raise an exception.
Below is a list of functions which are now retried when interrupted by a
signal:
* *note open(): 4f0. and *note io.open(): 65a.;
* functions of the *note faulthandler: 7d. module;
* *note os: c4. functions: *note fchdir(): 65b, *note fchmod(): 65c,
*note fchown(): 65d, *note fdatasync(): 65e, *note fstat(): 65f,
*note fstatvfs(): 660, *note fsync(): 661, *note ftruncate(): 662,
*note mkfifo(): 663, *note mknod(): 664, *note open(): 665, *note
posix_fadvise(): 666, *note posix_fallocate(): 667, *note pread():
3b9, *note pwrite(): 3bc, *note read(): 668, *note readv(): 3b8,
*note sendfile(): 359, *note wait3(): 669, *note wait4(): 66a,
*note wait(): 66b, *note waitid(): 66c, *note waitpid(): 66d, *note
write(): 66e, *note writev(): 3bb.;
* special cases: *note os.close(): 3d2. and *note os.dup2(): 3be. now
ignore *note EINTR: 658. errors; the syscall is not retried (see
the PEP for the rationale);
* *note select: e5. functions: *note devpoll.poll(): 66f, *note
epoll.poll(): 670, *note kqueue.control(): 671, *note poll.poll():
672, *note select(): 673.;
* methods of the *note socket: 674. class: *note accept(): 675, *note
connect(): 676. (except for non-blocking sockets), *note recv():
677, *note recvfrom(): 678, *note recvmsg(): 679, *note send():
67a, *note sendall(): 67b, *note sendmsg(): 67c, *note sendto():
67d.;
* *note signal.sigtimedwait(): 67e. and *note signal.sigwaitinfo():
67f.;
* *note time.sleep(): 680.
See also
........
PEP 475(2) – Retry system calls failing with EINTR
PEP and implementation written by Charles-François Natali and
Victor Stinner, with the help of Antoine Pitrou (the French
connection).
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0475
(2) https://www.python.org/dev/peps/pep-0475
File: python.info, Node: PEP 479 Change StopIteration handling inside generators, Next: PEP 485 A function for testing approximate equality, Prev: PEP 475 Retry system calls failing with EINTR, Up: New Features<4>
1.4.2.8 PEP 479: Change StopIteration handling inside generators
................................................................
The interaction of generators and *note StopIteration: 486. in Python
3.4 and earlier was sometimes surprising, and could conceal obscure
bugs. Previously, ‘StopIteration’ raised accidentally inside a
generator function was interpreted as the end of the iteration by the
loop construct driving the generator.
PEP 479(1) changes the behavior of generators: when a ‘StopIteration’
exception is raised inside a generator, it is replaced with a *note
RuntimeError: 2ba. before it exits the generator frame. The main goal
of this change is to ease debugging in the situation where an unguarded
*note next(): 682. call raises ‘StopIteration’ and causes the iteration
controlled by the generator to terminate silently. This is particularly
pernicious in combination with the ‘yield from’ construct.
This is a backwards incompatible change, so to enable the new behavior,
a *note __future__: 683. import is necessary:
>>> from __future__ import generator_stop
>>> def gen():
... next(iter([]))
... yield
...
>>> next(gen())
Traceback (most recent call last):
File "", line 2, in gen
StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "", line 1, in
RuntimeError: generator raised StopIteration
Without a ‘__future__’ import, a *note PendingDeprecationWarning: 279.
will be raised whenever a *note StopIteration: 486. exception is raised
inside a generator.
See also
........
PEP 479(2) – Change StopIteration handling inside generators
PEP written by Chris Angelico and Guido van Rossum. Implemented by
Chris Angelico, Yury Selivanov and Nick Coghlan.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0479
(2) https://www.python.org/dev/peps/pep-0479
File: python.info, Node: PEP 485 A function for testing approximate equality, Next: PEP 486 Make the Python Launcher aware of virtual environments, Prev: PEP 479 Change StopIteration handling inside generators, Up: New Features<4>
1.4.2.9 PEP 485: A function for testing approximate equality
............................................................
PEP 485(1) adds the *note math.isclose(): 686. and *note
cmath.isclose(): 687. functions which tell whether two values are
approximately equal or “close” to each other. Whether or not two values
are considered close is determined according to given absolute and
relative tolerances. Relative tolerance is the maximum allowed
difference between ‘isclose’ arguments, relative to the larger absolute
value:
>>> import math
>>> a = 5.0
>>> b = 4.99998
>>> math.isclose(a, b, rel_tol=1e-5)
True
>>> math.isclose(a, b, rel_tol=1e-6)
False
It is also possible to compare two values using absolute tolerance,
which must be a non-negative value:
>>> import math
>>> a = 5.0
>>> b = 4.99998
>>> math.isclose(a, b, abs_tol=0.00003)
True
>>> math.isclose(a, b, abs_tol=0.00001)
False
See also
........
PEP 485(2) – A function for testing approximate equality
PEP written by Christopher Barker; implemented by Chris Barker and
Tal Einat.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0485
(2) https://www.python.org/dev/peps/pep-0485
File: python.info, Node: PEP 486 Make the Python Launcher aware of virtual environments, Next: PEP 488 Elimination of PYO files, Prev: PEP 485 A function for testing approximate equality, Up: New Features<4>
1.4.2.10 PEP 486: Make the Python Launcher aware of virtual environments
........................................................................
PEP 486(1) makes the Windows launcher (see PEP 397(2)) aware of an
active virtual environment. When the default interpreter would be used
and the ‘VIRTUAL_ENV’ environment variable is set, the interpreter in
the virtual environment will be used.
See also
........
PEP 486(3) – Make the Python Launcher aware of virtual environments
PEP written and implemented by Paul Moore.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0486
(2) https://www.python.org/dev/peps/pep-0397
(3) https://www.python.org/dev/peps/pep-0486
File: python.info, Node: PEP 488 Elimination of PYO files, Next: PEP 489 Multi-phase extension module initialization, Prev: PEP 486 Make the Python Launcher aware of virtual environments, Up: New Features<4>
1.4.2.11 PEP 488: Elimination of PYO files
..........................................
PEP 488(1) does away with the concept of ‘.pyo’ files. This means that
‘.pyc’ files represent both unoptimized and optimized bytecode. To
prevent the need to constantly regenerate bytecode files, ‘.pyc’ files
now have an optional ‘opt-’ tag in their name when the bytecode is
optimized. This has the side-effect of no more bytecode file name
clashes when running under either *note -O: 68b. or *note -OO: 68c.
Consequently, bytecode files generated from *note -O: 68b, and *note
-OO: 68c. may now exist simultaneously. *note
importlib.util.cache_from_source(): 557. has an updated API to help with
this change.
See also
........
PEP 488(2) – Elimination of PYO files
PEP written and implemented by Brett Cannon.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0488
(2) https://www.python.org/dev/peps/pep-0488
File: python.info, Node: PEP 489 Multi-phase extension module initialization, Prev: PEP 488 Elimination of PYO files, Up: New Features<4>
1.4.2.12 PEP 489: Multi-phase extension module initialization
.............................................................
PEP 489(1) updates extension module initialization to take advantage of
the two step module loading mechanism introduced by PEP 451(2) in Python
3.4.
This change brings the import semantics of extension modules that opt-in
to using the new mechanism much closer to those of Python source and
bytecode modules, including the ability to use any valid identifier as a
module name, rather than being restricted to ASCII.
See also
........
PEP 489(3) – Multi-phase extension module initialization
PEP written by Petr Viktorin, Stefan Behnel, and Nick Coghlan;
implemented by Petr Viktorin.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0489
(2) https://www.python.org/dev/peps/pep-0451
(3) https://www.python.org/dev/peps/pep-0489
File: python.info, Node: Other Language Changes<4>, Next: New Modules<4>, Prev: New Features<4>, Up: What’s New In Python 3 5
1.4.3 Other Language Changes
----------------------------
Some smaller changes made to the core Python language are:
* Added the ‘"namereplace"’ error handlers. The ‘"backslashreplace"’
error handlers now work with decoding and translating.
(Contributed by Serhiy Storchaka in bpo-19676(1) and bpo-22286(2).)
* The *note -b: 415. option now affects comparisons of *note bytes:
331. with *note int: 184. (Contributed by Serhiy Storchaka in
bpo-23681(3).)
* New Kazakh ‘kz1048’ and Tajik ‘koi8_t’ *note codecs: 68f.
(Contributed by Serhiy Storchaka in bpo-22682(4) and bpo-22681(5).)
* Property docstrings are now writable. This is especially useful
for *note collections.namedtuple(): 1b7. docstrings. (Contributed
by Berker Peksag in bpo-24064(6).)
* Circular imports involving relative imports are now supported.
(Contributed by Brett Cannon and Antoine Pitrou in bpo-17636(7).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19676
(2) https://bugs.python.org/issue22286
(3) https://bugs.python.org/issue23681
(4) https://bugs.python.org/issue22682
(5) https://bugs.python.org/issue22681
(6) https://bugs.python.org/issue24064
(7) https://bugs.python.org/issue17636
File: python.info, Node: New Modules<4>, Next: Improved Modules<4>, Prev: Other Language Changes<4>, Up: What’s New In Python 3 5
1.4.4 New Modules
-----------------
* Menu:
* typing: typing<3>.
* zipapp: zipapp<2>.
File: python.info, Node: typing<3>, Next: zipapp<2>, Up: New Modules<4>
1.4.4.1 typing
..............
The new *note typing: 119. *note provisional: 348. module provides
standard definitions and tools for function type annotations. See *note
Type Hints: 62e. for more information.
File: python.info, Node: zipapp<2>, Prev: typing<3>, Up: New Modules<4>
1.4.4.2 zipapp
..............
The new *note zipapp: 141. module (specified in PEP 441(1)) provides an
API and command line tool for creating executable Python Zip
Applications, which were introduced in Python 2.6 in bpo-1739468(2), but
which were not well publicized, either at the time or since.
With the new module, bundling your application is as simple as putting
all the files, including a ‘__main__.py’ file, into a directory ‘myapp’
and running:
$ python -m zipapp myapp
$ python myapp.pyz
The module implementation has been contributed by Paul Moore in
bpo-23491(3).
See also
........
PEP 441(4) – Improving Python ZIP Application Support
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0441
(2) https://bugs.python.org/issue1739468
(3) https://bugs.python.org/issue23491
(4) https://www.python.org/dev/peps/pep-0441
File: python.info, Node: Improved Modules<4>, Next: Other module-level changes, Prev: New Modules<4>, Up: What’s New In Python 3 5
1.4.5 Improved Modules
----------------------
* Menu:
* argparse: argparse<2>.
* asyncio: asyncio<5>.
* bz2::
* cgi::
* cmath: cmath<2>.
* code::
* collections: collections<5>.
* collections.abc: collections abc.
* compileall: compileall<2>.
* concurrent.futures: concurrent futures<3>.
* configparser::
* contextlib: contextlib<3>.
* csv: csv<2>.
* curses: curses<2>.
* dbm: dbm<4>.
* difflib::
* distutils: distutils<4>.
* doctest::
* email: email<2>.
* enum: enum<4>.
* faulthandler: faulthandler<2>.
* functools: functools<3>.
* glob::
* gzip: gzip<2>.
* heapq::
* http::
* http.client: http client<3>.
* idlelib and IDLE: idlelib and IDLE<3>.
* imaplib::
* imghdr::
* importlib: importlib<5>.
* inspect: inspect<3>.
* io: io<3>.
* ipaddress: ipaddress<2>.
* json: json<2>.
* linecache::
* locale: locale<3>.
* logging: logging<4>.
* lzma::
* math: math<4>.
* multiprocessing: multiprocessing<4>.
* operator::
* os: os<5>.
* pathlib: pathlib<4>.
* pickle: pickle<3>.
* poplib::
* re: re<4>.
* readline: readline<2>.
* selectors::
* shutil: shutil<2>.
* signal: signal<2>.
* smtpd::
* smtplib::
* sndhdr::
* socket: socket<5>.
* ssl: ssl<6>.
* sqlite3: sqlite3<3>.
* subprocess: subprocess<3>.
* sys: sys<5>.
* sysconfig::
* tarfile: tarfile<2>.
* threading: threading<3>.
* time: time<4>.
* timeit: timeit<2>.
* tkinter: tkinter<5>.
* traceback: traceback<2>.
* types: types<2>.
* unicodedata: unicodedata<4>.
* unittest: unittest<3>.
* unittest.mock: unittest mock<3>.
* urllib::
* wsgiref::
* xmlrpc: xmlrpc<2>.
* xml.sax: xml sax.
* zipfile: zipfile<3>.
File: python.info, Node: argparse<2>, Next: asyncio<5>, Up: Improved Modules<4>
1.4.5.1 argparse
................
The *note ArgumentParser: 695. class now allows disabling *note
abbreviated usage: 696. of long options by setting *note allow_abbrev:
697. to ‘False’. (Contributed by Jonathan Paugh, Steven Bethard, paul
j3 and Daniel Eriksson in bpo-14910(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14910
File: python.info, Node: asyncio<5>, Next: bz2, Prev: argparse<2>, Up: Improved Modules<4>
1.4.5.2 asyncio
...............
Since the *note asyncio: a. module is *note provisional: 348, all
changes introduced in Python 3.5 have also been backported to Python
3.4.x.
Notable changes in the *note asyncio: a. module since Python 3.4.0:
* New debugging APIs: *note loop.set_debug(): 699. and *note
loop.get_debug(): 69a. methods. (Contributed by Victor Stinner.)
* The proactor event loop now supports SSL. (Contributed by Antoine
Pitrou and Victor Stinner in bpo-22560(1).)
* A new *note loop.is_closed(): 69b. method to check if the event
loop is closed. (Contributed by Victor Stinner in bpo-21326(2).)
* A new *note loop.create_task(): 1af. to conveniently create and
schedule a new *note Task: 1ad. for a coroutine. The ‘create_task’
method is also used by all asyncio functions that wrap coroutines
into tasks, such as *note asyncio.wait(): 289, *note
asyncio.gather(): 286, etc. (Contributed by Victor Stinner.)
* A new *note transport.get_write_buffer_limits(): 69c. method to
inquire for `high-' and `low-' water limits of the flow control.
(Contributed by Victor Stinner.)
* The ‘async()’ function is deprecated in favor of *note
ensure_future(): 517. (Contributed by Yury Selivanov.)
* New *note loop.set_task_factory(): 69d. and *note
loop.get_task_factory(): 69e. methods to customize the task factory
that *note loop.create_task(): 1af. method uses. (Contributed by
Yury Selivanov.)
* New *note Queue.join(): 69f. and *note Queue.task_done(): 6a0.
queue methods. (Contributed by Victor Stinner.)
* The ‘JoinableQueue’ class was removed, in favor of the *note
asyncio.Queue: 290. class. (Contributed by Victor Stinner.)
Updates in 3.5.1:
* The *note ensure_future(): 517. function and all functions that use
it, such as *note loop.run_until_complete(): 518, now accept all
kinds of *note awaitable objects: 519. (Contributed by Yury
Selivanov.)
* New *note run_coroutine_threadsafe(): 51a. function to submit
coroutines to event loops from other threads. (Contributed by
Vincent Michel.)
* New *note Transport.is_closing(): 51b. method to check if the
transport is closing or closed. (Contributed by Yury Selivanov.)
* The *note loop.create_server(): 35d. method can now accept a list
of hosts. (Contributed by Yann Sionneau.)
Updates in 3.5.2:
* New *note loop.create_future(): 51c. method to create Future
objects. This allows alternative event loop implementations, such
as uvloop(3), to provide a faster *note asyncio.Future: 443.
implementation. (Contributed by Yury Selivanov.)
* New *note loop.get_exception_handler(): 51d. method to get the
current exception handler. (Contributed by Yury Selivanov.)
* New *note StreamReader.readuntil(): 51e. method to read data from
the stream until a separator bytes sequence appears. (Contributed
by Mark Korenberg.)
* The *note loop.create_connection(): 1b2. and *note
loop.create_server(): 35d. methods are optimized to avoid calling
the system ‘getaddrinfo’ function if the address is already
resolved. (Contributed by A. Jesse Jiryu Davis.)
* The *note loop.sock_connect(sock, address): 6a1. no longer requires
the `address' to be resolved prior to the call. (Contributed by A.
Jesse Jiryu Davis.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22560
(2) https://bugs.python.org/issue21326
(3) https://github.com/MagicStack/uvloop
File: python.info, Node: bz2, Next: cgi, Prev: asyncio<5>, Up: Improved Modules<4>
1.4.5.3 bz2
...........
The *note BZ2Decompressor.decompress: 6a3. method now accepts an
optional `max_length' argument to limit the maximum size of decompressed
data. (Contributed by Nikolaus Rath in bpo-15955(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15955
File: python.info, Node: cgi, Next: cmath<2>, Prev: bz2, Up: Improved Modules<4>
1.4.5.4 cgi
...........
The ‘FieldStorage’ class now supports the *note context manager: 568.
protocol. (Contributed by Berker Peksag in bpo-20289(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20289
File: python.info, Node: cmath<2>, Next: code, Prev: cgi, Up: Improved Modules<4>
1.4.5.5 cmath
.............
A new function *note isclose(): 687. provides a way to test for
approximate equality. (Contributed by Chris Barker and Tal Einat in
bpo-24270(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24270
File: python.info, Node: code, Next: collections<5>, Prev: cmath<2>, Up: Improved Modules<4>
1.4.5.6 code
............
The *note InteractiveInterpreter.showtraceback(): 6a7. method now prints
the full chained traceback, just like the interactive interpreter.
(Contributed by Claudiu Popa in bpo-17442(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17442
File: python.info, Node: collections<5>, Next: collections abc, Prev: code, Up: Improved Modules<4>
1.4.5.7 collections
...................
The *note OrderedDict: 1b9. class is now implemented in C, which makes
it 4 to 100 times faster. (Contributed by Eric Snow in bpo-16991(1).)
‘OrderedDict.items()’, ‘OrderedDict.keys()’, ‘OrderedDict.values()’
views now support *note reversed(): 18e. iteration. (Contributed by
Serhiy Storchaka in bpo-19505(2).)
The *note deque: 531. class now defines *note index(): 6a9, *note
insert(): 6aa, and *note copy(): 6ab, and supports the ‘+’ and ‘*’
operators. This allows deques to be recognized as a *note
MutableSequence: 6ac. and improves their substitutability for lists.
(Contributed by Raymond Hettinger in bpo-23704(3).)
Docstrings produced by *note namedtuple(): 1b7. can now be updated:
Point = namedtuple('Point', ['x', 'y'])
Point.__doc__ += ': Cartesian coodinate'
Point.x.__doc__ = 'abscissa'
Point.y.__doc__ = 'ordinate'
(Contributed by Berker Peksag in bpo-24064(4).)
The *note UserString: 6ad. class now implements the *note
__getnewargs__(): 6ae, *note __rmod__(): 6af, *note casefold(): 6b0,
*note format_map(): 6b1, *note isprintable(): 6b2, and *note
maketrans(): 6b3. methods to match the corresponding methods of *note
str: 330. (Contributed by Joe Jevnik in bpo-22189(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16991
(2) https://bugs.python.org/issue19505
(3) https://bugs.python.org/issue23704
(4) https://bugs.python.org/issue24064
(5) https://bugs.python.org/issue22189
File: python.info, Node: collections abc, Next: compileall<2>, Prev: collections<5>, Up: Improved Modules<4>
1.4.5.8 collections.abc
.......................
The ‘Sequence.index()’ method now accepts `start' and `stop' arguments
to match the corresponding methods of *note tuple: 47e, *note list: 262,
etc. (Contributed by Devin Jeanpierre in bpo-23086(1).)
A new *note Generator: 6b5. abstract base class. (Contributed by Stefan
Behnel in bpo-24018(2).)
New *note Awaitable: 6b6, *note Coroutine: 6b7, *note AsyncIterator:
6b8, and *note AsyncIterable: 6b9. abstract base classes. (Contributed
by Yury Selivanov in bpo-24184(3).)
For earlier Python versions, a backport of the new ABCs is available in
an external PyPI package(4).
---------- Footnotes ----------
(1) https://bugs.python.org/issue23086
(2) https://bugs.python.org/issue24018
(3) https://bugs.python.org/issue24184
(4) https://pypi.org/project/backports_abc
File: python.info, Node: compileall<2>, Next: concurrent futures<3>, Prev: collections abc, Up: Improved Modules<4>
1.4.5.9 compileall
..................
A new *note compileall: 21. option, ‘-j `N'’, allows running `N' workers
simultaneously to perform parallel bytecode compilation. The *note
compile_dir(): 372. function has a corresponding ‘workers’ parameter.
(Contributed by Claudiu Popa in bpo-16104(1).)
Another new option, ‘-r’, allows controlling the maximum recursion level
for subdirectories. (Contributed by Claudiu Popa in bpo-19628(2).)
The ‘-q’ command line option can now be specified more than once, in
which case all output, including errors, will be suppressed. The
corresponding ‘quiet’ parameter in *note compile_dir(): 372, *note
compile_file(): 6bb, and *note compile_path(): 6bc. can now accept an
integer value indicating the level of output suppression. (Contributed
by Thomas Kluyver in bpo-21338(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16104
(2) https://bugs.python.org/issue19628
(3) https://bugs.python.org/issue21338
File: python.info, Node: concurrent futures<3>, Next: configparser, Prev: compileall<2>, Up: Improved Modules<4>
1.4.5.10 concurrent.futures
...........................
The *note Executor.map(): 6be. method now accepts a `chunksize' argument
to allow batching of tasks to improve performance when *note
ProcessPoolExecutor(): 2a4. is used. (Contributed by Dan O’Reilly in
bpo-11271(1).)
The number of workers in the *note ThreadPoolExecutor: 27a. constructor
is optional now. The default value is 5 times the number of CPUs.
(Contributed by Claudiu Popa in bpo-21527(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11271
(2) https://bugs.python.org/issue21527
File: python.info, Node: configparser, Next: contextlib<3>, Prev: concurrent futures<3>, Up: Improved Modules<4>
1.4.5.11 configparser
.....................
*note configparser: 23. now provides a way to customize the conversion
of values by specifying a dictionary of converters in the *note
ConfigParser: 4aa. constructor, or by defining them as methods in
‘ConfigParser’ subclasses. Converters defined in a parser instance are
inherited by its section proxies.
Example:
>>> import configparser
>>> conv = {}
>>> conv['list'] = lambda v: [e.strip() for e in v.split() if e.strip()]
>>> cfg = configparser.ConfigParser(converters=conv)
>>> cfg.read_string("""
... [s]
... list = a b c d e f g
... """)
>>> cfg.get('s', 'list')
'a b c d e f g'
>>> cfg.getlist('s', 'list')
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> section = cfg['s']
>>> section.getlist('list')
['a', 'b', 'c', 'd', 'e', 'f', 'g']
(Contributed by Łukasz Langa in bpo-18159(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18159
File: python.info, Node: contextlib<3>, Next: csv<2>, Prev: configparser, Up: Improved Modules<4>
1.4.5.12 contextlib
...................
The new *note redirect_stderr(): 6c1. *note context manager: 568.
(similar to *note redirect_stdout(): 6c2.) makes it easier for utility
scripts to handle inflexible APIs that write their output to *note
sys.stderr: 30f. and don’t provide any options to redirect it:
>>> import contextlib, io, logging
>>> f = io.StringIO()
>>> with contextlib.redirect_stderr(f):
... logging.warning('warning')
...
>>> f.getvalue()
'WARNING:root:warning\n'
(Contributed by Berker Peksag in bpo-22389(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22389
File: python.info, Node: csv<2>, Next: curses<2>, Prev: contextlib<3>, Up: Improved Modules<4>
1.4.5.13 csv
............
The *note writerow(): 6c4. method now supports arbitrary iterables, not
just sequences. (Contributed by Serhiy Storchaka in bpo-23171(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23171
File: python.info, Node: curses<2>, Next: dbm<4>, Prev: csv<2>, Up: Improved Modules<4>
1.4.5.14 curses
...............
The new *note update_lines_cols(): 6c6. function updates the ‘LINES’ and
‘COLS’ environment variables. This is useful for detecting manual
screen resizing. (Contributed by Arnon Yaari in bpo-4254(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4254
File: python.info, Node: dbm<4>, Next: difflib, Prev: curses<2>, Up: Improved Modules<4>
1.4.5.15 dbm
............
*note dumb.open: 2bf. always creates a new database when the flag has
the value ‘"n"’. (Contributed by Claudiu Popa in bpo-18039(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18039
File: python.info, Node: difflib, Next: distutils<4>, Prev: dbm<4>, Up: Improved Modules<4>
1.4.5.16 difflib
................
The charset of HTML documents generated by *note HtmlDiff.make_file():
6c9. can now be customized by using a new `charset' keyword-only
argument. The default charset of HTML document changed from
‘"ISO-8859-1"’ to ‘"utf-8"’. (Contributed by Berker Peksag in
bpo-2052(1).)
The *note diff_bytes(): 6ca. function can now compare lists of byte
strings. This fixes a regression from Python 2. (Contributed by Terry
J. Reedy and Greg Ward in bpo-17445(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue2052
(2) https://bugs.python.org/issue17445
File: python.info, Node: distutils<4>, Next: doctest, Prev: difflib, Up: Improved Modules<4>
1.4.5.17 distutils
..................
Both the ‘build’ and ‘build_ext’ commands now accept a ‘-j’ option to
enable parallel building of extension modules. (Contributed by Antoine
Pitrou in bpo-5309(1).)
The *note distutils: 39. module now supports ‘xz’ compression, and can
be enabled by passing ‘xztar’ as an argument to ‘bdist --format’.
(Contributed by Serhiy Storchaka in bpo-16314(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5309
(2) https://bugs.python.org/issue16314
File: python.info, Node: doctest, Next: email<2>, Prev: distutils<4>, Up: Improved Modules<4>
1.4.5.18 doctest
................
The *note DocTestSuite(): 6cd. function returns an empty *note
unittest.TestSuite: 6ce. if `module' contains no docstrings, instead of
raising *note ValueError: 1fb. (Contributed by Glenn Jones in
bpo-15916(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15916
File: python.info, Node: email<2>, Next: enum<4>, Prev: doctest, Up: Improved Modules<4>
1.4.5.19 email
..............
A new policy option *note Policy.mangle_from_: 6d0. controls whether or
not lines that start with ‘"From "’ in email bodies are prefixed with a
‘">"’ character by generators. The default is ‘True’ for *note
compat32: 540. and ‘False’ for all other policies. (Contributed by
Milan Oberkirch in bpo-20098(1).)
A new *note Message.get_content_disposition(): 6d1. method provides easy
access to a canonical value for the ‘Content-Disposition’ header.
(Contributed by Abhilash Raj in bpo-21083(2).)
A new policy option *note EmailPolicy.utf8: 6d2. can be set to ‘True’ to
encode email headers using the UTF-8 charset instead of using encoded
words. This allows ‘Messages’ to be formatted according to RFC 6532(3)
and used with an SMTP server that supports the RFC 6531(4) ‘SMTPUTF8’
extension. (Contributed by R. David Murray in bpo-24211(5).)
The *note mime.text.MIMEText: 6d3. constructor now accepts a *note
charset.Charset: 6d4. instance. (Contributed by Claude Paroz and Berker
Peksag in bpo-16324(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20098
(2) https://bugs.python.org/issue21083
(3) https://tools.ietf.org/html/rfc6532.html
(4) https://tools.ietf.org/html/rfc6531.html
(5) https://bugs.python.org/issue24211
(6) https://bugs.python.org/issue16324
File: python.info, Node: enum<4>, Next: faulthandler<2>, Prev: email<2>, Up: Improved Modules<4>
1.4.5.20 enum
.............
The *note Enum: 387. callable has a new parameter `start' to specify the
initial number of enum values if only `names' are provided:
>>> Animal = enum.Enum('Animal', 'cat dog', start=10)
>>> Animal.cat
>>> Animal.dog
(Contributed by Ethan Furman in bpo-21706(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21706
File: python.info, Node: faulthandler<2>, Next: functools<3>, Prev: enum<4>, Up: Improved Modules<4>
1.4.5.21 faulthandler
.....................
The *note enable(): 548, *note register(): 6d7, *note dump_traceback():
6d8. and *note dump_traceback_later(): 6d9. functions now accept file
descriptors in addition to file-like objects. (Contributed by Wei Wu in
bpo-23566(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23566
File: python.info, Node: functools<3>, Next: glob, Prev: faulthandler<2>, Up: Improved Modules<4>
1.4.5.22 functools
..................
Most of the *note lru_cache(): 1c7. machinery is now implemented in C,
making it significantly faster. (Contributed by Matt Joiner, Alexey
Kachayev, and Serhiy Storchaka in bpo-14373(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14373
File: python.info, Node: glob, Next: gzip<2>, Prev: functools<3>, Up: Improved Modules<4>
1.4.5.23 glob
.............
The *note iglob(): 5c7. and *note glob(): 5c6. functions now support
recursive search in subdirectories, using the ‘"**"’ pattern.
(Contributed by Serhiy Storchaka in bpo-13968(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13968
File: python.info, Node: gzip<2>, Next: heapq, Prev: glob, Up: Improved Modules<4>
1.4.5.24 gzip
.............
The `mode' argument of the *note GzipFile: 6dd. constructor now accepts
‘"x"’ to request exclusive creation. (Contributed by Tim Heaney in
bpo-19222(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19222
File: python.info, Node: heapq, Next: http, Prev: gzip<2>, Up: Improved Modules<4>
1.4.5.25 heapq
..............
Element comparison in *note merge(): 6df. can now be customized by
passing a *note key function: 6e0. in a new optional `key' keyword
argument, and a new optional `reverse' keyword argument can be used to
reverse element comparison:
>>> import heapq
>>> a = ['9', '777', '55555']
>>> b = ['88', '6666']
>>> list(heapq.merge(a, b, key=len))
['9', '88', '777', '6666', '55555']
>>> list(heapq.merge(reversed(a), reversed(b), key=len, reverse=True))
['55555', '6666', '777', '88', '9']
(Contributed by Raymond Hettinger in bpo-13742(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13742
File: python.info, Node: http, Next: http client<3>, Prev: heapq, Up: Improved Modules<4>
1.4.5.26 http
.............
A new *note HTTPStatus: 6e2. enum that defines a set of HTTP status
codes, reason phrases and long descriptions written in English.
(Contributed by Demian Brecht in bpo-21793(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21793
File: python.info, Node: http client<3>, Next: idlelib and IDLE<3>, Prev: http, Up: Improved Modules<4>
1.4.5.27 http.client
....................
*note HTTPConnection.getresponse(): 6e4. now raises a *note
RemoteDisconnected: 6e5. exception when a remote server connection is
closed unexpectedly. Additionally, if a *note ConnectionError: 6e6. (of
which ‘RemoteDisconnected’ is a subclass) is raised, the client socket
is now closed automatically, and will reconnect on the next request:
import http.client
conn = http.client.HTTPConnection('www.python.org')
for retries in range(3):
try:
conn.request('GET', '/')
resp = conn.getresponse()
except http.client.RemoteDisconnected:
pass
(Contributed by Martin Panter in bpo-3566(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue3566
File: python.info, Node: idlelib and IDLE<3>, Next: imaplib, Prev: http client<3>, Up: Improved Modules<4>
1.4.5.28 idlelib and IDLE
.........................
Since idlelib implements the IDLE shell and editor and is not intended
for import by other programs, it gets improvements with every release.
See ‘Lib/idlelib/NEWS.txt’ for a cumulative list of changes since 3.4.0,
as well as changes made in future 3.5.x releases. This file is also
available from the IDLE Help ‣ About IDLE dialog.
File: python.info, Node: imaplib, Next: imghdr, Prev: idlelib and IDLE<3>, Up: Improved Modules<4>
1.4.5.29 imaplib
................
The *note IMAP4: 60b. class now supports the *note context manager: 568.
protocol. When used in a *note with: 6e9. statement, the IMAP4 ‘LOGOUT’
command will be called automatically at the end of the block.
(Contributed by Tarek Ziadé and Serhiy Storchaka in bpo-4972(1).)
The *note imaplib: 98. module now supports RFC 5161(2) (ENABLE
Extension) and RFC 6855(3) (UTF-8 Support) via the *note IMAP4.enable():
6ea. method. A new *note IMAP4.utf8_enabled: 6eb. attribute tracks
whether or not RFC 6855(4) support is enabled. (Contributed by Milan
Oberkirch, R. David Murray, and Maciej Szulik in bpo-21800(5).)
The *note imaplib: 98. module now automatically encodes non-ASCII string
usernames and passwords using UTF-8, as recommended by the RFCs.
(Contributed by Milan Oberkirch in bpo-21800(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4972
(2) https://tools.ietf.org/html/rfc5161.html
(3) https://tools.ietf.org/html/rfc6855.html
(4) https://tools.ietf.org/html/rfc6855.html
(5) https://bugs.python.org/issue21800
(6) https://bugs.python.org/issue21800
File: python.info, Node: imghdr, Next: importlib<5>, Prev: imaplib, Up: Improved Modules<4>
1.4.5.30 imghdr
...............
The *note what(): 6ed. function now recognizes the OpenEXR(1) format
(contributed by Martin Vignali and Claudiu Popa in bpo-20295(2)), and
the WebP(3) format (contributed by Fabrice Aneche and Claudiu Popa in
bpo-20197(4).)
---------- Footnotes ----------
(1) http://www.openexr.com
(2) https://bugs.python.org/issue20295
(3) https://en.wikipedia.org/wiki/WebP
(4) https://bugs.python.org/issue20197
File: python.info, Node: importlib<5>, Next: inspect<3>, Prev: imghdr, Up: Improved Modules<4>
1.4.5.31 importlib
..................
The *note util.LazyLoader: 553. class allows for lazy loading of modules
in applications where startup time is important. (Contributed by Brett
Cannon in bpo-17621(1).)
The *note abc.InspectLoader.source_to_code(): 6ef. method is now a
static method. This makes it easier to initialize a module object with
code compiled from a string by running ‘exec(code, module.__dict__)’.
(Contributed by Brett Cannon in bpo-21156(2).)
The new *note util.module_from_spec(): 6f0. function is now the
preferred way to create a new module. As opposed to creating a *note
types.ModuleType: 6f1. instance directly, this new function will set the
various import-controlled attributes based on the passed-in spec object.
(Contributed by Brett Cannon in bpo-20383(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17621
(2) https://bugs.python.org/issue21156
(3) https://bugs.python.org/issue20383
File: python.info, Node: inspect<3>, Next: io<3>, Prev: importlib<5>, Up: Improved Modules<4>
1.4.5.32 inspect
................
Both the *note Signature: 6f3. and *note Parameter: 6f4. classes are now
picklable and hashable. (Contributed by Yury Selivanov in bpo-20726(1)
and bpo-20334(2).)
A new *note BoundArguments.apply_defaults(): 6f5. method provides a way
to set default values for missing arguments:
>>> def foo(a, b='ham', *args): pass
>>> ba = inspect.signature(foo).bind('spam')
>>> ba.apply_defaults()
>>> ba.arguments
OrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())])
(Contributed by Yury Selivanov in bpo-24190(3).)
A new class method *note Signature.from_callable(): 6f6. makes
subclassing of *note Signature: 6f3. easier. (Contributed by Yury
Selivanov and Eric Snow in bpo-17373(4).)
The *note signature(): 55b. function now accepts a `follow_wrapped'
optional keyword argument, which, when set to ‘False’, disables
automatic following of ‘__wrapped__’ links. (Contributed by Yury
Selivanov in bpo-20691(5).)
A set of new functions to inspect *note coroutine functions: 63f. and
*note coroutine objects: 1a6. has been added: *note iscoroutine(): 6f7,
*note iscoroutinefunction(): 6f8, *note isawaitable(): 6f9, *note
getcoroutinelocals(): 6fa, and *note getcoroutinestate(): 6fb.
(Contributed by Yury Selivanov in bpo-24017(6) and bpo-24400(7).)
The *note stack(): 6fc, *note trace(): 6fd, *note getouterframes(): 6fe,
and *note getinnerframes(): 6ff. functions now return a list of named
tuples. (Contributed by Daniel Shahaf in bpo-16808(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20726
(2) https://bugs.python.org/issue20334
(3) https://bugs.python.org/issue24190
(4) https://bugs.python.org/issue17373
(5) https://bugs.python.org/issue20691
(6) https://bugs.python.org/issue24017
(7) https://bugs.python.org/issue24400
(8) https://bugs.python.org/issue16808
File: python.info, Node: io<3>, Next: ipaddress<2>, Prev: inspect<3>, Up: Improved Modules<4>
1.4.5.33 io
...........
A new *note BufferedIOBase.readinto1(): 701. method, that uses at most
one call to the underlying raw stream’s *note RawIOBase.read(): 702. or
*note RawIOBase.readinto(): 703. methods. (Contributed by Nikolaus Rath
in bpo-20578(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20578
File: python.info, Node: ipaddress<2>, Next: json<2>, Prev: io<3>, Up: Improved Modules<4>
1.4.5.34 ipaddress
..................
Both the *note IPv4Network: 3a0. and *note IPv6Network: 39f. classes now
accept an ‘(address, netmask)’ tuple argument, so as to easily construct
network objects from existing addresses:
>>> import ipaddress
>>> ipaddress.IPv4Network(('127.0.0.0', 8))
IPv4Network('127.0.0.0/8')
>>> ipaddress.IPv4Network(('127.0.0.0', '255.0.0.0'))
IPv4Network('127.0.0.0/8')
(Contributed by Peter Moody and Antoine Pitrou in bpo-16531(1).)
A new ‘reverse_pointer’ attribute for the *note IPv4Network: 3a0. and
*note IPv6Network: 39f. classes returns the name of the reverse DNS PTR
record:
>>> import ipaddress
>>> addr = ipaddress.IPv4Address('127.0.0.1')
>>> addr.reverse_pointer
'1.0.0.127.in-addr.arpa'
>>> addr6 = ipaddress.IPv6Address('::1')
>>> addr6.reverse_pointer
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa'
(Contributed by Leon Weber in bpo-20480(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16531
(2) https://bugs.python.org/issue20480
File: python.info, Node: json<2>, Next: linecache, Prev: ipaddress<2>, Up: Improved Modules<4>
1.4.5.35 json
.............
The *note json.tool: a5. command line interface now preserves the order
of keys in JSON objects passed in input. The new ‘--sort-keys’ option
can be used to sort the keys alphabetically. (Contributed by Berker
Peksag in bpo-21650(1).)
JSON decoder now raises *note JSONDecodeError: 706. instead of *note
ValueError: 1fb. to provide better context information about the error.
(Contributed by Serhiy Storchaka in bpo-19361(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21650
(2) https://bugs.python.org/issue19361
File: python.info, Node: linecache, Next: locale<3>, Prev: json<2>, Up: Improved Modules<4>
1.4.5.36 linecache
..................
A new *note lazycache(): 708. function can be used to capture
information about a non-file-based module to permit getting its lines
later via *note getline(): 709. This avoids doing I/O until a line is
actually needed, without having to carry the module globals around
indefinitely. (Contributed by Robert Collins in bpo-17911(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17911
File: python.info, Node: locale<3>, Next: logging<4>, Prev: linecache, Up: Improved Modules<4>
1.4.5.37 locale
...............
A new *note delocalize(): 70b. function can be used to convert a string
into a normalized number string, taking the ‘LC_NUMERIC’ settings into
account:
>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
'de_DE.UTF-8'
>>> locale.delocalize('1.234,56')
'1234.56'
>>> locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.delocalize('1,234.56')
'1234.56'
(Contributed by Cédric Krier in bpo-13918(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13918
File: python.info, Node: logging<4>, Next: lzma, Prev: locale<3>, Up: Improved Modules<4>
1.4.5.38 logging
................
All logging methods (*note Logger: 3a7. *note log(): 70d, *note
exception(): 70e, *note critical(): 70f, *note debug(): 710, etc.), now
accept exception instances as an `exc_info' argument, in addition to
boolean values and exception tuples:
>>> import logging
>>> try:
... 1/0
... except ZeroDivisionError as ex:
... logging.error('exception', exc_info=ex)
ERROR:root:exception
(Contributed by Yury Selivanov in bpo-20537(1).)
The *note handlers.HTTPHandler: 711. class now accepts an optional *note
ssl.SSLContext: 3e4. instance to configure SSL settings used in an HTTP
connection. (Contributed by Alex Gaynor in bpo-22788(2).)
The *note handlers.QueueListener: 712. class now takes a
`respect_handler_level' keyword argument which, if set to ‘True’, will
pass messages to handlers taking handler levels into account.
(Contributed by Vinay Sajip.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20537
(2) https://bugs.python.org/issue22788
File: python.info, Node: lzma, Next: math<4>, Prev: logging<4>, Up: Improved Modules<4>
1.4.5.39 lzma
.............
The *note LZMADecompressor.decompress(): 714. method now accepts an
optional `max_length' argument to limit the maximum size of decompressed
data. (Contributed by Martin Panter in bpo-15955(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15955
File: python.info, Node: math<4>, Next: multiprocessing<4>, Prev: lzma, Up: Improved Modules<4>
1.4.5.40 math
.............
Two new constants have been added to the *note math: b1. module: *note
inf: 528. and *note nan: 529. (Contributed by Mark Dickinson in
bpo-23185(1).)
A new function *note isclose(): 686. provides a way to test for
approximate equality. (Contributed by Chris Barker and Tal Einat in
bpo-24270(2).)
A new *note gcd(): 716. function has been added. The *note
fractions.gcd(): 717. function is now deprecated. (Contributed by Mark
Dickinson and Serhiy Storchaka in bpo-22486(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23185
(2) https://bugs.python.org/issue24270
(3) https://bugs.python.org/issue22486
File: python.info, Node: multiprocessing<4>, Next: operator, Prev: math<4>, Up: Improved Modules<4>
1.4.5.41 multiprocessing
........................
*note sharedctypes.synchronized(): 719. objects now support the *note
context manager: 568. protocol. (Contributed by Charles-François Natali
in bpo-21565(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21565
File: python.info, Node: operator, Next: os<5>, Prev: multiprocessing<4>, Up: Improved Modules<4>
1.4.5.42 operator
.................
*note attrgetter(): 71b, *note itemgetter(): 261, and *note
methodcaller(): 71c. objects now support pickling. (Contributed by Josh
Rosenberg and Serhiy Storchaka in bpo-22955(1).)
New *note matmul(): 71d. and *note imatmul(): 71e. functions to perform
matrix multiplication. (Contributed by Benjamin Peterson in
bpo-21176(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22955
(2) https://bugs.python.org/issue21176
File: python.info, Node: os<5>, Next: pathlib<4>, Prev: operator, Up: Improved Modules<4>
1.4.5.43 os
...........
The new *note scandir(): 25f. function returning an iterator of *note
DirEntry: 4f1. objects has been added. If possible, *note scandir():
25f. extracts file attributes while scanning a directory, removing the
need to perform subsequent system calls to determine file type or
attributes, which may significantly improve performance. (Contributed
by Ben Hoyt with the help of Victor Stinner in bpo-22524(1).)
On Windows, a new *note stat_result.st_file_attributes: 720. attribute
is now available. It corresponds to the ‘dwFileAttributes’ member of
the ‘BY_HANDLE_FILE_INFORMATION’ structure returned by
‘GetFileInformationByHandle()’. (Contributed by Ben Hoyt in
bpo-21719(2).)
The *note urandom(): 4d1. function now uses the ‘getrandom()’ syscall on
Linux 3.17 or newer, and ‘getentropy()’ on OpenBSD 5.6 and newer,
removing the need to use ‘/dev/urandom’ and avoiding failures due to
potential file descriptor exhaustion. (Contributed by Victor Stinner in
bpo-22181(3).)
New *note get_blocking(): 721. and *note set_blocking(): 722. functions
allow getting and setting a file descriptor’s blocking mode (*note
O_NONBLOCK: 723.) (Contributed by Victor Stinner in bpo-22054(4).)
The *note truncate(): 724. and *note ftruncate(): 662. functions are now
supported on Windows. (Contributed by Steve Dower in bpo-23668(5).)
There is a new *note os.path.commonpath(): 725. function returning the
longest common sub-path of each passed pathname. Unlike the *note
os.path.commonprefix(): 726. function, it always returns a valid path:
>>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
'/usr/l'
>>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
'/usr'
(Contributed by Rafik Draoui and Serhiy Storchaka in bpo-10395(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22524
(2) https://bugs.python.org/issue21719
(3) https://bugs.python.org/issue22181
(4) https://bugs.python.org/issue22054
(5) https://bugs.python.org/issue23668
(6) https://bugs.python.org/issue10395
File: python.info, Node: pathlib<4>, Next: pickle<3>, Prev: os<5>, Up: Improved Modules<4>
1.4.5.44 pathlib
................
The new *note Path.samefile(): 728. method can be used to check whether
the path points to the same file as another path, which can be either
another *note Path: 201. object, or a string:
>>> import pathlib
>>> p1 = pathlib.Path('/etc/hosts')
>>> p2 = pathlib.Path('/etc/../etc/hosts')
>>> p1.samefile(p2)
True
(Contributed by Vajrasky Kok and Antoine Pitrou in bpo-19775(1).)
The *note Path.mkdir(): 729. method now accepts a new optional
`exist_ok' argument to match ‘mkdir -p’ and *note os.makedirs(): 3bd.
functionality. (Contributed by Berker Peksag in bpo-21539(2).)
There is a new *note Path.expanduser(): 72a. method to expand ‘~’ and
‘~user’ prefixes. (Contributed by Serhiy Storchaka and Claudiu Popa in
bpo-19776(3).)
A new *note Path.home(): 72b. class method can be used to get a *note
Path: 201. instance representing the user’s home directory.
(Contributed by Victor Salgado and Mayank Tripathi in bpo-19777(4).)
New *note Path.write_text(): 72c, *note Path.read_text(): 72d, *note
Path.write_bytes(): 72e, *note Path.read_bytes(): 72f. methods to
simplify read/write operations on files.
The following code snippet will create or rewrite existing file
‘~/spam42’:
>>> import pathlib
>>> p = pathlib.Path('~/spam42')
>>> p.expanduser().write_text('ham')
3
(Contributed by Christopher Welborn in bpo-20218(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19775
(2) https://bugs.python.org/issue21539
(3) https://bugs.python.org/issue19776
(4) https://bugs.python.org/issue19777
(5) https://bugs.python.org/issue20218
File: python.info, Node: pickle<3>, Next: poplib, Prev: pathlib<4>, Up: Improved Modules<4>
1.4.5.45 pickle
...............
Nested objects, such as unbound methods or nested classes, can now be
pickled using *note pickle protocols: 56e. older than protocol version
4. Protocol version 4 already supports these cases. (Contributed by
Serhiy Storchaka in bpo-23611(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23611
File: python.info, Node: poplib, Next: re<4>, Prev: pickle<3>, Up: Improved Modules<4>
1.4.5.46 poplib
...............
A new *note POP3.utf8(): 732. command enables RFC 6856(1)
(Internationalized Email) support, if a POP server supports it.
(Contributed by Milan OberKirch in bpo-21804(2).)
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc6856.html
(2) https://bugs.python.org/issue21804
File: python.info, Node: re<4>, Next: readline<2>, Prev: poplib, Up: Improved Modules<4>
1.4.5.47 re
...........
References and conditional references to groups with fixed length are
now allowed in lookbehind assertions:
>>> import re
>>> pat = re.compile(r'(a|b).(?<=\1)c')
>>> pat.match('aac')
<_sre.SRE_Match object; span=(0, 3), match='aac'>
>>> pat.match('bbc')
<_sre.SRE_Match object; span=(0, 3), match='bbc'>
(Contributed by Serhiy Storchaka in bpo-9179(1).)
The number of capturing groups in regular expressions is no longer
limited to 100. (Contributed by Serhiy Storchaka in bpo-22437(2).)
The *note sub(): 47b. and *note subn(): 734. functions now replace
unmatched groups with empty strings instead of raising an exception.
(Contributed by Serhiy Storchaka in bpo-1519638(3).)
The *note re.error: 735. exceptions have new attributes, *note msg: 736,
*note pattern: 737, *note pos: 738, *note lineno: 739, and *note colno:
73a, that provide better context information about the error:
>>> re.compile("""
... (?x)
... .++
... """)
Traceback (most recent call last):
...
sre_constants.error: multiple repeat at position 16 (line 3, column 7)
(Contributed by Serhiy Storchaka in bpo-22578(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9179
(2) https://bugs.python.org/issue22437
(3) https://bugs.python.org/issue1519638
(4) https://bugs.python.org/issue22578
File: python.info, Node: readline<2>, Next: selectors, Prev: re<4>, Up: Improved Modules<4>
1.4.5.48 readline
.................
A new *note append_history_file(): 73c. function can be used to append
the specified number of trailing elements in history to the given file.
(Contributed by Bruno Cauet in bpo-22940(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22940
File: python.info, Node: selectors, Next: shutil<2>, Prev: readline<2>, Up: Improved Modules<4>
1.4.5.49 selectors
..................
The new *note DevpollSelector: 44e. supports efficient ‘/dev/poll’
polling on Solaris. (Contributed by Giampaolo Rodola’ in bpo-18931(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18931
File: python.info, Node: shutil<2>, Next: signal<2>, Prev: selectors, Up: Improved Modules<4>
1.4.5.50 shutil
...............
The *note move(): 25b. function now accepts a `copy_function' argument,
allowing, for example, the *note copy(): 259. function to be used
instead of the default *note copy2(): 25a. if there is a need to ignore
file metadata when moving. (Contributed by Claudiu Popa in
bpo-19840(1).)
The *note make_archive(): 21b. function now supports the `xztar' format.
(Contributed by Serhiy Storchaka in bpo-5411(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19840
(2) https://bugs.python.org/issue5411
File: python.info, Node: signal<2>, Next: smtpd, Prev: shutil<2>, Up: Improved Modules<4>
1.4.5.51 signal
...............
On Windows, the *note set_wakeup_fd(): 3ce. function now also supports
socket handles. (Contributed by Victor Stinner in bpo-22018(1).)
Various ‘SIG*’ constants in the *note signal: ea. module have been
converted into *note Enums: 7b. This allows meaningful names to be
printed during debugging, instead of integer “magic numbers”.
(Contributed by Giampaolo Rodola’ in bpo-21076(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22018
(2) https://bugs.python.org/issue21076
File: python.info, Node: smtpd, Next: smtplib, Prev: signal<2>, Up: Improved Modules<4>
1.4.5.52 smtpd
..............
Both the *note SMTPServer: 602. and *note SMTPChannel: 601. classes now
accept a `decode_data' keyword argument to determine if the ‘DATA’
portion of the SMTP transaction is decoded using the ‘"utf-8"’ codec or
is instead provided to the *note SMTPServer.process_message(): 603.
method as a byte string. The default is ‘True’ for backward
compatibility reasons, but will change to ‘False’ in Python 3.6. If
`decode_data' is set to ‘False’, the ‘process_message’ method must be
prepared to accept keyword arguments. (Contributed by Maciej Szulik in
bpo-19662(1).)
The *note SMTPServer: 602. class now advertises the ‘8BITMIME’ extension
( RFC 6152(2)) if `decode_data' has been set ‘True’. If the client
specifies ‘BODY=8BITMIME’ on the ‘MAIL’ command, it is passed to *note
SMTPServer.process_message(): 603. via the `mail_options' keyword.
(Contributed by Milan Oberkirch and R. David Murray in bpo-21795(3).)
The *note SMTPServer: 602. class now also supports the ‘SMTPUTF8’
extension ( RFC 6531(4): Internationalized Email). If the client
specified ‘SMTPUTF8 BODY=8BITMIME’ on the ‘MAIL’ command, they are
passed to *note SMTPServer.process_message(): 603. via the
`mail_options' keyword. It is the responsibility of the
‘process_message’ method to correctly handle the ‘SMTPUTF8’ data.
(Contributed by Milan Oberkirch in bpo-21725(5).)
It is now possible to provide, directly or via name resolution, IPv6
addresses in the *note SMTPServer: 602. constructor, and have it
successfully connect. (Contributed by Milan Oberkirch in bpo-14758(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19662
(2) https://tools.ietf.org/html/rfc6152.html
(3) https://bugs.python.org/issue21795
(4) https://tools.ietf.org/html/rfc6531.html
(5) https://bugs.python.org/issue21725
(6) https://bugs.python.org/issue14758
File: python.info, Node: smtplib, Next: sndhdr, Prev: smtpd, Up: Improved Modules<4>
1.4.5.53 smtplib
................
A new *note SMTP.auth(): 742. method provides a convenient way to
implement custom authentication mechanisms. (Contributed by Milan
Oberkirch in bpo-15014(1).)
The *note SMTP.set_debuglevel(): 743. method now accepts an additional
debuglevel (2), which enables timestamps in debug messages.
(Contributed by Gavin Chappell and Maciej Szulik in bpo-16914(2).)
Both the *note SMTP.sendmail(): 744. and *note SMTP.send_message(): 745.
methods now support RFC 6531(3) (SMTPUTF8). (Contributed by Milan
Oberkirch and R. David Murray in bpo-22027(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15014
(2) https://bugs.python.org/issue16914
(3) https://tools.ietf.org/html/rfc6531.html
(4) https://bugs.python.org/issue22027
File: python.info, Node: sndhdr, Next: socket<5>, Prev: smtplib, Up: Improved Modules<4>
1.4.5.54 sndhdr
...............
The *note what(): 747. and *note whathdr(): 748. functions now return a
*note namedtuple(): 1b7. (Contributed by Claudiu Popa in bpo-18615(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18615
File: python.info, Node: socket<5>, Next: ssl<6>, Prev: sndhdr, Up: Improved Modules<4>
1.4.5.55 socket
...............
Functions with timeouts now use a monotonic clock, instead of a system
clock. (Contributed by Victor Stinner in bpo-22043(1).)
A new *note socket.sendfile(): 74a. method allows sending a file over a
socket by using the high-performance *note os.sendfile(): 359. function
on UNIX, resulting in uploads being from 2 to 3 times faster than when
using plain *note socket.send(): 67a. (Contributed by Giampaolo Rodola’
in bpo-17552(2).)
The *note socket.sendall(): 67b. method no longer resets the socket
timeout every time bytes are received or sent. The socket timeout is
now the maximum total duration to send all data. (Contributed by Victor
Stinner in bpo-23853(3).)
The `backlog' argument of the *note socket.listen(): 74b. method is now
optional. By default it is set to *note SOMAXCONN: 74c. or to ‘128’,
whichever is less. (Contributed by Charles-François Natali in
bpo-21455(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22043
(2) https://bugs.python.org/issue17552
(3) https://bugs.python.org/issue23853
(4) https://bugs.python.org/issue21455
File: python.info, Node: ssl<6>, Next: sqlite3<3>, Prev: socket<5>, Up: Improved Modules<4>
1.4.5.56 ssl
............
* Menu:
* Memory BIO Support::
* Application-Layer Protocol Negotiation Support::
* Other Changes::
File: python.info, Node: Memory BIO Support, Next: Application-Layer Protocol Negotiation Support, Up: ssl<6>
1.4.5.57 Memory BIO Support
...........................
(Contributed by Geert Jansen in bpo-21965(1).)
The new *note SSLObject: 3e3. class has been added to provide SSL
protocol support for cases when the network I/O capabilities of *note
SSLSocket: 3e2. are not necessary or are suboptimal. ‘SSLObject’
represents an SSL protocol instance, but does not implement any network
I/O methods, and instead provides a memory buffer interface. The new
*note MemoryBIO: 74f. class can be used to pass data between Python and
an SSL protocol instance.
The memory BIO SSL support is primarily intended to be used in
frameworks implementing asynchronous I/O for which *note SSLSocket:
3e2.’s readiness model (“select/poll”) is inefficient.
A new *note SSLContext.wrap_bio(): 3e6. method can be used to create a
new ‘SSLObject’ instance.
---------- Footnotes ----------
(1) https://bugs.python.org/issue21965
File: python.info, Node: Application-Layer Protocol Negotiation Support, Next: Other Changes, Prev: Memory BIO Support, Up: ssl<6>
1.4.5.58 Application-Layer Protocol Negotiation Support
.......................................................
(Contributed by Benjamin Peterson in bpo-20188(1).)
Where OpenSSL support is present, the *note ssl: f3. module now
implements the `Application-Layer Protocol Negotiation' TLS extension as
described in RFC 7301(2).
The new *note SSLContext.set_alpn_protocols(): 751. can be used to
specify which protocols a socket should advertise during the TLS
handshake.
The new *note SSLSocket.selected_alpn_protocol(): 752. returns the
protocol that was selected during the TLS handshake. The *note
HAS_ALPN: 753. flag indicates whether ALPN support is present.
---------- Footnotes ----------
(1) https://bugs.python.org/issue20188
(2) https://tools.ietf.org/html/rfc7301.html
File: python.info, Node: Other Changes, Prev: Application-Layer Protocol Negotiation Support, Up: ssl<6>
1.4.5.59 Other Changes
......................
There is a new *note SSLSocket.version(): 755. method to query the
actual protocol version in use. (Contributed by Antoine Pitrou in
bpo-20421(1).)
The *note SSLSocket: 3e2. class now implements a ‘SSLSocket.sendfile()’
method. (Contributed by Giampaolo Rodola’ in bpo-17552(2).)
The ‘SSLSocket.send()’ method now raises either the *note
ssl.SSLWantReadError: 756. or *note ssl.SSLWantWriteError: 757.
exception on a non-blocking socket if the operation would block.
Previously, it would return ‘0’. (Contributed by Nikolaus Rath in
bpo-20951(3).)
The *note cert_time_to_seconds(): 758. function now interprets the input
time as UTC and not as local time, per RFC 5280(4). Additionally, the
return value is always an *note int: 184. (Contributed by Akira Li in
bpo-19940(5).)
New ‘SSLObject.shared_ciphers()’ and *note SSLSocket.shared_ciphers():
759. methods return the list of ciphers sent by the client during the
handshake. (Contributed by Benjamin Peterson in bpo-23186(6).)
The *note SSLSocket.do_handshake(): 75a, *note SSLSocket.read(): 75b,
‘SSLSocket.shutdown()’, and *note SSLSocket.write(): 75c. methods of the
*note SSLSocket: 3e2. class no longer reset the socket timeout every
time bytes are received or sent. The socket timeout is now the maximum
total duration of the method. (Contributed by Victor Stinner in
bpo-23853(7).)
The *note match_hostname(): 3dc. function now supports matching of IP
addresses. (Contributed by Antoine Pitrou in bpo-23239(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20421
(2) https://bugs.python.org/issue17552
(3) https://bugs.python.org/issue20951
(4) https://tools.ietf.org/html/rfc5280.html
(5) https://bugs.python.org/issue19940
(6) https://bugs.python.org/issue23186
(7) https://bugs.python.org/issue23853
(8) https://bugs.python.org/issue23239
File: python.info, Node: sqlite3<3>, Next: subprocess<3>, Prev: ssl<6>, Up: Improved Modules<4>
1.4.5.60 sqlite3
................
The *note Row: 75e. class now fully supports the sequence protocol, in
particular *note reversed(): 18e. iteration and slice indexing.
(Contributed by Claudiu Popa in bpo-10203(1); by Lucas Sinclair, Jessica
McKellar, and Serhiy Storchaka in bpo-13583(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10203
(2) https://bugs.python.org/issue13583
File: python.info, Node: subprocess<3>, Next: sys<5>, Prev: sqlite3<3>, Up: Improved Modules<4>
1.4.5.61 subprocess
...................
The new *note run(): 3ed. function has been added. It runs the
specified command and returns a *note CompletedProcess: 760. object,
which describes a finished process. The new API is more consistent and
is the recommended approach to invoking subprocesses in Python code that
does not need to maintain compatibility with earlier Python versions.
(Contributed by Thomas Kluyver in bpo-23342(1).)
Examples:
>>> subprocess.run(["ls", "-l"]) # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')
---------- Footnotes ----------
(1) https://bugs.python.org/issue23342
File: python.info, Node: sys<5>, Next: sysconfig, Prev: subprocess<3>, Up: Improved Modules<4>
1.4.5.62 sys
............
A new ‘set_coroutine_wrapper()’ function allows setting a global hook
that will be called whenever a *note coroutine object: 1a6. is created
by an *note async def: 284. function. A corresponding
‘get_coroutine_wrapper()’ can be used to obtain a currently set wrapper.
Both functions are *note provisional: 348, and are intended for
debugging purposes only. (Contributed by Yury Selivanov in
bpo-24017(1).)
A new *note is_finalizing(): 762. function can be used to check if the
Python interpreter is *note shutting down: 763. (Contributed by Antoine
Pitrou in bpo-22696(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24017
(2) https://bugs.python.org/issue22696
File: python.info, Node: sysconfig, Next: tarfile<2>, Prev: sys<5>, Up: Improved Modules<4>
1.4.5.63 sysconfig
..................
The name of the user scripts directory on Windows now includes the first
two components of the Python version. (Contributed by Paul Moore in
bpo-23437(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23437
File: python.info, Node: tarfile<2>, Next: threading<3>, Prev: sysconfig, Up: Improved Modules<4>
1.4.5.64 tarfile
................
The `mode' argument of the *note open(): 766. function now accepts ‘"x"’
to request exclusive creation. (Contributed by Berker Peksag in
bpo-21717(1).)
The *note TarFile.extractall(): 767. and *note TarFile.extract(): 768.
methods now take a keyword argument `numeric_owner'. If set to ‘True’,
the extracted files and directories will be owned by the numeric ‘uid’
and ‘gid’ from the tarfile. If set to ‘False’ (the default, and the
behavior in versions prior to 3.5), they will be owned by the named user
and group in the tarfile. (Contributed by Michael Vogt and Eric Smith
in bpo-23193(2).)
The *note TarFile.list(): 769. now accepts an optional `members' keyword
argument that can be set to a subset of the list returned by *note
TarFile.getmembers(): 76a. (Contributed by Serhiy Storchaka in
bpo-21549(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21717
(2) https://bugs.python.org/issue23193
(3) https://bugs.python.org/issue21549
File: python.info, Node: threading<3>, Next: time<4>, Prev: tarfile<2>, Up: Improved Modules<4>
1.4.5.65 threading
..................
Both the *note Lock.acquire(): 76c. and *note RLock.acquire(): 76d.
methods now use a monotonic clock for timeout management. (Contributed
by Victor Stinner in bpo-22043(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22043
File: python.info, Node: time<4>, Next: timeit<2>, Prev: threading<3>, Up: Improved Modules<4>
1.4.5.66 time
.............
The *note monotonic(): 76f. function is now always available.
(Contributed by Victor Stinner in bpo-22043(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue22043
File: python.info, Node: timeit<2>, Next: tkinter<5>, Prev: time<4>, Up: Improved Modules<4>
1.4.5.67 timeit
...............
A new command line option ‘-u’ or ‘--unit=`U'’ can be used to specify
the time unit for the timer output. Supported options are ‘usec’,
‘msec’, or ‘sec’. (Contributed by Julian Gindi in bpo-18983(1).)
The *note timeit(): 771. function has a new `globals' parameter for
specifying the namespace in which the code will be running.
(Contributed by Ben Roberts in bpo-2527(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18983
(2) https://bugs.python.org/issue2527
File: python.info, Node: tkinter<5>, Next: traceback<2>, Prev: timeit<2>, Up: Improved Modules<4>
1.4.5.68 tkinter
................
The ‘tkinter._fix’ module used for setting up the Tcl/Tk environment on
Windows has been replaced by a private function in the ‘_tkinter’ module
which makes no permanent changes to environment variables. (Contributed
by Zachary Ware in bpo-20035(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20035
File: python.info, Node: traceback<2>, Next: types<2>, Prev: tkinter<5>, Up: Improved Modules<4>
1.4.5.69 traceback
..................
New *note walk_stack(): 774. and *note walk_tb(): 775. functions to
conveniently traverse frame and traceback objects. (Contributed by
Robert Collins in bpo-17911(1).)
New lightweight classes: *note TracebackException: 776, *note
StackSummary: 777, and *note FrameSummary: 778. (Contributed by Robert
Collins in bpo-17911(2).)
Both the *note print_tb(): 779. and *note print_stack(): 77a. functions
now support negative values for the `limit' argument. (Contributed by
Dmitry Kazakov in bpo-22619(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17911
(2) https://bugs.python.org/issue17911
(3) https://bugs.python.org/issue22619
File: python.info, Node: types<2>, Next: unicodedata<4>, Prev: traceback<2>, Up: Improved Modules<4>
1.4.5.70 types
..............
A new *note coroutine(): 77c. function to transform *note generator:
457. and *note generator-like: 6b5. objects into *note awaitables: 519.
(Contributed by Yury Selivanov in bpo-24017(1).)
A new type called *note CoroutineType: 77d, which is used for *note
coroutine: 1a6. objects created by *note async def: 284. functions.
(Contributed by Yury Selivanov in bpo-24400(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue24017
(2) https://bugs.python.org/issue24400
File: python.info, Node: unicodedata<4>, Next: unittest<3>, Prev: types<2>, Up: Improved Modules<4>
1.4.5.71 unicodedata
....................
The *note unicodedata: 11a. module now uses data from Unicode 8.0.0(1).
---------- Footnotes ----------
(1) http://unicode.org/versions/Unicode8.0.0/
File: python.info, Node: unittest<3>, Next: unittest mock<3>, Prev: unicodedata<4>, Up: Improved Modules<4>
1.4.5.72 unittest
.................
The *note TestLoader.loadTestsFromModule(): 780. method now accepts a
keyword-only argument `pattern' which is passed to ‘load_tests’ as the
third argument. Found packages are now checked for ‘load_tests’
regardless of whether their path matches `pattern', because it is
impossible for a package name to match the default pattern.
(Contributed by Robert Collins and Barry A. Warsaw in bpo-16662(1).)
Unittest discovery errors now are exposed in the *note
TestLoader.errors: 781. attribute of the *note TestLoader: 782.
instance. (Contributed by Robert Collins in bpo-19746(2).)
A new command line option ‘--locals’ to show local variables in
tracebacks. (Contributed by Robert Collins in bpo-22936(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16662
(2) https://bugs.python.org/issue19746
(3) https://bugs.python.org/issue22936
File: python.info, Node: unittest mock<3>, Next: urllib, Prev: unittest<3>, Up: Improved Modules<4>
1.4.5.73 unittest.mock
......................
The *note Mock: 249. class has the following improvements:
* The class constructor has a new `unsafe' parameter, which causes
mock objects to raise *note AttributeError: 39b. on attribute names
starting with ‘"assert"’. (Contributed by Kushal Das in
bpo-21238(1).)
* A new *note Mock.assert_not_called(): 784. method to check if the
mock object was called. (Contributed by Kushal Das in
bpo-21262(2).)
The *note MagicMock: 785. class now supports *note __truediv__(): 786,
*note __divmod__(): 787. and *note __matmul__(): 648. operators.
(Contributed by Johannes Baiter in bpo-20968(3), and Håkan Lövdahl in
bpo-23581(4) and bpo-23568(5).)
It is no longer necessary to explicitly pass ‘create=True’ to the *note
patch(): 788. function when patching builtin names. (Contributed by
Kushal Das in bpo-17660(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue21238
(2) https://bugs.python.org/issue21262
(3) https://bugs.python.org/issue20968
(4) https://bugs.python.org/issue23581
(5) https://bugs.python.org/issue23568
(6) https://bugs.python.org/issue17660
File: python.info, Node: urllib, Next: wsgiref, Prev: unittest mock<3>, Up: Improved Modules<4>
1.4.5.74 urllib
...............
A new *note request.HTTPPasswordMgrWithPriorAuth: 78a. class allows HTTP
Basic Authentication credentials to be managed so as to eliminate
unnecessary ‘401’ response handling, or to unconditionally send
credentials on the first request in order to communicate with servers
that return a ‘404’ response instead of a ‘401’ if the ‘Authorization’
header is not sent. (Contributed by Matej Cepl in bpo-19494(1) and
Akshit Khurana in bpo-7159(2).)
A new `quote_via' argument for the *note parse.urlencode(): 78b.
function provides a way to control the encoding of query parts if
needed. (Contributed by Samwyse and Arnon Yaari in bpo-13866(3).)
The *note request.urlopen(): 78c. function accepts an *note
ssl.SSLContext: 3e4. object as a `context' argument, which will be used
for the HTTPS connection. (Contributed by Alex Gaynor in bpo-22366(4).)
The *note parse.urljoin(): 78d. was updated to use the RFC 3986(5)
semantics for the resolution of relative URLs, rather than RFC 1808(6)
and RFC 2396(7). (Contributed by Demian Brecht and Senthil Kumaran in
bpo-22118(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19494
(2) https://bugs.python.org/issue7159
(3) https://bugs.python.org/issue13866
(4) https://bugs.python.org/issue22366
(5) https://tools.ietf.org/html/rfc3986.html
(6) https://tools.ietf.org/html/rfc1808.html
(7) https://tools.ietf.org/html/rfc2396.html
(8) https://bugs.python.org/issue22118
File: python.info, Node: wsgiref, Next: xmlrpc<2>, Prev: urllib, Up: Improved Modules<4>
1.4.5.75 wsgiref
................
The `headers' argument of the *note headers.Headers: 78f. class
constructor is now optional. (Contributed by Pablo Torres Navarrete and
SilentGhost in bpo-5800(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5800
File: python.info, Node: xmlrpc<2>, Next: xml sax, Prev: wsgiref, Up: Improved Modules<4>
1.4.5.76 xmlrpc
...............
The *note client.ServerProxy: 255. class now supports the *note context
manager: 568. protocol. (Contributed by Claudiu Popa in bpo-20627(1).)
The *note client.ServerProxy: 255. constructor now accepts an optional
*note ssl.SSLContext: 3e4. instance. (Contributed by Alex Gaynor in
bpo-22960(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue20627
(2) https://bugs.python.org/issue22960
File: python.info, Node: xml sax, Next: zipfile<3>, Prev: xmlrpc<2>, Up: Improved Modules<4>
1.4.5.77 xml.sax
................
SAX parsers now support a character stream of the *note
xmlreader.InputSource: 792. object. (Contributed by Serhiy Storchaka in
bpo-2175(1).)
*note parseString(): 793. now accepts a *note str: 330. instance.
(Contributed by Serhiy Storchaka in bpo-10590(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue2175
(2) https://bugs.python.org/issue10590
File: python.info, Node: zipfile<3>, Prev: xml sax, Up: Improved Modules<4>
1.4.5.78 zipfile
................
ZIP output can now be written to unseekable streams. (Contributed by
Serhiy Storchaka in bpo-23252(1).)
The `mode' argument of *note ZipFile.open(): 5bc. method now accepts
‘"x"’ to request exclusive creation. (Contributed by Serhiy Storchaka
in bpo-21717(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23252
(2) https://bugs.python.org/issue21717
File: python.info, Node: Other module-level changes, Next: Optimizations<4>, Prev: Improved Modules<4>, Up: What’s New In Python 3 5
1.4.6 Other module-level changes
--------------------------------
Many functions in the *note mmap: b3, *note ossaudiodev: c6, *note
socket: ef, *note ssl: f3, and *note codecs: 1c. modules now accept
writable *note bytes-like objects: 5e8. (Contributed by Serhiy
Storchaka in bpo-23001(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23001
File: python.info, Node: Optimizations<4>, Next: Build and C API Changes<3>, Prev: Other module-level changes, Up: What’s New In Python 3 5
1.4.7 Optimizations
-------------------
The *note os.walk(): 654. function has been sped up by 3 to 5 times on
POSIX systems, and by 7 to 20 times on Windows. This was done using the
new *note os.scandir(): 25f. function, which exposes file information
from the underlying ‘readdir’ or ‘FindFirstFile’/‘FindNextFile’ system
calls. (Contributed by Ben Hoyt with help from Victor Stinner in
bpo-23605(1).)
Construction of ‘bytes(int)’ (filled by zero bytes) is faster and uses
less memory for large objects. ‘calloc()’ is used instead of ‘malloc()’
to allocate memory for these objects. (Contributed by Victor Stinner in
bpo-21233(2).)
Some operations on *note ipaddress: a2. *note IPv4Network: 3a0. and
*note IPv6Network: 39f. have been massively sped up, such as *note
subnets(): 797, *note supernet(): 798, *note summarize_address_range():
799, *note collapse_addresses(): 79a. The speed up can range from 3 to
15 times. (Contributed by Antoine Pitrou, Michel Albert, and Markus in
bpo-21486(3), bpo-21487(4), bpo-20826(5), bpo-23266(6).)
Pickling of *note ipaddress: a2. objects was optimized to produce
significantly smaller output. (Contributed by Serhiy Storchaka in
bpo-23133(7).)
Many operations on *note io.BytesIO: 79b. are now 50% to 100% faster.
(Contributed by Serhiy Storchaka in bpo-15381(8) and David Wilson in
bpo-22003(9).)
The *note marshal.dumps(): 79c. function is now faster: 65–85% with
versions 3 and 4, 20–25% with versions 0 to 2 on typical data, and up to
5 times in best cases. (Contributed by Serhiy Storchaka in
bpo-20416(10) and bpo-23344(11).)
The UTF-32 encoder is now 3 to 7 times faster. (Contributed by Serhiy
Storchaka in bpo-15027(12).)
Regular expressions are now parsed up to 10% faster. (Contributed by
Serhiy Storchaka in bpo-19380(13).)
The *note json.dumps(): 605. function was optimized to run with
‘ensure_ascii=False’ as fast as with ‘ensure_ascii=True’. (Contributed
by Naoki Inada in bpo-23206(14).)
The *note PyObject_IsInstance(): 79d. and *note PyObject_IsSubclass():
79e. functions have been sped up in the common case that the second
argument has *note type: 608. as its metaclass. (Contributed Georg
Brandl by in bpo-22540(15).)
Method caching was slightly improved, yielding up to 5% performance
improvement in some benchmarks. (Contributed by Antoine Pitrou in
bpo-22847(16).)
Objects from the *note random: dc. module now use 50% less memory on
64-bit builds. (Contributed by Serhiy Storchaka in bpo-23488(17).)
The *note property(): 1d7. getter calls are up to 25% faster.
(Contributed by Joe Jevnik in bpo-23910(18).)
Instantiation of *note fractions.Fraction: 185. is now up to 30% faster.
(Contributed by Stefan Behnel in bpo-22464(19).)
String methods *note find(): 79f, *note rfind(): 7a0, *note split():
7a1, *note partition(): 7a2. and the *note in: 7a3. string operator are
now significantly faster for searching 1-character substrings.
(Contributed by Serhiy Storchaka in bpo-23573(20).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23605
(2) https://bugs.python.org/issue21233
(3) https://bugs.python.org/issue21486
(4) https://bugs.python.org/issue21487
(5) https://bugs.python.org/issue20826
(6) https://bugs.python.org/issue23266
(7) https://bugs.python.org/issue23133
(8) https://bugs.python.org/issue15381
(9) https://bugs.python.org/issue22003
(10) https://bugs.python.org/issue20416
(11) https://bugs.python.org/issue23344
(12) https://bugs.python.org/issue15027
(13) https://bugs.python.org/issue19380
(14) https://bugs.python.org/issue23206
(15) https://bugs.python.org/issue22540
(16) https://bugs.python.org/issue22847
(17) https://bugs.python.org/issue23488
(18) https://bugs.python.org/issue23910
(19) https://bugs.python.org/issue22464
(20) https://bugs.python.org/issue23573
File: python.info, Node: Build and C API Changes<3>, Next: Deprecated<3>, Prev: Optimizations<4>, Up: What’s New In Python 3 5
1.4.8 Build and C API Changes
-----------------------------
New ‘calloc’ functions were added:
* *note PyMem_RawCalloc(): 7a5,
* *note PyMem_Calloc(): 7a6,
* *note PyObject_Calloc(): 7a7.
(Contributed by Victor Stinner in bpo-21233(1).)
New encoding/decoding helper functions:
* *note Py_DecodeLocale(): 43c. (replaced ‘_Py_char2wchar()’),
* *note Py_EncodeLocale(): 43d. (replaced ‘_Py_wchar2char()’).
(Contributed by Victor Stinner in bpo-18395(2).)
A new *note PyCodec_NameReplaceErrors(): 7a8. function to replace the
unicode encode error with ‘\N{...}’ escapes. (Contributed by Serhiy
Storchaka in bpo-19676(3).)
A new *note PyErr_FormatV(): 7a9. function similar to *note
PyErr_Format(): 7aa, but accepts a ‘va_list’ argument. (Contributed by
Antoine Pitrou in bpo-18711(4).)
A new ‘PyExc_RecursionError’ exception. (Contributed by Georg Brandl in
bpo-19235(5).)
New *note PyModule_FromDefAndSpec(): 7ab, *note
PyModule_FromDefAndSpec2(): 7ac, and *note PyModule_ExecDef(): 7ad.
functions introduced by PEP 489(6) – multi-phase extension module
initialization. (Contributed by Petr Viktorin in bpo-24268(7).)
New *note PyNumber_MatrixMultiply(): 7ae. and *note
PyNumber_InPlaceMatrixMultiply(): 7af. functions to perform matrix
multiplication. (Contributed by Benjamin Peterson in bpo-21176(8). See
also PEP 465(9) for details.)
The *note PyTypeObject.tp_finalize: 2d7. slot is now part of the stable
ABI.
Windows builds now require Microsoft Visual C++ 14.0, which is available
as part of Visual Studio 2015(10).
Extension modules now include a platform information tag in their
filename on some platforms (the tag is optional, and CPython will import
extensions without it, although if the tag is present and mismatched,
the extension won’t be loaded):
* On Linux, extension module filenames end with
‘.cpython-m--.pyd’:
* ‘’ is the major number of the Python version; for
Python 3.5 this is ‘3’.
* ‘’ is the minor number of the Python version; for
Python 3.5 this is ‘5’.
* ‘’ is the hardware architecture the extension
module was built to run on. It’s most commonly either ‘i386’
for 32-bit Intel platforms or ‘x86_64’ for 64-bit Intel (and
AMD) platforms.
* ‘’ is always ‘linux-gnu’, except for extensions built to
talk to the 32-bit ABI on 64-bit platforms, in which case it
is ‘linux-gnu32’ (and ‘’ will be ‘x86_64’).
* On Windows, extension module filenames end with
‘.cp-.pyd’:
* ‘’ is the major number of the Python version; for
Python 3.5 this is ‘3’.
* ‘’ is the minor number of the Python version; for
Python 3.5 this is ‘5’.
* ‘’ is the platform the extension module was built
for, either ‘win32’ for Win32, ‘win_amd64’ for Win64,
‘win_ia64’ for Windows Itanium 64, and ‘win_arm’ for Windows
on ARM.
* If built in debug mode, ‘’ will be ‘_d’, otherwise it
will be blank.
* On OS X platforms, extension module filenames now end with
‘-darwin.so’.
* On all other platforms, extension module filenames are the same as
they were with Python 3.4.
---------- Footnotes ----------
(1) https://bugs.python.org/issue21233
(2) https://bugs.python.org/issue18395
(3) https://bugs.python.org/issue19676
(4) https://bugs.python.org/issue18711
(5) https://bugs.python.org/issue19235
(6) https://www.python.org/dev/peps/pep-0489
(7) https://bugs.python.org/issue24268
(8) https://bugs.python.org/issue21176
(9) https://www.python.org/dev/peps/pep-0465
(10) https://www.visualstudio.com/
File: python.info, Node: Deprecated<3>, Next: Removed<2>, Prev: Build and C API Changes<3>, Up: What’s New In Python 3 5
1.4.9 Deprecated
----------------
* Menu:
* New Keywords: New Keywords<2>.
* Deprecated Python Behavior: Deprecated Python Behavior<2>.
* Unsupported Operating Systems::
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods<3>.
File: python.info, Node: New Keywords<2>, Next: Deprecated Python Behavior<2>, Up: Deprecated<3>
1.4.9.1 New Keywords
....................
‘async’ and ‘await’ are not recommended to be used as variable, class,
function or module names. Introduced by PEP 492(1) in Python 3.5, they
will become proper keywords in Python 3.7.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0492
File: python.info, Node: Deprecated Python Behavior<2>, Next: Unsupported Operating Systems, Prev: New Keywords<2>, Up: Deprecated<3>
1.4.9.2 Deprecated Python Behavior
..................................
Raising the *note StopIteration: 486. exception inside a generator will
now generate a silent *note PendingDeprecationWarning: 279, which will
become a non-silent deprecation warning in Python 3.6 and will trigger a
*note RuntimeError: 2ba. in Python 3.7. See *note PEP 479; Change
StopIteration handling inside generators: 5d7. for details.
File: python.info, Node: Unsupported Operating Systems, Next: Deprecated Python modules functions and methods<3>, Prev: Deprecated Python Behavior<2>, Up: Deprecated<3>
1.4.9.3 Unsupported Operating Systems
.....................................
Windows XP is no longer supported by Microsoft, thus, per PEP 11(1),
CPython 3.5 is no longer officially supported on this OS.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0011
File: python.info, Node: Deprecated Python modules functions and methods<3>, Prev: Unsupported Operating Systems, Up: Deprecated<3>
1.4.9.4 Deprecated Python modules, functions and methods
........................................................
The *note formatter: 82. module has now graduated to full deprecation
and is still slated for removal in Python 3.6.
The ‘asyncio.async()’ function is deprecated in favor of *note
ensure_future(): 517.
The *note smtpd: ec. module has in the past always decoded the DATA
portion of email messages using the ‘utf-8’ codec. This can now be
controlled by the new `decode_data' keyword to *note SMTPServer: 602.
The default value is ‘True’, but this default is deprecated. Specify
the `decode_data' keyword with an appropriate value to avoid the
deprecation warning.
Directly assigning values to the *note key: 48d, *note value: 48e. and
*note coded_value: 48f. of *note http.cookies.Morsel: 490. objects is
deprecated. Use the *note set(): 491. method instead. In addition, the
undocumented `LegalChars' parameter of *note set(): 491. is deprecated,
and is now ignored.
Passing a format string as keyword argument `format_string' to the *note
format(): 48c. method of the *note string.Formatter: 7b5. class has been
deprecated. (Contributed by Serhiy Storchaka in bpo-23671(1).)
The ‘platform.dist()’ and ‘platform.linux_distribution()’ functions are
now deprecated. Linux distributions use too many different ways of
describing themselves, so the functionality is left to a package.
(Contributed by Vajrasky Kok and Berker Peksag in bpo-1322(2).)
The previously undocumented ‘from_function’ and ‘from_builtin’ methods
of *note inspect.Signature: 6f3. are deprecated. Use the new *note
Signature.from_callable(): 6f6. method instead. (Contributed by Yury
Selivanov in bpo-24248(3).)
The *note inspect.getargspec(): 55c. function is deprecated and
scheduled to be removed in Python 3.6. (See bpo-20438(4) for details.)
The *note inspect: a0. *note getfullargspec(): 55d, *note getcallargs():
7b6, and *note formatargspec(): 7b7. functions are deprecated in favor
of the *note inspect.signature(): 55b. API. (Contributed by Yury
Selivanov in bpo-20438(5).)
*note getargvalues(): 7b8. and *note formatargvalues(): 7b9. functions
were inadvertently marked as deprecated with the release of Python
3.5.0.
Use of *note re.LOCALE: 3c9. flag with str patterns or *note re.ASCII:
3c8. is now deprecated. (Contributed by Serhiy Storchaka in
bpo-22407(6).)
Use of unrecognized special sequences consisting of ‘'\'’ and an ASCII
letter in regular expression patterns and replacement patterns now
raises a deprecation warning and will be forbidden in Python 3.6.
(Contributed by Serhiy Storchaka in bpo-23622(7).)
The undocumented and unofficial `use_load_tests' default argument of the
*note unittest.TestLoader.loadTestsFromModule(): 780. method now is
deprecated and ignored. (Contributed by Robert Collins and Barry A.
Warsaw in bpo-16662(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue23671
(2) https://bugs.python.org/issue1322
(3) https://bugs.python.org/issue24248
(4) https://bugs.python.org/issue20438
(5) https://bugs.python.org/issue20438
(6) https://bugs.python.org/issue22407
(7) https://bugs.python.org/issue23622
(8) https://bugs.python.org/issue16662
File: python.info, Node: Removed<2>, Next: Porting to Python 3 5, Prev: Deprecated<3>, Up: What’s New In Python 3 5
1.4.10 Removed
--------------
* Menu:
* API and Feature Removals: API and Feature Removals<4>.
File: python.info, Node: API and Feature Removals<4>, Up: Removed<2>
1.4.10.1 API and Feature Removals
.................................
The following obsolete and previously deprecated APIs and features have
been removed:
* The ‘__version__’ attribute has been dropped from the email
package. The email code hasn’t been shipped separately from the
stdlib for a long time, and the ‘__version__’ string was not
updated in the last few releases.
* The internal ‘Netrc’ class in the *note ftplib: 84. module was
deprecated in 3.4, and has now been removed. (Contributed by Matt
Chaput in bpo-6623(1).)
* The concept of ‘.pyo’ files has been removed.
* The JoinableQueue class in the provisional *note asyncio: a. module
was deprecated in 3.4.4 and is now removed. (Contributed by A.
Jesse Jiryu Davis in bpo-23464(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6623
(2) https://bugs.python.org/issue23464
File: python.info, Node: Porting to Python 3 5, Next: Notable changes in Python 3 5 4, Prev: Removed<2>, Up: What’s New In Python 3 5
1.4.11 Porting to Python 3.5
----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code.
* Menu:
* Changes in Python behavior: Changes in Python behavior<2>.
* Changes in the Python API: Changes in the Python API<4>.
* Changes in the C API: Changes in the C API<4>.
File: python.info, Node: Changes in Python behavior<2>, Next: Changes in the Python API<4>, Up: Porting to Python 3 5
1.4.11.1 Changes in Python behavior
...................................
* Due to an oversight, earlier Python versions erroneously accepted
the following syntax:
f(1 for x in [1], *args)
f(1 for x in [1], **kwargs)
Python 3.5 now correctly raises a *note SyntaxError: 458, as
generator expressions must be put in parentheses if not a sole
argument to a function.
File: python.info, Node: Changes in the Python API<4>, Next: Changes in the C API<4>, Prev: Changes in Python behavior<2>, Up: Porting to Python 3 5
1.4.11.2 Changes in the Python API
..................................
* PEP 475(1): System calls are now retried when interrupted by a
signal instead of raising *note InterruptedError: 659. if the
Python signal handler does not raise an exception.
* Before Python 3.5, a *note datetime.time: 4f3. object was
considered to be false if it represented midnight in UTC. This
behavior was considered obscure and error-prone and has been
removed in Python 3.5. See bpo-13936(2) for full details.
* The ‘ssl.SSLSocket.send()’ method now raises either *note
ssl.SSLWantReadError: 756. or *note ssl.SSLWantWriteError: 757. on
a non-blocking socket if the operation would block. Previously, it
would return ‘0’. (Contributed by Nikolaus Rath in bpo-20951(3).)
* The ‘__name__’ attribute of generators is now set from the function
name, instead of being set from the code name. Use
‘gen.gi_code.co_name’ to retrieve the code name. Generators also
have a new ‘__qualname__’ attribute, the qualified name, which is
now used for the representation of a generator (‘repr(gen)’).
(Contributed by Victor Stinner in bpo-21205(4).)
* The deprecated “strict” mode and argument of *note HTMLParser: 7bf,
‘HTMLParser.error()’, and the ‘HTMLParserError’ exception have been
removed. (Contributed by Ezio Melotti in bpo-15114(5).) The
`convert_charrefs' argument of *note HTMLParser: 7bf. is now ‘True’
by default. (Contributed by Berker Peksag in bpo-21047(6).)
* Although it is not formally part of the API, it is worth noting for
porting purposes (ie: fixing tests) that error messages that were
previously of the form “‘sometype’ does not support the buffer
protocol” are now of the form “a *note bytes-like object: 5e8. is
required, not ‘sometype’”. (Contributed by Ezio Melotti in
bpo-16518(7).)
* If the current directory is set to a directory that no longer
exists then *note FileNotFoundError: 7c0. will no longer be raised
and instead *note find_spec(): 7c1. will return ‘None’ `without'
caching ‘None’ in *note sys.path_importer_cache: 49d, which is
different than the typical case (bpo-22834(8)).
* HTTP status code and messages from *note http.client: 94. and *note
http.server: 97. were refactored into a common *note HTTPStatus:
6e2. enum. The values in *note http.client: 94. and *note
http.server: 97. remain available for backwards compatibility.
(Contributed by Demian Brecht in bpo-21793(9).)
* When an import loader defines
‘importlib.machinery.Loader.exec_module()’ it is now expected to
also define ‘create_module()’ (raises a *note DeprecationWarning:
278. now, will be an error in Python 3.6). If the loader inherits
from *note importlib.abc.Loader: 7c2. then there is nothing to do,
else simply define ‘create_module()’ to return ‘None’.
(Contributed by Brett Cannon in bpo-23014(10).)
* The *note re.split(): 3ca. function always ignored empty pattern
matches, so the ‘"x*"’ pattern worked the same as ‘"x+"’, and the
‘"\b"’ pattern never worked. Now *note re.split(): 3ca. raises a
warning if the pattern could match an empty string. For
compatibility, use patterns that never match an empty string (e.g.
‘"x+"’ instead of ‘"x*"’). Patterns that could only match an empty
string (such as ‘"\b"’) now raise an error. (Contributed by Serhiy
Storchaka in bpo-22818(11).)
* The *note http.cookies.Morsel: 490. dict-like interface has been
made self consistent: morsel comparison now takes the *note key:
48d. and *note value: 48e. into account, *note copy(): 7c3. now
results in a *note Morsel: 490. instance rather than a *note dict:
1b8, and *note update(): 7c4. will now raise an exception if any of
the keys in the update dictionary are invalid. In addition, the
undocumented `LegalChars' parameter of *note set(): 491. is
deprecated and is now ignored. (Contributed by Demian Brecht in
bpo-2211(12).)
* PEP 488(13) has removed ‘.pyo’ files from Python and introduced the
optional ‘opt-’ tag in ‘.pyc’ file names. The *note
importlib.util.cache_from_source(): 557. has gained an
`optimization' parameter to help control the ‘opt-’ tag. Because
of this, the `debug_override' parameter of the function is now
deprecated. ‘.pyo’ files are also no longer supported as a file
argument to the Python interpreter and thus serve no purpose when
distributed on their own (i.e. sourceless code distribution). Due
to the fact that the magic number for bytecode has changed in
Python 3.5, all old ‘.pyo’ files from previous versions of Python
are invalid regardless of this PEP.
* The *note socket: ef. module now exports the *note
CAN_RAW_FD_FRAMES: 7c5. constant on linux 3.6 and greater.
* The *note ssl.cert_time_to_seconds(): 758. function now interprets
the input time as UTC and not as local time, per RFC 5280(14).
Additionally, the return value is always an *note int: 184.
(Contributed by Akira Li in bpo-19940(15).)
* The ‘pygettext.py’ Tool now uses the standard +NNNN format for
timezones in the POT-Creation-Date header.
* The *note smtplib: ed. module now uses *note sys.stderr: 30f.
instead of the previous module-level ‘stderr’ variable for debug
output. If your (test) program depends on patching the
module-level variable to capture the debug output, you will need to
update it to capture sys.stderr instead.
* The *note str.startswith(): 7c6. and *note str.endswith(): 7c7.
methods no longer return ‘True’ when finding the empty string and
the indexes are completely out of range. (Contributed by Serhiy
Storchaka in bpo-24284(16).)
* The *note inspect.getdoc(): 1d6. function now returns documentation
strings inherited from base classes. Documentation strings no
longer need to be duplicated if the inherited documentation is
appropriate. To suppress an inherited string, an empty string must
be specified (or the documentation may be filled in). This change
affects the output of the *note pydoc: d9. module and the *note
help(): 572. function. (Contributed by Serhiy Storchaka in
bpo-15582(17).)
* Nested *note functools.partial(): 7c8. calls are now flattened. If
you were relying on the previous behavior, you can now either add
an attribute to a *note functools.partial(): 7c8. object or you can
create a subclass of *note functools.partial(): 7c8. (Contributed
by Alexander Belopolsky in bpo-7830(18).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0475
(2) https://bugs.python.org/issue13936
(3) https://bugs.python.org/issue20951
(4) https://bugs.python.org/issue21205
(5) https://bugs.python.org/issue15114
(6) https://bugs.python.org/issue21047
(7) https://bugs.python.org/issue16518
(8) https://bugs.python.org/issue22834
(9) https://bugs.python.org/issue21793
(10) https://bugs.python.org/issue23014
(11) https://bugs.python.org/issue22818
(12) https://bugs.python.org/issue2211
(13) https://www.python.org/dev/peps/pep-0488
(14) https://tools.ietf.org/html/rfc5280.html
(15) https://bugs.python.org/issue19940
(16) https://bugs.python.org/issue24284
(17) https://bugs.python.org/issue15582
(18) https://bugs.python.org/issue7830
File: python.info, Node: Changes in the C API<4>, Prev: Changes in the Python API<4>, Up: Porting to Python 3 5
1.4.11.3 Changes in the C API
.............................
* The undocumented ‘format’ member of the (non-public)
‘PyMemoryViewObject’ structure has been removed. All extensions
relying on the relevant parts in ‘memoryobject.h’ must be rebuilt.
* The ‘PyMemAllocator’ structure was renamed to *note
PyMemAllocatorEx: 7ca. and a new ‘calloc’ field was added.
* Removed non-documented macro ‘PyObject_REPR’ which leaked
references. Use format character ‘%R’ in *note
PyUnicode_FromFormat(): 7cb.-like functions to format the *note
repr(): 7cc. of the object. (Contributed by Serhiy Storchaka in
bpo-22453(1).)
* Because the lack of the ‘__module__’ attribute breaks pickling and
introspection, a deprecation warning is now raised for builtin
types without the ‘__module__’ attribute. This would be an
AttributeError in the future. (Contributed by Serhiy Storchaka in
bpo-20204(2).)
* As part of the PEP 492(3) implementation, the ‘tp_reserved’ slot of
*note PyTypeObject: 2d6. was replaced with a ‘tp_as_async’ slot.
Refer to *note Coroutine Objects: 7cd. for new types, structures
and functions.
---------- Footnotes ----------
(1) https://bugs.python.org/issue22453
(2) https://bugs.python.org/issue20204
(3) https://www.python.org/dev/peps/pep-0492
File: python.info, Node: Notable changes in Python 3 5 4, Prev: Porting to Python 3 5, Up: What’s New In Python 3 5
1.4.12 Notable changes in Python 3.5.4
--------------------------------------
* Menu:
* New make regen-all build target: New make regen-all build target<2>.
* Removal of make touch build target: Removal of make touch build target<2>.
File: python.info, Node: New make regen-all build target<2>, Next: Removal of make touch build target<2>, Up: Notable changes in Python 3 5 4
1.4.12.1 New ‘make regen-all’ build target
..........................................
To simplify cross-compilation, and to ensure that CPython can reliably
be compiled without requiring an existing version of Python to already
be available, the autotools-based build system no longer attempts to
implicitly recompile generated files based on file modification times.
Instead, a new ‘make regen-all’ command has been added to force
regeneration of these files when desired (e.g. after an initial version
of Python has already been built based on the pregenerated versions).
More selective regeneration targets are also defined - see
Makefile.pre.in(1) for details.
(Contributed by Victor Stinner in bpo-23404(2).)
New in version 3.5.4.
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Makefile.pre.in
(2) https://bugs.python.org/issue23404
File: python.info, Node: Removal of make touch build target<2>, Prev: New make regen-all build target<2>, Up: Notable changes in Python 3 5 4
1.4.12.2 Removal of ‘make touch’ build target
.............................................
The ‘make touch’ build target previously used to request implicit
regeneration of generated files by updating their modification times has
been removed.
It has been replaced by the new ‘make regen-all’ target.
(Contributed by Victor Stinner in bpo-23404(1).)
Changed in version 3.5.4.
---------- Footnotes ----------
(1) https://bugs.python.org/issue23404
File: python.info, Node: What’s New In Python 3 4, Next: What’s New In Python 3 3, Prev: What’s New In Python 3 5, Up: What’s New in Python
1.5 What’s New In Python 3.4
============================
Author: R. David Murray <> (Editor)
This article explains the new features in Python 3.4, compared to 3.3.
Python 3.4 was released on March 16, 2014. For full details, see the
changelog(1).
See also
........
PEP 429(2) – Python 3.4 Release Schedule
* Menu:
* Summary – Release Highlights: Summary – Release Highlights<2>.
* New Features: New Features<5>.
* New Modules: New Modules<5>.
* Improved Modules: Improved Modules<5>.
* CPython Implementation Changes::
* Deprecated: Deprecated<4>.
* Removed: Removed<3>.
* Porting to Python 3.4: Porting to Python 3 4.
* Changed in 3.4.3: Changed in 3 4 3.
---------- Footnotes ----------
(1) https://docs.python.org/3.4/whatsnew/changelog.html
(2) https://www.python.org/dev/peps/pep-0429
File: python.info, Node: Summary – Release Highlights<2>, Next: New Features<5>, Up: What’s New In Python 3 4
1.5.1 Summary – Release Highlights
----------------------------------
New syntax features:
* No new syntax features were added in Python 3.4.
Other new features:
* *note pip should always be available: 7d4. ( PEP 453(1)).
* *note Newly created file descriptors are non-inheritable: 7d5. (
PEP 446(2)).
* command line option for *note isolated mode: 7d6. (bpo-16499(3)).
* *note improvements in the handling of codecs: 7d7. that are not
text encodings (multiple issues).
* *note A ModuleSpec Type: 7d8. for the Import System ( PEP 451(4)).
(Affects importer authors.)
* The *note marshal: b0. format has been made *note more compact and
efficient: 7d9. (bpo-16475(5)).
New library modules:
* *note asyncio: a.: *note New provisional API for asynchronous IO:
7da. ( PEP 3156(6)).
* *note ensurepip: 7a.: *note Bootstrapping the pip installer: 7db. (
PEP 453(7)).
* *note enum: 7b.: *note Support for enumeration types: 7dc. ( PEP
435(8)).
* *note pathlib: c8.: *note Object-oriented filesystem paths: 7dd. (
PEP 428(9)).
* *note selectors: e6.: *note High-level and efficient I/O
multiplexing: 7de, built upon the *note select: e5. module
primitives (part of PEP 3156(10)).
* *note statistics: f5.: A basic *note numerically stable statistics
library: 7df. ( PEP 450(11)).
* *note tracemalloc: 114.: *note Trace Python memory allocations:
7e0. ( PEP 454(12)).
Significantly improved library modules:
* *note Single-dispatch generic functions: 7e1. in *note functools:
85. ( PEP 443(13)).
* New *note pickle: ca. *note protocol 4: 7e2. ( PEP 3154(14)).
* *note multiprocessing: b7. now has *note an option to avoid using
os.fork on Unix: 7e3. (bpo-8713(15)).
* *note email: 69. has a new submodule, *note contentmanager: 6b, and
a new *note Message: 541. subclass (‘EmailMessage’) that *note
simplify MIME handling: 7e4. (bpo-18891(16)).
* The *note inspect: a0. and *note pydoc: d9. modules are now capable
of correct introspection of a much wider variety of callable
objects, which improves the output of the Python *note help(): 572.
system.
* The *note ipaddress: a2. module API has been declared stable
Security improvements:
* *note Secure and interchangeable hash algorithm: 7e5. ( PEP
456(17)).
* *note Make newly created file descriptors non-inheritable: 7d5. (
PEP 446(18)) to avoid leaking file descriptors to child processes.
* New command line option for *note isolated mode: 7d6,
(bpo-16499(19)).
* *note multiprocessing: b7. now has *note an option to avoid using
os.fork on Unix: 7e3. `spawn' and `forkserver' are more secure
because they avoid sharing data with child processes.
* *note multiprocessing: b7. child processes on Windows no longer
inherit all of the parent’s inheritable handles, only the necessary
ones.
* A new *note hashlib.pbkdf2_hmac(): 7e6. function provides the
PKCS#5 password-based key derivation function 2(20).
* *note TLSv1.1 and TLSv1.2 support: 7e7. for *note ssl: f3.
* *note Retrieving certificates from the Windows system cert store
support: 7e8. for *note ssl: f3.
* *note Server-side SNI (Server Name Indication) support: 7e9. for
*note ssl: f3.
* The *note ssl.SSLContext: 3e4. class has a *note lot of
improvements: 7ea.
* All modules in the standard library that support SSL now support
server certificate verification, including hostname matching (*note
ssl.match_hostname(): 3dc.) and CRLs (Certificate Revocation lists,
see *note ssl.SSLContext.load_verify_locations(): 7eb.).
CPython implementation improvements:
* *note Safe object finalization: 7ec. ( PEP 442(21)).
* Leveraging PEP 442(22), in most cases *note module globals are no
longer set to None during finalization: 7ec. (bpo-18214(23)).
* *note Configurable memory allocators: 7ed. ( PEP 445(24)).
* *note Argument Clinic: 7ee. ( PEP 436(25)).
Please read on for a comprehensive list of user-facing changes,
including many other smaller improvements, CPython optimizations,
deprecations, and potential porting issues.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0453
(2) https://www.python.org/dev/peps/pep-0446
(3) https://bugs.python.org/issue16499
(4) https://www.python.org/dev/peps/pep-0451
(5) https://bugs.python.org/issue16475
(6) https://www.python.org/dev/peps/pep-3156
(7) https://www.python.org/dev/peps/pep-0453
(8) https://www.python.org/dev/peps/pep-0435
(9) https://www.python.org/dev/peps/pep-0428
(10) https://www.python.org/dev/peps/pep-3156
(11) https://www.python.org/dev/peps/pep-0450
(12) https://www.python.org/dev/peps/pep-0454
(13) https://www.python.org/dev/peps/pep-0443
(14) https://www.python.org/dev/peps/pep-3154
(15) https://bugs.python.org/issue8713
(16) https://bugs.python.org/issue18891
(17) https://www.python.org/dev/peps/pep-0456
(18) https://www.python.org/dev/peps/pep-0446
(19) https://bugs.python.org/issue16499
(20) https://en.wikipedia.org/wiki/PBKDF2
(21) https://www.python.org/dev/peps/pep-0442
(22) https://www.python.org/dev/peps/pep-0442
(23) https://bugs.python.org/issue18214
(24) https://www.python.org/dev/peps/pep-0445
(25) https://www.python.org/dev/peps/pep-0436
File: python.info, Node: New Features<5>, Next: New Modules<5>, Prev: Summary – Release Highlights<2>, Up: What’s New In Python 3 4
1.5.2 New Features
------------------
* Menu:
* PEP 453; Explicit Bootstrapping of PIP in Python Installations: PEP 453 Explicit Bootstrapping of PIP in Python Installations.
* PEP 446; Newly Created File Descriptors Are Non-Inheritable: PEP 446 Newly Created File Descriptors Are Non-Inheritable.
* Improvements to Codec Handling::
* PEP 451; A ModuleSpec Type for the Import System: PEP 451 A ModuleSpec Type for the Import System.
* Other Language Changes: Other Language Changes<5>.
File: python.info, Node: PEP 453 Explicit Bootstrapping of PIP in Python Installations, Next: PEP 446 Newly Created File Descriptors Are Non-Inheritable, Up: New Features<5>
1.5.2.1 PEP 453: Explicit Bootstrapping of PIP in Python Installations
......................................................................
* Menu:
* Bootstrapping pip By Default::
* Documentation Changes::
File: python.info, Node: Bootstrapping pip By Default, Next: Documentation Changes, Up: PEP 453 Explicit Bootstrapping of PIP in Python Installations
1.5.2.2 Bootstrapping pip By Default
....................................
The new *note ensurepip: 7a. module (defined in PEP 453(1)) provides a
standard cross-platform mechanism to bootstrap the pip installer into
Python installations and virtual environments. The version of ‘pip’
included with Python 3.4.0 is ‘pip’ 1.5.4, and future 3.4.x maintenance
releases will update the bundled version to the latest version of ‘pip’
that is available at the time of creating the release candidate.
By default, the commands ‘pipX’ and ‘pipX.Y’ will be installed on all
platforms (where X.Y stands for the version of the Python installation),
along with the ‘pip’ Python package and its dependencies. On Windows
and in virtual environments on all platforms, the unversioned ‘pip’
command will also be installed. On other platforms, the system wide
unversioned ‘pip’ command typically refers to the separately installed
Python 2 version.
The ‘pyvenv’ command line utility and the *note venv: 125. module make
use of the *note ensurepip: 7a. module to make ‘pip’ readily available
in virtual environments. When using the command line utility, ‘pip’ is
installed by default, while when using the *note venv: 125. module *note
API: 7f2. installation of ‘pip’ must be requested explicitly.
For CPython *note source builds on POSIX systems: 7f3, the ‘make
install’ and ‘make altinstall’ commands bootstrap ‘pip’ by default.
This behaviour can be controlled through configure options, and
overridden through Makefile options.
On Windows and Mac OS X, the CPython installers now default to
installing ‘pip’ along with CPython itself (users may opt out of
installing it during the installation process). Window users will need
to opt in to the automatic ‘PATH’ modifications to have ‘pip’ available
from the command line by default, otherwise it can still be accessed
through the Python launcher for Windows as ‘py -m pip’.
As discussed in the PEP(2), platform packagers may choose not to install
these commands by default, as long as, when invoked, they provide clear
and simple directions on how to install them on that platform (usually
using the system package manager).
Note: To avoid conflicts between parallel Python 2 and Python 3
installations, only the versioned ‘pip3’ and ‘pip3.4’ commands are
bootstrapped by default when ‘ensurepip’ is invoked directly - the
‘--default-pip’ option is needed to also request the unversioned
‘pip’ command. ‘pyvenv’ and the Windows installer ensure that the
unqualified ‘pip’ command is made available in those environments,
and ‘pip’ can always be invoked via the ‘-m’ switch rather than
directly to avoid ambiguity on systems with multiple Python
installations.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0453
(2)
https://www.python.org/dev/peps/pep-0453/#recommendations-for-downstream-distributors
File: python.info, Node: Documentation Changes, Prev: Bootstrapping pip By Default, Up: PEP 453 Explicit Bootstrapping of PIP in Python Installations
1.5.2.3 Documentation Changes
.............................
As part of this change, the *note Installing Python Modules: 7f5. and
*note Distributing Python Modules: 7f6. sections of the documentation
have been completely redesigned as short getting started and FAQ
documents. Most packaging documentation has now been moved out to the
Python Packaging Authority maintained Python Packaging User Guide(1) and
the documentation of the individual projects.
However, as this migration is currently still incomplete, the legacy
versions of those guides remaining available as *note Installing Python
Modules (Legacy version): 7f7. and *note Distributing Python Modules
(Legacy version): 7f8.
See also
........
PEP 453(2) – Explicit bootstrapping of pip in Python installations
PEP written by Donald Stufft and Nick Coghlan, implemented by
Donald Stufft, Nick Coghlan, Martin von Löwis and Ned Deily.
---------- Footnotes ----------
(1) https://packaging.python.org
(2) https://www.python.org/dev/peps/pep-0453
File: python.info, Node: PEP 446 Newly Created File Descriptors Are Non-Inheritable, Next: Improvements to Codec Handling, Prev: PEP 453 Explicit Bootstrapping of PIP in Python Installations, Up: New Features<5>
1.5.2.4 PEP 446: Newly Created File Descriptors Are Non-Inheritable
...................................................................
PEP 446(1) makes newly created file descriptors *note non-inheritable:
7fa. In general, this is the behavior an application will want: when
launching a new process, having currently open files also open in the
new process can lead to all sorts of hard to find bugs, and potentially
to security issues.
However, there are occasions when inheritance is desired. To support
these cases, the following new functions and methods are available:
* *note os.get_inheritable(): 7fb, *note os.set_inheritable(): 7fc.
* *note os.get_handle_inheritable(): 7fd, *note
os.set_handle_inheritable(): 7fe.
* *note socket.socket.get_inheritable(): 7ff, *note
socket.socket.set_inheritable(): 800.
See also
........
PEP 446(2) – Make newly created file descriptors non-inheritable
PEP written and implemented by Victor Stinner.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0446
(2) https://www.python.org/dev/peps/pep-0446
File: python.info, Node: Improvements to Codec Handling, Next: PEP 451 A ModuleSpec Type for the Import System, Prev: PEP 446 Newly Created File Descriptors Are Non-Inheritable, Up: New Features<5>
1.5.2.5 Improvements to Codec Handling
......................................
Since it was first introduced, the *note codecs: 1c. module has always
been intended to operate as a type-neutral dynamic encoding and decoding
system. However, its close coupling with the Python text model,
especially the type restricted convenience methods on the builtin *note
str: 330, *note bytes: 331. and *note bytearray: 332. types, has
historically obscured that fact.
As a key step in clarifying the situation, the *note codecs.encode():
802. and *note codecs.decode(): 803. convenience functions are now
properly documented in Python 2.7, 3.3 and 3.4. These functions have
existed in the *note codecs: 1c. module (and have been covered by the
regression test suite) since Python 2.4, but were previously only
discoverable through runtime introspection.
Unlike the convenience methods on *note str: 330, *note bytes: 331. and
*note bytearray: 332, the *note codecs: 1c. convenience functions
support arbitrary codecs in both Python 2 and Python 3, rather than
being limited to Unicode text encodings (in Python 3) or ‘basestring’
<-> ‘basestring’ conversions (in Python 2).
In Python 3.4, the interpreter is able to identify the known non-text
encodings provided in the standard library and direct users towards
these general purpose convenience functions when appropriate:
>>> b"abcdef".decode("hex")
Traceback (most recent call last):
File "", line 1, in
LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs
>>> "hello".encode("rot13")
Traceback (most recent call last):
File "", line 1, in
LookupError: 'rot13' is not a text encoding; use codecs.encode() to handle arbitrary codecs
>>> open("foo.txt", encoding="hex")
Traceback (most recent call last):
File "", line 1, in
LookupError: 'hex' is not a text encoding; use codecs.open() to handle arbitrary codecs
In a related change, whenever it is feasible without breaking backwards
compatibility, exceptions raised during encoding and decoding operations
are wrapped in a chained exception of the same type that mentions the
name of the codec responsible for producing the error:
>>> import codecs
>>> codecs.decode(b"abcdefgh", "hex")
Traceback (most recent call last):
File "/usr/lib/python3.4/encodings/hex_codec.py", line 20, in hex_decode
return (binascii.a2b_hex(input), len(input))
binascii.Error: Non-hexadecimal digit found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "", line 1, in
binascii.Error: decoding with 'hex' codec failed (Error: Non-hexadecimal digit found)
>>> codecs.encode("hello", "bz2")
Traceback (most recent call last):
File "/usr/lib/python3.4/encodings/bz2_codec.py", line 17, in bz2_encode
return (bz2.compress(input), len(input))
File "/usr/lib/python3.4/bz2.py", line 498, in compress
return comp.compress(data) + comp.flush()
TypeError: 'str' does not support the buffer interface
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "", line 1, in
TypeError: encoding with 'bz2' codec failed (TypeError: 'str' does not support the buffer interface)
Finally, as the examples above show, these improvements have permitted
the restoration of the convenience aliases for the non-Unicode codecs
that were themselves restored in Python 3.2. This means that encoding
binary data to and from its hexadecimal representation (for example) can
now be written as:
>>> from codecs import encode, decode
>>> encode(b"hello", "hex")
b'68656c6c6f'
>>> decode(b"68656c6c6f", "hex")
b'hello'
The binary and text transforms provided in the standard library are
detailed in *note Binary Transforms: 804. and *note Text Transforms:
805.
(Contributed by Nick Coghlan in bpo-7475(1), bpo-17827(2), bpo-17828(3)
and bpo-19619(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue7475
(2) https://bugs.python.org/issue17827
(3) https://bugs.python.org/issue17828
(4) https://bugs.python.org/issue19619
File: python.info, Node: PEP 451 A ModuleSpec Type for the Import System, Next: Other Language Changes<5>, Prev: Improvements to Codec Handling, Up: New Features<5>
1.5.2.6 PEP 451: A ModuleSpec Type for the Import System
........................................................
PEP 451(1) provides an encapsulation of the information about a module
that the import machinery will use to load it (that is, a module
specification). This helps simplify both the import implementation and
several import-related APIs. The change is also a stepping stone for
several future import-related improvements(2).
The public-facing changes from the PEP are entirely backward-compatible.
Furthermore, they should be transparent to everyone but importer
authors. Key finder and loader methods have been deprecated, but they
will continue working. New importers should use the new methods
described in the PEP. Existing importers should be updated to implement
the new methods. See the *note Deprecated: 807. section for a list of
methods that should be replaced and their replacements.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0451
(2)
https://mail.python.org/pipermail/python-dev/2013-November/130111.html
File: python.info, Node: Other Language Changes<5>, Prev: PEP 451 A ModuleSpec Type for the Import System, Up: New Features<5>
1.5.2.7 Other Language Changes
..............................
Some smaller changes made to the core Python language are:
* Unicode database updated to UCD version 6.3.
* *note min(): 809. and *note max(): 80a. now accept a `default'
keyword-only argument that can be used to specify the value they
return if the iterable they are evaluating has no elements.
(Contributed by Julian Berman in bpo-18111(1).)
* Module objects are now *note weakref: 128.’able.
* Module ‘__file__’ attributes (and related values) should now always
contain absolute paths by default, with the sole exception of
‘__main__.__file__’ when a script has been executed directly using
a relative path. (Contributed by Brett Cannon in bpo-18416(2).)
* All the UTF-* codecs (except UTF-7) now reject surrogates during
both encoding and decoding unless the ‘surrogatepass’ error handler
is used, with the exception of the UTF-16 decoder (which accepts
valid surrogate pairs) and the UTF-16 encoder (which produces them
while encoding non-BMP characters). (Contributed by Victor
Stinner, Kang-Hao (Kenny) Lu and Serhiy Storchaka in bpo-12892(3).)
* New German EBCDIC *note codec: 68f. ‘cp273’. (Contributed by
Michael Bierenfeld and Andrew Kuchling in bpo-1097797(4).)
* New Ukrainian *note codec: 68f. ‘cp1125’. (Contributed by Serhiy
Storchaka in bpo-19668(5).)
* *note bytes: 331.join() and *note bytearray: 332.join() now accept
arbitrary buffer objects as arguments. (Contributed by Antoine
Pitrou in bpo-15958(6).)
* The *note int: 184. constructor now accepts any object that has an
‘__index__’ method for its `base' argument. (Contributed by Mark
Dickinson in bpo-16772(7).)
* Frame objects now have a *note clear(): 80b. method that clears all
references to local variables from the frame. (Contributed by
Antoine Pitrou in bpo-17934(8).)
* *note memoryview: 25c. is now registered as a *note Sequence: 1f,
and supports the *note reversed(): 18e. builtin. (Contributed by
Nick Coghlan and Claudiu Popa in bpo-18690(9) and bpo-19078(10).)
* Signatures reported by *note help(): 572. have been modified and
improved in several cases as a result of the introduction of
Argument Clinic and other changes to the *note inspect: a0. and
*note pydoc: d9. modules.
* *note __length_hint__(): 80c. is now part of the formal language
specification (see PEP 424(11)). (Contributed by Armin Ronacher in
bpo-16148(12).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18111
(2) https://bugs.python.org/issue18416
(3) https://bugs.python.org/issue12892
(4) https://bugs.python.org/issue1097797
(5) https://bugs.python.org/issue19668
(6) https://bugs.python.org/issue15958
(7) https://bugs.python.org/issue16772
(8) https://bugs.python.org/issue17934
(9) https://bugs.python.org/issue18690
(10) https://bugs.python.org/issue19078
(11) https://www.python.org/dev/peps/pep-0424
(12) https://bugs.python.org/issue16148
File: python.info, Node: New Modules<5>, Next: Improved Modules<5>, Prev: New Features<5>, Up: What’s New In Python 3 4
1.5.3 New Modules
-----------------
* Menu:
* asyncio: asyncio<6>.
* ensurepip::
* enum: enum<5>.
* pathlib: pathlib<5>.
* selectors: selectors<2>.
* statistics: statistics<3>.
* tracemalloc: tracemalloc<3>.
File: python.info, Node: asyncio<6>, Next: ensurepip, Up: New Modules<5>
1.5.3.1 asyncio
...............
The new *note asyncio: a. module (defined in PEP 3156(1)) provides a
standard pluggable event loop model for Python, providing solid
asynchronous IO support in the standard library, and making it easier
for other event loop implementations to interoperate with the standard
library and each other.
For Python 3.4, this module is considered a *note provisional API: 348.
See also
........
PEP 3156(2) – Asynchronous IO Support Rebooted: the “asyncio” Module
PEP written and implementation led by Guido van Rossum.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3156
(2) https://www.python.org/dev/peps/pep-3156
File: python.info, Node: ensurepip, Next: enum<5>, Prev: asyncio<6>, Up: New Modules<5>
1.5.3.2 ensurepip
.................
The new *note ensurepip: 7a. module is the primary infrastructure for
the PEP 453(1) implementation. In the normal course of events end users
will not need to interact with this module, but it can be used to
manually bootstrap ‘pip’ if the automated bootstrapping into an
installation or virtual environment was declined.
*note ensurepip: 7a. includes a bundled copy of ‘pip’, up-to-date as of
the first release candidate of the release of CPython with which it
ships (this applies to both maintenance releases and feature releases).
‘ensurepip’ does not access the internet. If the installation has
Internet access, after ‘ensurepip’ is run the bundled ‘pip’ can be used
to upgrade ‘pip’ to a more recent release than the bundled one. (Note
that such an upgraded version of ‘pip’ is considered to be a separately
installed package and will not be removed if Python is uninstalled.)
The module is named `ensure'pip because if called when ‘pip’ is already
installed, it does nothing. It also has an ‘--upgrade’ option that will
cause it to install the bundled copy of ‘pip’ if the existing installed
version of ‘pip’ is older than the bundled copy.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0453
File: python.info, Node: enum<5>, Next: pathlib<5>, Prev: ensurepip, Up: New Modules<5>
1.5.3.3 enum
............
The new *note enum: 7b. module (defined in PEP 435(1)) provides a
standard implementation of enumeration types, allowing other modules
(such as *note socket: ef.) to provide more informative error messages
and better debugging support by replacing opaque integer constants with
backwards compatible enumeration values.
See also
........
PEP 435(2) – Adding an Enum type to the Python standard library
PEP written by Barry Warsaw, Eli Bendersky and Ethan Furman,
implemented by Ethan Furman.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0435
(2) https://www.python.org/dev/peps/pep-0435
File: python.info, Node: pathlib<5>, Next: selectors<2>, Prev: enum<5>, Up: New Modules<5>
1.5.3.4 pathlib
...............
The new *note pathlib: c8. module offers classes representing filesystem
paths with semantics appropriate for different operating systems. Path
classes are divided between `pure paths', which provide purely
computational operations without I/O, and `concrete paths', which
inherit from pure paths but also provide I/O operations.
For Python 3.4, this module is considered a *note provisional API: 348.
See also
........
PEP 428(1) – The pathlib module – object-oriented filesystem paths
PEP written and implemented by Antoine Pitrou.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0428
File: python.info, Node: selectors<2>, Next: statistics<3>, Prev: pathlib<5>, Up: New Modules<5>
1.5.3.5 selectors
.................
The new *note selectors: e6. module (created as part of implementing PEP
3156(1)) allows high-level and efficient I/O multiplexing, built upon
the *note select: e5. module primitives.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3156
File: python.info, Node: statistics<3>, Next: tracemalloc<3>, Prev: selectors<2>, Up: New Modules<5>
1.5.3.6 statistics
..................
The new *note statistics: f5. module (defined in PEP 450(1)) offers some
core statistics functionality directly in the standard library. This
module supports calculation of the mean, median, mode, variance and
standard deviation of a data series.
See also
........
PEP 450(2) – Adding A Statistics Module To The Standard Library
PEP written and implemented by Steven D’Aprano
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0450
(2) https://www.python.org/dev/peps/pep-0450
File: python.info, Node: tracemalloc<3>, Prev: statistics<3>, Up: New Modules<5>
1.5.3.7 tracemalloc
...................
The new *note tracemalloc: 114. module (defined in PEP 454(1)) is a
debug tool to trace memory blocks allocated by Python. It provides the
following information:
* Trace where an object was allocated
* Statistics on allocated memory blocks per filename and per line
number: total size, number and average size of allocated memory
blocks
* Compute the differences between two snapshots to detect memory
leaks
See also
........
PEP 454(2) – Add a new tracemalloc module to trace Python memory allocations
PEP written and implemented by Victor Stinner
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0454
(2) https://www.python.org/dev/peps/pep-0454
File: python.info, Node: Improved Modules<5>, Next: CPython Implementation Changes, Prev: New Modules<5>, Up: What’s New In Python 3 4
1.5.4 Improved Modules
----------------------
* Menu:
* abc::
* aifc: aifc<2>.
* argparse: argparse<3>.
* audioop::
* base64::
* collections: collections<6>.
* colorsys::
* contextlib: contextlib<4>.
* dbm: dbm<5>.
* dis: dis<2>.
* doctest: doctest<2>.
* email: email<3>.
* filecmp::
* functools: functools<4>.
* gc: gc<3>.
* glob: glob<2>.
* hashlib: hashlib<2>.
* hmac: hmac<2>.
* html::
* http: http<2>.
* idlelib and IDLE: idlelib and IDLE<4>.
* importlib: importlib<6>.
* inspect: inspect<4>.
* ipaddress: ipaddress<3>.
* logging: logging<5>.
* marshal::
* mmap: mmap<2>.
* multiprocessing: multiprocessing<5>.
* operator: operator<2>.
* os: os<6>.
* pdb: pdb<3>.
* pickle: pickle<4>.
* plistlib: plistlib<2>.
* poplib: poplib<2>.
* pprint: pprint<2>.
* pty::
* pydoc: pydoc<3>.
* re: re<5>.
* resource::
* select::
* shelve::
* shutil: shutil<3>.
* smtpd: smtpd<2>.
* smtplib: smtplib<2>.
* socket: socket<6>.
* sqlite3: sqlite3<4>.
* ssl: ssl<7>.
* stat::
* struct: struct<2>.
* subprocess: subprocess<4>.
* sunau: sunau<2>.
* sys: sys<6>.
* tarfile: tarfile<3>.
* textwrap::
* threading: threading<4>.
* traceback: traceback<3>.
* types: types<3>.
* urllib: urllib<2>.
* unittest: unittest<4>.
* venv: venv<4>.
* wave: wave<2>.
* weakref: weakref<2>.
* xml.etree: xml etree<2>.
* zipfile: zipfile<4>.
File: python.info, Node: abc, Next: aifc<2>, Up: Improved Modules<5>
1.5.4.1 abc
...........
New function *note abc.get_cache_token(): 817. can be used to know when
to invalidate caches that are affected by changes in the object graph.
(Contributed by Łukasz Langa in bpo-16832(1).)
New class *note ABC: 818. has *note ABCMeta: 819. as its meta class.
Using ‘ABC’ as a base class has essentially the same effect as
specifying ‘metaclass=abc.ABCMeta’, but is simpler to type and easier to
read. (Contributed by Bruno Dupuis in bpo-16049(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16832
(2) https://bugs.python.org/issue16049
File: python.info, Node: aifc<2>, Next: argparse<3>, Prev: abc, Up: Improved Modules<5>
1.5.4.2 aifc
............
The *note getparams(): 81b. method now returns a namedtuple rather than
a plain tuple. (Contributed by Claudiu Popa in bpo-17818(1).)
*note aifc.open(): 45b. now supports the context management protocol:
when used in a *note with: 6e9. block, the *note close(): 81c. method of
the returned object will be called automatically at the end of the
block. (Contributed by Serhiy Storchacha in bpo-16486(2).)
The *note writeframesraw(): 81d. and *note writeframes(): 81e. methods
now accept any *note bytes-like object: 5e8. (Contributed by Serhiy
Storchaka in bpo-8311(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17818
(2) https://bugs.python.org/issue16486
(3) https://bugs.python.org/issue8311
File: python.info, Node: argparse<3>, Next: audioop, Prev: aifc<2>, Up: Improved Modules<5>
1.5.4.3 argparse
................
The *note FileType: 820. class now accepts `encoding' and `errors'
arguments, which are passed through to *note open(): 4f0. (Contributed
by Lucas Maystre in bpo-11175(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11175
File: python.info, Node: audioop, Next: base64, Prev: argparse<3>, Up: Improved Modules<5>
1.5.4.4 audioop
...............
*note audioop: d. now supports 24-bit samples. (Contributed by Serhiy
Storchaka in bpo-12866(1).)
New *note byteswap(): 822. function converts big-endian samples to
little-endian and vice versa. (Contributed by Serhiy Storchaka in
bpo-19641(2).)
All *note audioop: d. functions now accept any *note bytes-like object:
5e8. Strings are not accepted: they didn’t work before, now they raise
an error right away. (Contributed by Serhiy Storchaka in bpo-16685(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12866
(2) https://bugs.python.org/issue19641
(3) https://bugs.python.org/issue16685
File: python.info, Node: base64, Next: collections<6>, Prev: audioop, Up: Improved Modules<5>
1.5.4.5 base64
..............
The encoding and decoding functions in *note base64: e. now accept any
*note bytes-like object: 5e8. in cases where it previously required a
*note bytes: 331. or *note bytearray: 332. instance. (Contributed by
Nick Coghlan in bpo-17839(1).)
New functions *note a85encode(): 824, *note a85decode(): 825, *note
b85encode(): 826, and *note b85decode(): 827. provide the ability to
encode and decode binary data from and to ‘Ascii85’ and the
git/mercurial ‘Base85’ formats, respectively. The ‘a85’ functions have
options that can be used to make them compatible with the variants of
the ‘Ascii85’ encoding, including the Adobe variant. (Contributed by
Martin Morrison, the Mercurial project, Serhiy Storchaka, and Antoine
Pitrou in bpo-17618(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17839
(2) https://bugs.python.org/issue17618
File: python.info, Node: collections<6>, Next: colorsys, Prev: base64, Up: Improved Modules<5>
1.5.4.6 collections
...................
The *note ChainMap.new_child(): 829. method now accepts an `m' argument
specifying the child map to add to the chain. This allows an existing
mapping and/or a custom mapping type to be used for the child.
(Contributed by Vinay Sajip in bpo-16613(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16613
File: python.info, Node: colorsys, Next: contextlib<4>, Prev: collections<6>, Up: Improved Modules<5>
1.5.4.7 colorsys
................
The number of digits in the coefficients for the RGB — YIQ conversions
have been expanded so that they match the FCC NTSC versions. The change
in results should be less than 1% and may better match results found
elsewhere. (Contributed by Brian Landers and Serhiy Storchaka in
bpo-14323(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14323
File: python.info, Node: contextlib<4>, Next: dbm<5>, Prev: colorsys, Up: Improved Modules<5>
1.5.4.8 contextlib
..................
The new *note contextlib.suppress: 82c. context manager helps to clarify
the intent of code that deliberately suppresses exceptions from a single
statement. (Contributed by Raymond Hettinger in bpo-15806(1) and Zero
Piraeus in bpo-19266(2).)
The new *note contextlib.redirect_stdout(): 6c2. context manager makes
it easier for utility scripts to handle inflexible APIs that write their
output to *note sys.stdout: 30e. and don’t provide any options to
redirect it. Using the context manager, the *note sys.stdout: 30e.
output can be redirected to any other stream or, in conjunction with
*note io.StringIO: 82d, to a string. The latter can be especially
useful, for example, to capture output from a function that was written
to implement a command line interface. It is recommended only for
utility scripts because it affects the global state of *note sys.stdout:
30e. (Contributed by Raymond Hettinger in bpo-15805(3).)
The *note contextlib: 24. documentation has also been updated to include
a *note discussion: 82e. of the differences between single use, reusable
and reentrant context managers.
---------- Footnotes ----------
(1) https://bugs.python.org/issue15806
(2) https://bugs.python.org/issue19266
(3) https://bugs.python.org/issue15805
File: python.info, Node: dbm<5>, Next: dis<2>, Prev: contextlib<4>, Up: Improved Modules<5>
1.5.4.9 dbm
...........
*note dbm.open(): 830. objects now support the context management
protocol. When used in a *note with: 6e9. statement, the ‘close’ method
of the database object will be called automatically at the end of the
block. (Contributed by Claudiu Popa and Nick Coghlan in bpo-19282(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19282
File: python.info, Node: dis<2>, Next: doctest<2>, Prev: dbm<5>, Up: Improved Modules<5>
1.5.4.10 dis
............
Functions *note show_code(): 832, *note dis(): 384, *note distb(): 833,
and *note disassemble(): 834. now accept a keyword-only `file' argument
that controls where they write their output.
The *note dis: 38. module is now built around an *note Instruction: 835.
class that provides object oriented access to the details of each
individual bytecode operation.
A new method, *note get_instructions(): 836, provides an iterator that
emits the Instruction stream for a given piece of Python code. Thus it
is now possible to write a program that inspects and manipulates a
bytecode object in ways different from those provided by the *note dis:
38. module itself. For example:
>>> import dis
>>> for instr in dis.get_instructions(lambda x: x + 1):
... print(instr.opname)
LOAD_FAST
LOAD_CONST
BINARY_ADD
RETURN_VALUE
The various display tools in the *note dis: 38. module have been
rewritten to use these new components.
In addition, a new application-friendly class *note Bytecode: 837.
provides an object-oriented API for inspecting bytecode in both in
human-readable form and for iterating over instructions. The *note
Bytecode: 837. constructor takes the same arguments that
‘get_instruction()’ does (plus an optional `current_offset'), and the
resulting object can be iterated to produce *note Instruction: 835.
objects. But it also has a *note dis: 838. method, equivalent to
calling *note dis: 384. on the constructor argument, but returned as a
multi-line string:
>>> bytecode = dis.Bytecode(lambda x: x + 1, current_offset=3)
>>> for instr in bytecode:
... print('{} ({})'.format(instr.opname, instr.opcode))
LOAD_FAST (124)
LOAD_CONST (100)
BINARY_ADD (23)
RETURN_VALUE (83)
>>> bytecode.dis().splitlines()
[' 1 0 LOAD_FAST 0 (x)',
' --> 3 LOAD_CONST 1 (1)',
' 6 BINARY_ADD',
' 7 RETURN_VALUE']
*note Bytecode: 837. also has a class method, *note from_traceback():
839, that provides the ability to manipulate a traceback (that is,
‘print(Bytecode.from_traceback(tb).dis())’ is equivalent to
‘distb(tb)’).
(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in
bpo-11816(1) and Claudiu Popa in bpo-17916(2).)
New function *note stack_effect(): 83a. computes the effect on the
Python stack of a given opcode and argument, information that is not
otherwise available. (Contributed by Larry Hastings in bpo-19722(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11816
(2) https://bugs.python.org/issue17916
(3) https://bugs.python.org/issue19722
File: python.info, Node: doctest<2>, Next: email<3>, Prev: dis<2>, Up: Improved Modules<5>
1.5.4.11 doctest
................
A new *note option flag: 83c, *note FAIL_FAST: 83d, halts test running
as soon as the first failure is detected. (Contributed by R. David
Murray and Daniel Urban in bpo-16522(1).)
The *note doctest: 67. command line interface now uses *note argparse:
6, and has two new options, ‘-o’ and ‘-f’. ‘-o’ allows *note doctest
options: 83c. to be specified on the command line, and ‘-f’ is a
shorthand for ‘-o FAIL_FAST’ (to parallel the similar option supported
by the *note unittest: 11b. CLI). (Contributed by R. David Murray in
bpo-11390(2).)
*note doctest: 67. will now find doctests in extension module ‘__doc__’
strings. (Contributed by Zachary Ware in bpo-3158(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16522
(2) https://bugs.python.org/issue11390
(3) https://bugs.python.org/issue3158
File: python.info, Node: email<3>, Next: filecmp, Prev: doctest<2>, Up: Improved Modules<5>
1.5.4.12 email
..............
*note as_string(): 83f. now accepts a `policy' argument to override the
default policy of the message when generating a string representation of
it. This means that ‘as_string’ can now be used in more circumstances,
instead of having to create and use a *note generator: 6e. in order to
pass formatting parameters to its ‘flatten’ method. (Contributed by R.
David Murray in bpo-18600(1).)
New method *note as_bytes(): 840. added to produce a bytes
representation of the message in a fashion similar to how ‘as_string’
produces a string representation. It does not accept the `maxheaderlen'
argument, but does accept the `unixfrom' and `policy' arguments. The
*note Message: 541. *note __bytes__(): 841. method calls it, meaning
that ‘bytes(mymsg)’ will now produce the intuitive result: a bytes
object containing the fully formatted message. (Contributed by R. David
Murray in bpo-18600(2).)
The *note Message.set_param(): 842. message now accepts a `replace'
keyword argument. When specified, the associated header will be updated
without changing its location in the list of headers. For backward
compatibility, the default is ‘False’. (Contributed by R. David Murray
in bpo-18891(3).) A pair of new subclasses of *note Message: 541. have
been added (*note EmailMessage: 542. and *note MIMEPart: 843.), along
with a new sub-module, *note contentmanager: 6b. and a new *note policy:
75. attribute *note content_manager: 844. All documentation is
currently in the new module, which is being added as part of email’s new
*note provisional API: 348. These classes provide a number of new
methods that make extracting content from and inserting content into
email messages much easier. For details, see the *note contentmanager:
6b. documentation and the *note email; Examples: 845. These API
additions complete the bulk of the work that was planned as part of the
email6 project. The currently provisional API is scheduled to become
final in Python 3.5 (possibly with a few minor additions in the area of
error handling). (Contributed by R. David Murray in bpo-18891(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18600
(2) https://bugs.python.org/issue18600
(3) https://bugs.python.org/issue18891
(4) https://bugs.python.org/issue18891
File: python.info, Node: filecmp, Next: functools<4>, Prev: email<3>, Up: Improved Modules<5>
1.5.4.13 filecmp
................
A new *note clear_cache(): 847. function provides the ability to clear
the *note filecmp: 7f. comparison cache, which uses *note os.stat():
1f1. information to determine if the file has changed since the last
compare. This can be used, for example, if the file might have been
changed and re-checked in less time than the resolution of a particular
filesystem’s file modification time field. (Contributed by Mark Levitt
in bpo-18149(1).)
New module attribute *note DEFAULT_IGNORES: 848. provides the list of
directories that are used as the default value for the `ignore'
parameter of the *note dircmp(): 849. function. (Contributed by Eli
Bendersky in bpo-15442(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18149
(2) https://bugs.python.org/issue15442
File: python.info, Node: functools<4>, Next: gc<3>, Prev: filecmp, Up: Improved Modules<5>
1.5.4.14 functools
..................
The new *note partialmethod(): 29c. descriptor brings partial argument
application to descriptors, just as *note partial(): 7c8. provides for
normal callables. The new descriptor also makes it easier to get
arbitrary callables (including *note partial(): 7c8. instances) to
behave like normal instance methods when included in a class definition.
(Contributed by Alon Horev and Nick Coghlan in bpo-4331(1).) The new
*note singledispatch(): 38a. decorator brings support for
single-dispatch generic functions to the Python standard library. Where
object oriented programming focuses on grouping multiple operations on a
common set of data into a class, a generic function focuses on grouping
multiple implementations of an operation that allows it to work with
`different' kinds of data.
See also
........
PEP 443(2) – Single-dispatch generic functions
PEP written and implemented by Łukasz Langa.
*note total_ordering(): 84b. now supports a return value of *note
NotImplemented: 84c. from the underlying comparison function.
(Contributed by Katie Miller in bpo-10042(3).)
A pure-python version of the *note partial(): 7c8. function is now in
the stdlib; in CPython it is overridden by the C accelerated version,
but it is available for other implementations to use. (Contributed by
Brian Thorne in bpo-12428(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4331
(2) https://www.python.org/dev/peps/pep-0443
(3) https://bugs.python.org/issue10042
(4) https://bugs.python.org/issue12428
File: python.info, Node: gc<3>, Next: glob<2>, Prev: functools<4>, Up: Improved Modules<5>
1.5.4.15 gc
...........
New function *note get_stats(): 84e. returns a list of three
per-generation dictionaries containing the collections statistics since
interpreter startup. (Contributed by Antoine Pitrou in bpo-16351(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16351
File: python.info, Node: glob<2>, Next: hashlib<2>, Prev: gc<3>, Up: Improved Modules<5>
1.5.4.16 glob
.............
A new function *note escape(): 850. provides a way to escape special
characters in a filename so that they do not become part of the globbing
expansion but are instead matched literally. (Contributed by Serhiy
Storchaka in bpo-8402(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8402
File: python.info, Node: hashlib<2>, Next: hmac<2>, Prev: glob<2>, Up: Improved Modules<5>
1.5.4.17 hashlib
................
A new *note hashlib.pbkdf2_hmac(): 7e6. function provides the PKCS#5
password-based key derivation function 2(1). (Contributed by Christian
Heimes in bpo-18582(2).)
The *note name: 852. attribute of *note hashlib: 8d. hash objects is now
a formally supported interface. It has always existed in CPython’s
*note hashlib: 8d. (although it did not return lower case names for all
supported hashes), but it was not a public interface and so some other
Python implementations have not previously supported it. (Contributed
by Jason R. Coombs in bpo-18532(3).)
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/PBKDF2
(2) https://bugs.python.org/issue18582
(3) https://bugs.python.org/issue18532
File: python.info, Node: hmac<2>, Next: html, Prev: hashlib<2>, Up: Improved Modules<5>
1.5.4.18 hmac
.............
*note hmac: 8f. now accepts ‘bytearray’ as well as ‘bytes’ for the `key'
argument to the *note new(): 854. function, and the `msg' parameter to
both the *note new(): 854. function and the *note update(): 855. method
now accepts any type supported by the *note hashlib: 8d. module.
(Contributed by Jonas Borgström in bpo-18240(1).)
The `digestmod' argument to the *note hmac.new(): 854. function may now
be any hash digest name recognized by *note hashlib: 8d. In addition,
the current behavior in which the value of `digestmod' defaults to ‘MD5’
is deprecated: in a future version of Python there will be no default
value. (Contributed by Christian Heimes in bpo-17276(2).)
With the addition of *note block_size: 856. and *note name: 857.
attributes (and the formal documentation of the *note digest_size: 858.
attribute), the *note hmac: 8f. module now conforms fully to the PEP
247(3) API. (Contributed by Christian Heimes in bpo-18775(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18240
(2) https://bugs.python.org/issue17276
(3) https://www.python.org/dev/peps/pep-0247
(4) https://bugs.python.org/issue18775
File: python.info, Node: html, Next: http<2>, Prev: hmac<2>, Up: Improved Modules<5>
1.5.4.19 html
.............
New function *note unescape(): 85a. function converts HTML5 character
references to the corresponding Unicode characters. (Contributed by
Ezio Melotti in bpo-2927(1).)
*note HTMLParser: 7bf. accepts a new keyword argument `convert_charrefs'
that, when ‘True’, automatically converts all character references. For
backward-compatibility, its value defaults to ‘False’, but it will
change to ‘True’ in a future version of Python, so you are invited to
set it explicitly and update your code to use this new feature.
(Contributed by Ezio Melotti in bpo-13633(2).)
The `strict' argument of *note HTMLParser: 7bf. is now deprecated.
(Contributed by Ezio Melotti in bpo-15114(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue2927
(2) https://bugs.python.org/issue13633
(3) https://bugs.python.org/issue15114
File: python.info, Node: http<2>, Next: idlelib and IDLE<4>, Prev: html, Up: Improved Modules<5>
1.5.4.20 http
.............
*note send_error(): 85c. now accepts an optional additional `explain'
parameter which can be used to provide an extended error description,
overriding the hardcoded default if there is one. This extended error
description will be formatted using the ‘error_message_format’ attribute
and sent as the body of the error response. (Contributed by Karl Cow in
bpo-12921(1).)
The *note http.server: 97. *note command line interface: 85d. now has a
‘-b/--bind’ option that causes the server to listen on a specific
address. (Contributed by Malte Swart in bpo-17764(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12921
(2) https://bugs.python.org/issue17764
File: python.info, Node: idlelib and IDLE<4>, Next: importlib<6>, Prev: http<2>, Up: Improved Modules<5>
1.5.4.21 idlelib and IDLE
.........................
Since idlelib implements the IDLE shell and editor and is not intended
for import by other programs, it gets improvements with every release.
See ‘Lib/idlelib/NEWS.txt’ for a cumulative list of changes since 3.3.0,
as well as changes made in future 3.4.x releases. This file is also
available from the IDLE Help ‣ About IDLE dialog.
File: python.info, Node: importlib<6>, Next: inspect<4>, Prev: idlelib and IDLE<4>, Up: Improved Modules<5>
1.5.4.22 importlib
..................
The *note InspectLoader: 860. ABC defines a new method, *note
source_to_code(): 6ef. that accepts source data and a path and returns a
code object. The default implementation is equivalent to ‘compile(data,
path, 'exec', dont_inherit=True)’. (Contributed by Eric Snow and Brett
Cannon in bpo-15627(1).)
*note InspectLoader: 860. also now has a default implementation for the
*note get_code(): 861. method. However, it will normally be desirable
to override the default implementation for performance reasons.
(Contributed by Brett Cannon in bpo-18072(2).)
The *note reload(): 399. function has been moved from *note imp: 9a. to
*note importlib: 9b. as part of the *note imp: 9a. module deprecation.
(Contributed by Berker Peksag in bpo-18193(3).)
*note importlib.util: 9f. now has a *note MAGIC_NUMBER: 862. attribute
providing access to the bytecode version number. This replaces the
*note get_magic(): 863. function in the deprecated *note imp: 9a.
module. (Contributed by Brett Cannon in bpo-18192(4).)
New *note importlib.util: 9f. functions *note cache_from_source(): 557.
and *note source_from_cache(): 558. replace the same-named functions in
the deprecated *note imp: 9a. module. (Contributed by Brett Cannon in
bpo-18194(5).)
The *note importlib: 9b. bootstrap ‘NamespaceLoader’ now conforms to the
*note InspectLoader: 860. ABC, which means that ‘runpy’ and ‘python -m’
can now be used with namespace packages. (Contributed by Brett Cannon
in bpo-18058(6).)
*note importlib.util: 9f. has a new function *note decode_source(): 864.
that decodes source from bytes using universal newline processing. This
is useful for implementing *note InspectLoader.get_source(): 865.
methods.
*note importlib.machinery.ExtensionFileLoader: 556. now has a *note
get_filename(): 866. method. This was inadvertently omitted in the
original implementation. (Contributed by Eric Snow in bpo-19152(7).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15627
(2) https://bugs.python.org/issue18072
(3) https://bugs.python.org/issue18193
(4) https://bugs.python.org/issue18192
(5) https://bugs.python.org/issue18194
(6) https://bugs.python.org/issue18058
(7) https://bugs.python.org/issue19152
File: python.info, Node: inspect<4>, Next: ipaddress<3>, Prev: importlib<6>, Up: Improved Modules<5>
1.5.4.23 inspect
................
The *note inspect: a0. module now offers a basic *note command line
interface: 868. to quickly display source code and other information for
modules, classes and functions. (Contributed by Claudiu Popa and Nick
Coghlan in bpo-18626(1).)
*note unwrap(): 869. makes it easy to unravel wrapper function chains
created by *note functools.wraps(): 86a. (and any other API that sets
the ‘__wrapped__’ attribute on a wrapper function). (Contributed by
Daniel Urban, Aaron Iles and Nick Coghlan in bpo-13266(2).)
As part of the implementation of the new *note enum: 7b. module, the
*note inspect: a0. module now has substantially better support for
custom ‘__dir__’ methods and dynamic class attributes provided through
metaclasses. (Contributed by Ethan Furman in bpo-18929(3) and
bpo-19030(4).)
*note getfullargspec(): 55d. and *note getargspec(): 55c. now use the
*note signature(): 55b. API. This allows them to support a much broader
range of callables, including those with ‘__signature__’ attributes,
those with metadata provided by argument clinic, *note
functools.partial(): 7c8. objects and more. Note that, unlike *note
signature(): 55b, these functions still ignore ‘__wrapped__’ attributes,
and report the already bound first argument for bound methods, so it is
still necessary to update your code to use *note signature(): 55b.
directly if those features are desired. (Contributed by Yury Selivanov
in bpo-17481(5).)
*note signature(): 55b. now supports duck types of CPython functions,
which adds support for functions compiled with Cython. (Contributed by
Stefan Behnel and Yury Selivanov in bpo-17159(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18626
(2) https://bugs.python.org/issue13266
(3) https://bugs.python.org/issue18929
(4) https://bugs.python.org/issue19030
(5) https://bugs.python.org/issue17481
(6) https://bugs.python.org/issue17159
File: python.info, Node: ipaddress<3>, Next: logging<5>, Prev: inspect<4>, Up: Improved Modules<5>
1.5.4.24 ipaddress
..................
*note ipaddress: a2. was added to the standard library in Python 3.3 as
a *note provisional API: 348. With the release of Python 3.4, this
qualification has been removed: *note ipaddress: a2. is now considered a
stable API, covered by the normal standard library requirements to
maintain backwards compatibility.
A new *note is_global: 86c. property is ‘True’ if an address is globally
routeable. (Contributed by Peter Moody in bpo-17400(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17400
File: python.info, Node: logging<5>, Next: marshal, Prev: ipaddress<3>, Up: Improved Modules<5>
1.5.4.25 logging
................
The *note TimedRotatingFileHandler: 86e. has a new `atTime' parameter
that can be used to specify the time of day when rollover should happen.
(Contributed by Ronald Oussoren in bpo-9556(1).)
*note SocketHandler: 86f. and *note DatagramHandler: 870. now support
Unix domain sockets (by setting `port' to ‘None’). (Contributed by
Vinay Sajip in commit ce46195b56a9.)
*note fileConfig(): 3a9. now accepts a *note
configparser.RawConfigParser: 871. subclass instance for the `fname'
parameter. This facilitates using a configuration file when logging
configuration is just a part of the overall application configuration,
or where the application modifies the configuration before passing it to
*note fileConfig(): 3a9. (Contributed by Vinay Sajip in bpo-16110(2).)
Logging configuration data received from a socket via the *note
logging.config.listen(): 872. function can now be validated before being
processed by supplying a verification function as the argument to the
new `verify' keyword argument. (Contributed by Vinay Sajip in
bpo-15452(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9556
(2) https://bugs.python.org/issue16110
(3) https://bugs.python.org/issue15452
File: python.info, Node: marshal, Next: mmap<2>, Prev: logging<5>, Up: Improved Modules<5>
1.5.4.26 marshal
................
The default *note marshal: b0. version has been bumped to 3. The code
implementing the new version restores the Python2 behavior of recording
only one copy of interned strings and preserving the interning on
deserialization, and extends this “one copy” ability to any object type
(including handling recursive references). This reduces both the size
of ‘.pyc’ files and the amount of memory a module occupies in memory
when it is loaded from a ‘.pyc’ (or ‘.pyo’) file. (Contributed by
Kristján Valur Jónsson in bpo-16475(1), with additional speedups by
Antoine Pitrou in bpo-19219(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16475
(2) https://bugs.python.org/issue19219
File: python.info, Node: mmap<2>, Next: multiprocessing<5>, Prev: marshal, Up: Improved Modules<5>
1.5.4.27 mmap
.............
mmap objects can now be *note weakref: 128.ed. (Contributed by Valerie
Lambert in bpo-4885(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4885
File: python.info, Node: multiprocessing<5>, Next: operator<2>, Prev: mmap<2>, Up: Improved Modules<5>
1.5.4.28 multiprocessing
........................
On Unix two new *note start methods: 876, ‘spawn’ and ‘forkserver’, have
been added for starting processes using *note multiprocessing: b7.
These make the mixing of processes with threads more robust, and the
‘spawn’ method matches the semantics that multiprocessing has always
used on Windows. New function *note get_all_start_methods(): 877.
reports all start methods available on the platform, *note
get_start_method(): 878. reports the current start method, and *note
set_start_method(): 879. sets the start method. (Contributed by Richard
Oudkerk in bpo-8713(1).)
*note multiprocessing: b7. also now has the concept of a ‘context’,
which determines how child processes are created. New function *note
get_context(): 87a. returns a context that uses a specified start
method. It has the same API as the *note multiprocessing: b7. module
itself, so you can use it to create *note Pool: 87b.s and other objects
that will operate within that context. This allows a framework and an
application or different parts of the same application to use
multiprocessing without interfering with each other. (Contributed by
Richard Oudkerk in bpo-18999(2).)
Except when using the old `fork' start method, child processes no longer
inherit unneeded handles/file descriptors from their parents (part of
bpo-8713(3)).
*note multiprocessing: b7. now relies on *note runpy: e2. (which
implements the ‘-m’ switch) to initialise ‘__main__’ appropriately in
child processes when using the ‘spawn’ or ‘forkserver’ start methods.
This resolves some edge cases where combining multiprocessing, the ‘-m’
command line switch, and explicit relative imports could cause obscure
failures in child processes. (Contributed by Nick Coghlan in
bpo-19946(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8713
(2) https://bugs.python.org/issue18999
(3) https://bugs.python.org/issue8713
(4) https://bugs.python.org/issue19946
File: python.info, Node: operator<2>, Next: os<6>, Prev: multiprocessing<5>, Up: Improved Modules<5>
1.5.4.29 operator
.................
New function *note length_hint(): 87d. provides an implementation of the
specification for how the *note __length_hint__(): 80c. special method
should be used, as part of the PEP 424(1) formal specification of this
language feature. (Contributed by Armin Ronacher in bpo-16148(2).)
There is now a pure-python version of the *note operator: c2. module
available for reference and for use by alternate implementations of
Python. (Contributed by Zachary Ware in bpo-16694(3).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0424
(2) https://bugs.python.org/issue16148
(3) https://bugs.python.org/issue16694
File: python.info, Node: os<6>, Next: pdb<3>, Prev: operator<2>, Up: Improved Modules<5>
1.5.4.30 os
...........
There are new functions to get and set the *note inheritable flag: 7fa.
of a file descriptor (*note os.get_inheritable(): 7fb, *note
os.set_inheritable(): 7fc.) or a Windows handle (*note
os.get_handle_inheritable(): 7fd, *note os.set_handle_inheritable():
7fe.).
New function *note cpu_count(): 87f. reports the number of CPUs
available on the platform on which Python is running (or ‘None’ if the
count can’t be determined). The *note multiprocessing.cpu_count(): 880.
function is now implemented in terms of this function). (Contributed by
Trent Nelson, Yogesh Chaudhari, Victor Stinner, and Charles-François
Natali in bpo-17914(1).)
*note os.path.samestat(): 881. is now available on the Windows platform
(and the *note os.path.samefile(): 882. implementation is now shared
between Unix and Windows). (Contributed by Brian Curtin in
bpo-11939(2).)
*note os.path.ismount(): 1fa. now recognizes volumes mounted below a
drive root on Windows. (Contributed by Tim Golden in bpo-9035(3).)
*note os.open(): 665. supports two new flags on platforms that provide
them, *note O_PATH: 883. (un-opened file descriptor), and *note
O_TMPFILE: 884. (unnamed temporary file; as of 3.4.0 release available
only on Linux systems with a kernel version of 3.11 or newer that have
uapi headers). (Contributed by Christian Heimes in bpo-18673(4) and
Benjamin Peterson, respectively.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17914
(2) https://bugs.python.org/issue11939
(3) https://bugs.python.org/issue9035
(4) https://bugs.python.org/issue18673
File: python.info, Node: pdb<3>, Next: pickle<4>, Prev: os<6>, Up: Improved Modules<5>
1.5.4.31 pdb
............
*note pdb: c9. has been enhanced to handle generators, *note yield: 18f,
and ‘yield from’ in a more useful fashion. This is especially helpful
when debugging *note asyncio: a. based programs. (Contributed by Andrew
Svetlov and Xavier de Gaye in bpo-16596(1).)
The ‘print’ command has been removed from *note pdb: c9, restoring
access to the Python *note print(): 886. function from the pdb command
line. Python2’s ‘pdb’ did not have a ‘print’ command; instead, entering
‘print’ executed the ‘print’ statement. In Python3 ‘print’ was
mistakenly made an alias for the pdb *note p: 887. command. ‘p’,
however, prints the ‘repr’ of its argument, not the ‘str’ like the
Python2 ‘print’ command did. Worse, the Python3 ‘pdb print’ command
shadowed the Python3 ‘print’ function, making it inaccessible at the
‘pdb’ prompt. (Contributed by Connor Osborn in bpo-18764(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16596
(2) https://bugs.python.org/issue18764
File: python.info, Node: pickle<4>, Next: plistlib<2>, Prev: pdb<3>, Up: Improved Modules<5>
1.5.4.32 pickle
...............
*note pickle: ca. now supports (but does not use by default) a new
pickle protocol, protocol 4. This new protocol addresses a number of
issues that were present in previous protocols, such as the
serialization of nested classes, very large strings and containers, and
classes whose *note __new__(): 889. method takes keyword-only arguments.
It also provides some efficiency improvements.
See also
........
PEP 3154(1) – Pickle protocol 4
PEP written by Antoine Pitrou and implemented by Alexandre
Vassalotti.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3154
File: python.info, Node: plistlib<2>, Next: poplib<2>, Prev: pickle<4>, Up: Improved Modules<5>
1.5.4.33 plistlib
.................
*note plistlib: cf. now has an API that is similar to the standard
pattern for stdlib serialization protocols, with new *note load(): 88b,
*note dump(): 88c, *note loads(): 88d, and *note dumps(): 88e.
functions. (The older API is now deprecated.) In addition to the
already supported XML plist format (*note FMT_XML: 88f.), it also now
supports the binary plist format (*note FMT_BINARY: 890.). (Contributed
by Ronald Oussoren and others in bpo-14455(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14455
File: python.info, Node: poplib<2>, Next: pprint<2>, Prev: plistlib<2>, Up: Improved Modules<5>
1.5.4.34 poplib
...............
Two new methods have been added to *note poplib: d0.: *note capa(): 892,
which returns the list of capabilities advertised by the POP server, and
*note stls(): 893, which switches a clear-text POP3 session into an
encrypted POP3 session if the POP server supports it. (Contributed by
Lorenzo Catucci in bpo-4473(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4473
File: python.info, Node: pprint<2>, Next: pty, Prev: poplib<2>, Up: Improved Modules<5>
1.5.4.35 pprint
...............
The *note pprint: d2. module’s *note PrettyPrinter: 895. class and its
*note pformat(): 896, and *note pprint(): 213. functions have a new
option, `compact', that controls how the output is formatted. Currently
setting `compact' to ‘True’ means that sequences will be printed with as
many sequence elements as will fit within `width' on each (indented)
line. (Contributed by Serhiy Storchaka in bpo-19132(1).)
Long strings are now wrapped using Python’s normal line continuation
syntax. (Contributed by Antoine Pitrou in bpo-17150(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19132
(2) https://bugs.python.org/issue17150
File: python.info, Node: pty, Next: pydoc<3>, Prev: pprint<2>, Up: Improved Modules<5>
1.5.4.36 pty
............
*note pty.spawn(): 898. now returns the status value from *note
os.waitpid(): 66d. on the child process, instead of ‘None’.
(Contributed by Gregory P. Smith.)
File: python.info, Node: pydoc<3>, Next: re<5>, Prev: pty, Up: Improved Modules<5>
1.5.4.37 pydoc
..............
The *note pydoc: d9. module is now based directly on the *note
inspect.signature(): 55b. introspection API, allowing it to provide
signature information for a wider variety of callable objects. This
change also means that ‘__wrapped__’ attributes are now taken into
account when displaying help information. (Contributed by Larry
Hastings in bpo-19674(1).)
The *note pydoc: d9. module no longer displays the ‘self’ parameter for
already bound methods. Instead, it aims to always display the exact
current signature of the supplied callable. (Contributed by Larry
Hastings in bpo-20710(2).)
In addition to the changes that have been made to *note pydoc: d9.
directly, its handling of custom ‘__dir__’ methods and various
descriptor behaviours has also been improved substantially by the
underlying changes in the *note inspect: a0. module.
As the *note help(): 572. builtin is based on *note pydoc: d9, the above
changes also affect the behaviour of *note help(): 572.
---------- Footnotes ----------
(1) https://bugs.python.org/issue19674
(2) https://bugs.python.org/issue20710
File: python.info, Node: re<5>, Next: resource, Prev: pydoc<3>, Up: Improved Modules<5>
1.5.4.38 re
...........
New *note fullmatch(): 89b. function and ‘regex.fullmatch()’ method
anchor the pattern at both ends of the string to match. This provides a
way to be explicit about the goal of the match, which avoids a class of
subtle bugs where ‘$’ characters get lost during code changes or the
addition of alternatives to an existing regular expression.
(Contributed by Matthew Barnett in bpo-16203(1).)
The repr of *note regex objects: 89c. now includes the pattern and the
flags; the repr of *note match objects: 89d. now includes the start,
end, and the part of the string that matched. (Contributed by Hugo
Lopes Tavares and Serhiy Storchaka in bpo-13592(2) and bpo-17087(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16203
(2) https://bugs.python.org/issue13592
(3) https://bugs.python.org/issue17087
File: python.info, Node: resource, Next: select, Prev: re<5>, Up: Improved Modules<5>
1.5.4.39 resource
.................
New *note prlimit(): 89f. function, available on Linux platforms with a
kernel version of 2.6.36 or later and glibc of 2.13 or later, provides
the ability to query or set the resource limits for processes other than
the one making the call. (Contributed by Christian Heimes in
bpo-16595(1).)
On Linux kernel version 2.6.36 or later, there are also some new Linux
specific constants: *note RLIMIT_MSGQUEUE: 8a0, *note RLIMIT_NICE: 8a1,
*note RLIMIT_RTPRIO: 8a2, *note RLIMIT_RTTIME: 8a3, and *note
RLIMIT_SIGPENDING: 8a4. (Contributed by Christian Heimes in
bpo-19324(2).)
On FreeBSD version 9 and later, there some new FreeBSD specific
constants: *note RLIMIT_SBSIZE: 8a5, *note RLIMIT_SWAP: 8a6, and *note
RLIMIT_NPTS: 8a7. (Contributed by Claudiu Popa in bpo-19343(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16595
(2) https://bugs.python.org/issue19324
(3) https://bugs.python.org/issue19343
File: python.info, Node: select, Next: shelve, Prev: resource, Up: Improved Modules<5>
1.5.4.40 select
...............
*note epoll: 8a9. objects now support the context management protocol.
When used in a *note with: 6e9. statement, the *note close(): 8aa.
method will be called automatically at the end of the block.
(Contributed by Serhiy Storchaka in bpo-16488(1).)
*note devpoll: 8ab. objects now have *note fileno(): 8ac. and *note
close(): 8ad. methods, as well as a new attribute *note closed: 8ae.
(Contributed by Victor Stinner in bpo-18794(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16488
(2) https://bugs.python.org/issue18794
File: python.info, Node: shelve, Next: shutil<3>, Prev: select, Up: Improved Modules<5>
1.5.4.41 shelve
...............
*note Shelf: 8b0. instances may now be used in *note with: 6e9.
statements, and will be automatically closed at the end of the ‘with’
block. (Contributed by Filip Gruszczyński in bpo-13896(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13896
File: python.info, Node: shutil<3>, Next: smtpd<2>, Prev: shelve, Up: Improved Modules<5>
1.5.4.42 shutil
...............
*note copyfile(): 258. now raises a specific *note Error: 8b2. subclass,
*note SameFileError: 8b3, when the source and destination are the same
file, which allows an application to take appropriate action on this
specific error. (Contributed by Atsuo Ishimoto and Hynek Schlawack in
bpo-1492704(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1492704
File: python.info, Node: smtpd<2>, Next: smtplib<2>, Prev: shutil<3>, Up: Improved Modules<5>
1.5.4.43 smtpd
..............
The *note SMTPServer: 602. and *note SMTPChannel: 601. classes now
accept a `map' keyword argument which, if specified, is passed in to
*note asynchat.async_chat: 8b5. as its `map' argument. This allows an
application to avoid affecting the global socket map. (Contributed by
Vinay Sajip in bpo-11959(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11959
File: python.info, Node: smtplib<2>, Next: socket<6>, Prev: smtpd<2>, Up: Improved Modules<5>
1.5.4.44 smtplib
................
*note SMTPException: 8b7. is now a subclass of *note OSError: 1d3, which
allows both socket level errors and SMTP protocol level errors to be
caught in one try/except statement by code that only cares whether or
not an error occurred. (Contributed by Ned Jackson Lovely in
bpo-2118(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue2118
File: python.info, Node: socket<6>, Next: sqlite3<4>, Prev: smtplib<2>, Up: Improved Modules<5>
1.5.4.45 socket
...............
The socket module now supports the *note CAN_BCM: 8b9. protocol on
platforms that support it. (Contributed by Brian Thorne in
bpo-15359(1).)
Socket objects have new methods to get or set their *note inheritable
flag: 7fa, *note get_inheritable(): 7ff. and *note set_inheritable():
800.
The ‘socket.AF_*’ and ‘socket.SOCK_*’ constants are now enumeration
values using the new *note enum: 7b. module. This allows meaningful
names to be printed during debugging, instead of integer “magic
numbers”.
The *note AF_LINK: 8ba. constant is now available on BSD and OSX.
*note inet_pton(): 8bb. and *note inet_ntop(): 8bc. are now supported on
Windows. (Contributed by Atsuo Ishimoto in bpo-7171(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15359
(2) https://bugs.python.org/issue7171
File: python.info, Node: sqlite3<4>, Next: ssl<7>, Prev: socket<6>, Up: Improved Modules<5>
1.5.4.46 sqlite3
................
A new boolean parameter to the *note connect(): 3da. function, `uri',
can be used to indicate that the `database' parameter is a ‘uri’ (see
the SQLite URI documentation(1)). (Contributed by poq in bpo-13773(2).)
---------- Footnotes ----------
(1) https://www.sqlite.org/uri.html
(2) https://bugs.python.org/issue13773
File: python.info, Node: ssl<7>, Next: stat, Prev: sqlite3<4>, Up: Improved Modules<5>
1.5.4.47 ssl
............
*note PROTOCOL_TLSv1_1: 8bf. and *note PROTOCOL_TLSv1_2: 8c0. (TLSv1.1
and TLSv1.2 support) have been added; support for these protocols is
only available if Python is linked with OpenSSL 1.0.1 or later.
(Contributed by Michele Orrù and Antoine Pitrou in bpo-16692(1).) New
function *note create_default_context(): 8c1. provides a standard way to
obtain an *note SSLContext: 3e4. whose settings are intended to be a
reasonable balance between compatibility and security. These settings
are more stringent than the defaults provided by the *note SSLContext:
3e4. constructor, and may be adjusted in the future, without prior
deprecation, if best-practice security requirements change. The new
recommended best practice for using stdlib libraries that support SSL is
to use *note create_default_context(): 8c1. to obtain an *note
SSLContext: 3e4. object, modify it if needed, and then pass it as the
`context' argument of the appropriate stdlib API. (Contributed by
Christian Heimes in bpo-19689(2).)
*note SSLContext: 3e4. method *note load_verify_locations(): 7eb.
accepts a new optional argument `cadata', which can be used to provide
PEM or DER encoded certificates directly via strings or bytes,
respectively. (Contributed by Christian Heimes in bpo-18138(3).)
New function *note get_default_verify_paths(): 8c2. returns a named
tuple of the paths and environment variables that the *note
set_default_verify_paths(): 8c3. method uses to set OpenSSL’s default
‘cafile’ and ‘capath’. This can be an aid in debugging default
verification issues. (Contributed by Christian Heimes in bpo-18143(4).)
*note SSLContext: 3e4. has a new method, *note cert_store_stats(): 8c4,
that reports the number of loaded ‘X.509’ certs, ‘X.509 CA’ certs, and
certificate revocation lists (‘crl’s), as well as a *note
get_ca_certs(): 8c5. method that returns a list of the loaded ‘CA’
certificates. (Contributed by Christian Heimes in bpo-18147(5).)
If OpenSSL 0.9.8 or later is available, *note SSLContext: 3e4. has a new
attribute *note verify_flags: 8c6. that can be used to control the
certificate verification process by setting it to some combination of
the new constants *note VERIFY_DEFAULT: 8c7, *note
VERIFY_CRL_CHECK_LEAF: 8c8, *note VERIFY_CRL_CHECK_CHAIN: 8c9, or *note
VERIFY_X509_STRICT: 8ca. OpenSSL does not do any CRL verification by
default. (Contributed by Christien Heimes in bpo-8813(6).)
New *note SSLContext: 3e4. method *note load_default_certs(): 8cb. loads
a set of default “certificate authority” (CA) certificates from default
locations, which vary according to the platform. It can be used to load
both TLS web server authentication certificates (‘purpose=’*note
SERVER_AUTH: 8cc.) for a client to use to verify a server, and
certificates for a server to use in verifying client certificates
(‘purpose=’*note CLIENT_AUTH: 8cd.). (Contributed by Christian Heimes
in bpo-19292(7).) Two new windows-only functions, *note
enum_certificates(): 8ce. and *note enum_crls(): 8cf. provide the
ability to retrieve certificates, certificate information, and CRLs from
the Windows cert store. (Contributed by Christian Heimes in
bpo-17134(8).) Support for server-side SNI (Server Name Indication)
using the new *note ssl.SSLContext.set_servername_callback(): 8d0.
method. (Contributed by Daniel Black in bpo-8109(9).)
The dictionary returned by *note SSLSocket.getpeercert(): 8d1. contains
additional ‘X509v3’ extension items: ‘crlDistributionPoints’,
‘calIssuers’, and ‘OCSP’ URIs. (Contributed by Christian Heimes in
bpo-18379(10).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16692
(2) https://bugs.python.org/issue19689
(3) https://bugs.python.org/issue18138
(4) https://bugs.python.org/issue18143
(5) https://bugs.python.org/issue18147
(6) https://bugs.python.org/issue8813
(7) https://bugs.python.org/issue19292
(8) https://bugs.python.org/issue17134
(9) https://bugs.python.org/issue8109
(10) https://bugs.python.org/issue18379
File: python.info, Node: stat, Next: struct<2>, Prev: ssl<7>, Up: Improved Modules<5>
1.5.4.48 stat
.............
The *note stat: f4. module is now backed by a C implementation in
‘_stat’. A C implementation is required as most of the values aren’t
standardized and are platform-dependent. (Contributed by Christian
Heimes in bpo-11016(1).)
The module supports new *note ST_MODE: 8d3. flags, *note S_IFDOOR: 8d4,
*note S_IFPORT: 8d5, and *note S_IFWHT: 8d6. (Contributed by Christian
Hiemes in bpo-11016(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11016
(2) https://bugs.python.org/issue11016
File: python.info, Node: struct<2>, Next: subprocess<4>, Prev: stat, Up: Improved Modules<5>
1.5.4.49 struct
...............
New function *note iter_unpack: 8d8. and a new *note
struct.Struct.iter_unpack(): 8d9. method on compiled formats provide
streamed unpacking of a buffer containing repeated instances of a given
format of data. (Contributed by Antoine Pitrou in bpo-17804(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17804
File: python.info, Node: subprocess<4>, Next: sunau<2>, Prev: struct<2>, Up: Improved Modules<5>
1.5.4.50 subprocess
...................
*note check_output(): 8db. now accepts an `input' argument that can be
used to provide the contents of ‘stdin’ for the command that is run.
(Contributed by Zack Weinberg in bpo-16624(1).)
‘getstatus()’ and *note getstatusoutput(): 8dc. now work on Windows.
This change was actually inadvertently made in 3.3.4. (Contributed by
Tim Golden in bpo-10197(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16624
(2) https://bugs.python.org/issue10197
File: python.info, Node: sunau<2>, Next: sys<6>, Prev: subprocess<4>, Up: Improved Modules<5>
1.5.4.51 sunau
..............
The ‘getparams()’ method now returns a namedtuple rather than a plain
tuple. (Contributed by Claudiu Popa in bpo-18901(1).)
*note sunau.open(): 472. now supports the context management protocol:
when used in a *note with: 6e9. block, the ‘close’ method of the
returned object will be called automatically at the end of the block.
(Contributed by Serhiy Storchaka in bpo-18878(2).)
*note AU_write.setsampwidth(): 8de. now supports 24 bit samples, thus
adding support for writing 24 sample using the module. (Contributed by
Serhiy Storchaka in bpo-19261(3).)
The *note writeframesraw(): 8df. and *note writeframes(): 8e0. methods
now accept any *note bytes-like object: 5e8. (Contributed by Serhiy
Storchaka in bpo-8311(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18901
(2) https://bugs.python.org/issue18878
(3) https://bugs.python.org/issue19261
(4) https://bugs.python.org/issue8311
File: python.info, Node: sys<6>, Next: tarfile<3>, Prev: sunau<2>, Up: Improved Modules<5>
1.5.4.52 sys
............
New function *note sys.getallocatedblocks(): 8e2. returns the current
number of blocks allocated by the interpreter. (In CPython with the
default ‘--with-pymalloc’ setting, this is allocations made through the
*note PyObject_Malloc(): 508. API.) This can be useful for tracking
memory leaks, especially if automated via a test suite. (Contributed by
Antoine Pitrou in bpo-13390(1).)
When the Python interpreter starts in *note interactive mode: 8e3, it
checks for an *note __interactivehook__: 8e4. attribute on the *note
sys: fd. module. If the attribute exists, its value is called with no
arguments just before interactive mode is started. The check is made
after the *note PYTHONSTARTUP: 8e5. file is read, so it can be set
there. The *note site: eb. module *note sets it: 8e6. to a function
that enables tab completion and history saving (in ‘~/.python-history’)
if the platform supports *note readline: de. If you do not want this
(new) behavior, you can override it in *note PYTHONSTARTUP: 8e5,
‘sitecustomize’, or ‘usercustomize’ by deleting this attribute from
*note sys: fd. (or setting it to some other callable). (Contributed by
Éric Araujo and Antoine Pitrou in bpo-5845(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13390
(2) https://bugs.python.org/issue5845
File: python.info, Node: tarfile<3>, Next: textwrap, Prev: sys<6>, Up: Improved Modules<5>
1.5.4.53 tarfile
................
The *note tarfile: 101. module now supports a simple *note Command-Line
Interface: 8e8. when called as a script directly or via ‘-m’. This can
be used to create and extract tarfile archives. (Contributed by Berker
Peksag in bpo-13477(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13477
File: python.info, Node: textwrap, Next: threading<4>, Prev: tarfile<3>, Up: Improved Modules<5>
1.5.4.54 textwrap
.................
The *note TextWrapper: 8ea. class has two new attributes/constructor
arguments: *note max_lines: 8eb, which limits the number of lines in the
output, and *note placeholder: 8ec, which is a string that will appear
at the end of the output if it has been truncated because of
`max_lines'. Building on these capabilities, a new convenience function
*note shorten(): 8ed. collapses all of the whitespace in the input to
single spaces and produces a single line of a given `width' that ends
with the `placeholder' (by default, ‘[...]’). (Contributed by Antoine
Pitrou and Serhiy Storchaka in bpo-18585(1) and bpo-18725(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18585
(2) https://bugs.python.org/issue18725
File: python.info, Node: threading<4>, Next: traceback<3>, Prev: textwrap, Up: Improved Modules<5>
1.5.4.55 threading
..................
The *note Thread: 235. object representing the main thread can be
obtained from the new *note main_thread(): 8ef. function. In normal
conditions this will be the thread from which the Python interpreter was
started. (Contributed by Andrew Svetlov in bpo-18882(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue18882
File: python.info, Node: traceback<3>, Next: types<3>, Prev: threading<4>, Up: Improved Modules<5>
1.5.4.56 traceback
..................
A new *note traceback.clear_frames(): 8f1. function takes a traceback
object and clears the local variables in all of the frames it
references, reducing the amount of memory consumed. (Contributed by
Andrew Kuchling in bpo-1565525(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1565525
File: python.info, Node: types<3>, Next: urllib<2>, Prev: traceback<3>, Up: Improved Modules<5>
1.5.4.57 types
..............
A new *note DynamicClassAttribute(): 8f3. descriptor provides a way to
define an attribute that acts normally when looked up through an
instance object, but which is routed to the `class' ‘__getattr__’ when
looked up through the class. This allows one to have properties active
on a class, and have virtual attributes on the class with the same name
(see ‘Enum’ for an example). (Contributed by Ethan Furman in
bpo-19030(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19030
File: python.info, Node: urllib<2>, Next: unittest<4>, Prev: types<3>, Up: Improved Modules<5>
1.5.4.58 urllib
...............
*note urllib.request: 120. now supports ‘data:’ URLs via the *note
DataHandler: 8f5. class. (Contributed by Mathias Panzenböck in
bpo-16423(1).)
The http method that will be used by a *note Request: 8f6. class can now
be specified by setting a *note method: 8f7. class attribute on the
subclass. (Contributed by Jason R Coombs in bpo-18978(2).)
*note Request: 8f6. objects are now reusable: if the *note full_url:
8f8. or *note data: 8f9. attributes are modified, all relevant internal
properties are updated. This means, for example, that it is now
possible to use the same *note Request: 8f6. object in more than one
*note OpenerDirector.open(): 8fa. call with different `data' arguments,
or to modify a *note Request: 8f6.’s ‘url’ rather than recomputing it
from scratch. There is also a new *note remove_header(): 8fb. method
that can be used to remove headers from a *note Request: 8f6.
(Contributed by Alexey Kachayev in bpo-16464(3), Daniel Wozniak in
bpo-17485(4), and Damien Brecht and Senthil Kumaran in bpo-17272(5).)
*note HTTPError: 8fc. objects now have a *note headers: 8fd. attribute
that provides access to the HTTP response headers associated with the
error. (Contributed by Berker Peksag in bpo-15701(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16423
(2) https://bugs.python.org/issue18978
(3) https://bugs.python.org/issue16464
(4) https://bugs.python.org/issue17485
(5) https://bugs.python.org/issue17272
(6) https://bugs.python.org/issue15701
File: python.info, Node: unittest<4>, Next: venv<4>, Prev: urllib<2>, Up: Improved Modules<5>
1.5.4.59 unittest
.................
The *note TestCase: 8ff. class has a new method, *note subTest(): 900,
that produces a context manager whose *note with: 6e9. block becomes a
“sub-test”. This context manager allows a test method to dynamically
generate subtests by, say, calling the ‘subTest’ context manager inside
a loop. A single test method can thereby produce an indefinite number
of separately-identified and separately-counted tests, all of which will
run even if one or more of them fail. For example:
class NumbersTest(unittest.TestCase):
def test_even(self):
for i in range(6):
with self.subTest(i=i):
self.assertEqual(i % 2, 0)
will result in six subtests, each identified in the unittest verbose
output with a label consisting of the variable name ‘i’ and a particular
value for that variable (‘i=0’, ‘i=1’, etc). See *note Distinguishing
test iterations using subtests: 901. for the full version of this
example. (Contributed by Antoine Pitrou in bpo-16997(1).)
*note unittest.main(): 902. now accepts an iterable of test names for
`defaultTest', where previously it only accepted a single test name as a
string. (Contributed by Jyrki Pulliainen in bpo-15132(2).)
If *note SkipTest: 903. is raised during test discovery (that is, at the
module level in the test file), it is now reported as a skip instead of
an error. (Contributed by Zach Ware in bpo-16935(3).)
*note discover(): 904. now sorts the discovered files to provide
consistent test ordering. (Contributed by Martin Melin and Jeff Ramnani
in bpo-16709(4).)
*note TestSuite: 6ce. now drops references to tests as soon as the test
has been run, if the test is successful. On Python interpreters that do
garbage collection, this allows the tests to be garbage collected if
nothing else is holding a reference to the test. It is possible to
override this behavior by creating a *note TestSuite: 6ce. subclass that
defines a custom ‘_removeTestAtIndex’ method. (Contributed by Tom
Wardill, Matt McClure, and Andrew Svetlov in bpo-11798(5).)
A new test assertion context-manager, *note assertLogs(): 905, will
ensure that a given block of code emits a log message using the *note
logging: aa. module. By default the message can come from any logger
and have a priority of ‘INFO’ or higher, but both the logger name and an
alternative minimum logging level may be specified. The object returned
by the context manager can be queried for the *note LogRecord: 906.s
and/or formatted messages that were logged. (Contributed by Antoine
Pitrou in bpo-18937(6).)
Test discovery now works with namespace packages (Contributed by Claudiu
Popa in bpo-17457(7).)
*note unittest.mock: 11c. objects now inspect their specification
signatures when matching calls, which means an argument can now be
matched by either position or name, instead of only by position.
(Contributed by Antoine Pitrou in bpo-17015(8).)
‘mock_open()’ objects now have ‘readline’ and ‘readlines’ methods.
(Contributed by Toshio Kuratomi in bpo-17467(9).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16997
(2) https://bugs.python.org/issue15132
(3) https://bugs.python.org/issue16935
(4) https://bugs.python.org/issue16709
(5) https://bugs.python.org/issue11798
(6) https://bugs.python.org/issue18937
(7) https://bugs.python.org/issue17457
(8) https://bugs.python.org/issue17015
(9) https://bugs.python.org/issue17467
File: python.info, Node: venv<4>, Next: wave<2>, Prev: unittest<4>, Up: Improved Modules<5>
1.5.4.60 venv
.............
*note venv: 125. now includes activation scripts for the ‘csh’ and
‘fish’ shells. (Contributed by Andrew Svetlov in bpo-15417(1).)
*note EnvBuilder: 908. and the *note create(): 909. convenience function
take a new keyword argument `with_pip', which defaults to ‘False’, that
controls whether or not *note EnvBuilder: 908. ensures that ‘pip’ is
installed in the virtual environment. (Contributed by Nick Coghlan in
bpo-19552(2) as part of the PEP 453(3) implementation.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15417
(2) https://bugs.python.org/issue19552
(3) https://www.python.org/dev/peps/pep-0453
File: python.info, Node: wave<2>, Next: weakref<2>, Prev: venv<4>, Up: Improved Modules<5>
1.5.4.61 wave
.............
The ‘getparams()’ method now returns a namedtuple rather than a plain
tuple. (Contributed by Claudiu Popa in bpo-17487(1).)
*note wave.open(): 476. now supports the context management protocol.
(Contributed by Claudiu Popa in bpo-17616(2).)
*note wave: 127. can now *note write output to unseekable files: 90b.
(Contributed by David Jones, Guilherme Polo, and Serhiy Storchaka in
bpo-5202(3).)
The *note writeframesraw(): 90c. and *note writeframes(): 90d. methods
now accept any *note bytes-like object: 5e8. (Contributed by Serhiy
Storchaka in bpo-8311(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17487
(2) https://bugs.python.org/issue17616
(3) https://bugs.python.org/issue5202
(4) https://bugs.python.org/issue8311
File: python.info, Node: weakref<2>, Next: xml etree<2>, Prev: wave<2>, Up: Improved Modules<5>
1.5.4.62 weakref
................
New *note WeakMethod: 90f. class simulates weak references to bound
methods. (Contributed by Antoine Pitrou in bpo-14631(1).)
New *note finalize: 29d. class makes it possible to register a callback
to be invoked when an object is garbage collected, without needing to
carefully manage the lifecycle of the weak reference itself.
(Contributed by Richard Oudkerk in bpo-15528(2).)
The callback, if any, associated with a *note ref: 910. is now exposed
via the *note __callback__: 911. attribute. (Contributed by Mark
Dickinson in bpo-17643(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14631
(2) https://bugs.python.org/issue15528
(3) https://bugs.python.org/issue17643
File: python.info, Node: xml etree<2>, Next: zipfile<4>, Prev: weakref<2>, Up: Improved Modules<5>
1.5.4.63 xml.etree
..................
A new parser, *note XMLPullParser: 913, allows a non-blocking
applications to parse XML documents. An example can be seen at *note
Pull API for non-blocking parsing: 914. (Contributed by Antoine Pitrou
in bpo-17741(1).)
The *note xml.etree.ElementTree: 137. *note tostring(): 915. and *note
tostringlist(): 916. functions, and the *note ElementTree: 917. *note
write(): 918. method, now have a `short_empty_elements' *note
keyword-only parameter: 2ac. providing control over whether elements
with no content are written in abbreviated (‘ ’) or expanded
(‘ ’) form. (Contributed by Ariel Poliak and Serhiy Storchaka
in bpo-14377(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17741
(2) https://bugs.python.org/issue14377
File: python.info, Node: zipfile<4>, Prev: xml etree<2>, Up: Improved Modules<5>
1.5.4.64 zipfile
................
The *note writepy(): 91a. method of the *note PyZipFile: 91b. class has
a new `filterfunc' option that can be used to control which directories
and files are added to the archive. For example, this could be used to
exclude test files from the archive. (Contributed by Christian Tismer
in bpo-19274(1).)
The `allowZip64' parameter to *note ZipFile: 41f. and ‘PyZipfile’ is now
‘True’ by default. (Contributed by William Mallard in bpo-17201(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue19274
(2) https://bugs.python.org/issue17201
File: python.info, Node: CPython Implementation Changes, Next: Deprecated<4>, Prev: Improved Modules<5>, Up: What’s New In Python 3 4
1.5.5 CPython Implementation Changes
------------------------------------
* Menu:
* PEP 445; Customization of CPython Memory Allocators: PEP 445 Customization of CPython Memory Allocators.
* PEP 442; Safe Object Finalization: PEP 442 Safe Object Finalization.
* PEP 456; Secure and Interchangeable Hash Algorithm: PEP 456 Secure and Interchangeable Hash Algorithm.
* PEP 436; Argument Clinic: PEP 436 Argument Clinic.
* Other Build and C API Changes::
* Other Improvements: Other Improvements<2>.
* Significant Optimizations::
File: python.info, Node: PEP 445 Customization of CPython Memory Allocators, Next: PEP 442 Safe Object Finalization, Up: CPython Implementation Changes
1.5.5.1 PEP 445: Customization of CPython Memory Allocators
...........................................................
PEP 445(1) adds new C level interfaces to customize memory allocation in
the CPython interpreter.
See also
........
PEP 445(2) – Add new APIs to customize Python memory allocators
PEP written and implemented by Victor Stinner.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0445
(2) https://www.python.org/dev/peps/pep-0445
File: python.info, Node: PEP 442 Safe Object Finalization, Next: PEP 456 Secure and Interchangeable Hash Algorithm, Prev: PEP 445 Customization of CPython Memory Allocators, Up: CPython Implementation Changes
1.5.5.2 PEP 442: Safe Object Finalization
.........................................
PEP 442(1) removes the current limitations and quirks of object
finalization in CPython. With it, objects with *note __del__(): 91f.
methods, as well as generators with *note finally: 182. clauses, can be
finalized when they are part of a reference cycle.
As part of this change, module globals are no longer forcibly set to
*note None: 157. during interpreter shutdown in most cases, instead
relying on the normal operation of the cyclic garbage collector. This
avoids a whole class of interpreter-shutdown-time errors, usually
involving ‘__del__’ methods, that have plagued Python since the cyclic
GC was first introduced.
See also
........
PEP 442(2) – Safe object finalization
PEP written and implemented by Antoine Pitrou.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0442
(2) https://www.python.org/dev/peps/pep-0442
File: python.info, Node: PEP 456 Secure and Interchangeable Hash Algorithm, Next: PEP 436 Argument Clinic, Prev: PEP 442 Safe Object Finalization, Up: CPython Implementation Changes
1.5.5.3 PEP 456: Secure and Interchangeable Hash Algorithm
..........................................................
PEP 456(1) follows up on earlier security fix work done on Python’s hash
algorithm to address certain DOS attacks to which public facing APIs
backed by dictionary lookups may be subject. (See bpo-14621(2) for the
start of the current round of improvements.) The PEP unifies CPython’s
hash code to make it easier for a packager to substitute a different
hash algorithm, and switches Python’s default implementation to a
SipHash implementation on platforms that have a 64 bit data type. Any
performance differences in comparison with the older FNV algorithm are
trivial.
The PEP adds additional fields to the *note sys.hash_info: 921. named
tuple to describe the hash algorithm in use by the currently executing
binary. Otherwise, the PEP does not alter any existing CPython APIs.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0456
(2) https://bugs.python.org/issue14621
File: python.info, Node: PEP 436 Argument Clinic, Next: Other Build and C API Changes, Prev: PEP 456 Secure and Interchangeable Hash Algorithm, Up: CPython Implementation Changes
1.5.5.4 PEP 436: Argument Clinic
................................
“Argument Clinic” ( PEP 436(1)) is now part of the CPython build process
and can be used to simplify the process of defining and maintaining
accurate signatures for builtins and standard library extension modules
implemented in C.
Some standard library extension modules have been converted to use
Argument Clinic in Python 3.4, and *note pydoc: d9. and *note inspect:
a0. have been updated accordingly.
It is expected that signature metadata for programmatic introspection
will be added to additional callables implemented in C as part of Python
3.4 maintenance releases.
Note: The Argument Clinic PEP is not fully up to date with the
state of the implementation. This has been deemed acceptable by
the release manager and core development team in this case, as
Argument Clinic will not be made available as a public API for
third party use in Python 3.4.
See also
........
PEP 436(2) – The Argument Clinic DSL
PEP written and implemented by Larry Hastings.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0436
(2) https://www.python.org/dev/peps/pep-0436
File: python.info, Node: Other Build and C API Changes, Next: Other Improvements<2>, Prev: PEP 436 Argument Clinic, Up: CPython Implementation Changes
1.5.5.5 Other Build and C API Changes
.....................................
* The new *note PyType_GetSlot(): 924. function has been added to the
stable ABI, allowing retrieval of function pointers from named type
slots when using the limited API. (Contributed by Martin von Löwis
in bpo-17162(1).)
* The new *note Py_SetStandardStreamEncoding(): 925.
pre-initialization API allows applications embedding the CPython
interpreter to reliably force a particular encoding and error
handler for the standard streams. (Contributed by Bastien Montagne
and Nick Coghlan in bpo-16129(2).)
* Most Python C APIs that don’t mutate string arguments are now
correctly marked as accepting ‘const char *’ rather than ‘char *’.
(Contributed by Serhiy Storchaka in bpo-1772673(3).)
* A new shell version of ‘python-config’ can be used even when a
python interpreter is not available (for example, in cross
compilation scenarios).
* *note PyUnicode_FromFormat(): 7cb. now supports width and precision
specifications for ‘%s’, ‘%A’, ‘%U’, ‘%V’, ‘%S’, and ‘%R’.
(Contributed by Ysj Ray and Victor Stinner in bpo-7330(4).)
* New function *note PyStructSequence_InitType2(): 926. supplements
the existing *note PyStructSequence_InitType(): 927. function. The
difference is that it returns ‘0’ on success and ‘-1’ on failure.
* The CPython source can now be compiled using the address sanity
checking features of recent versions of GCC and clang: the false
alarms in the small object allocator have been silenced.
(Contributed by Dhiru Kholia in bpo-18596(5).)
* The Windows build now uses Address Space Layout Randomization(6)
and Data Execution Prevention(7). (Contributed by Christian Heimes
in bpo-16632(8).)
* New function *note PyObject_LengthHint(): 928. is the C API
equivalent of *note operator.length_hint(): 87d. (Contributed by
Armin Ronacher in bpo-16148(9).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue17162
(2) https://bugs.python.org/issue16129
(3) https://bugs.python.org/issue1772673
(4) https://bugs.python.org/issue7330
(5) https://bugs.python.org/issue18596
(6) https://en.wikipedia.org/wiki/Address_space_layout_randomization
(7) https://en.wikipedia.org/wiki/Data_Execution_Prevention
(8) https://bugs.python.org/issue16632
(9) https://bugs.python.org/issue16148
File: python.info, Node: Other Improvements<2>, Next: Significant Optimizations, Prev: Other Build and C API Changes, Up: CPython Implementation Changes
1.5.5.6 Other Improvements
..........................
* The *note python: 92b. command has a new *note option: 92c, ‘-I’,
which causes it to run in “isolated mode”, which means that *note
sys.path: 488. contains neither the script’s directory nor the
user’s ‘site-packages’ directory, and all ‘PYTHON*’ environment
variables are ignored (it implies both ‘-s’ and ‘-E’). Other
restrictions may also be applied in the future, with the goal being
to isolate the execution of a script from the user’s environment.
This is appropriate, for example, when Python is used to run a
system script. On most POSIX systems it can and should be used in
the ‘#!’ line of system scripts. (Contributed by Christian Heimes
in bpo-16499(1).)
* Tab-completion is now enabled by default in the interactive
interpreter on systems that support *note readline: de. History is
also enabled by default, and is written to (and read from) the file
‘~/.python-history’. (Contributed by Antoine Pitrou and Éric
Araujo in bpo-5845(2).)
* Invoking the Python interpreter with ‘--version’ now outputs the
version to standard output instead of standard error
(bpo-18338(3)). Similar changes were made to *note argparse: 6.
(bpo-18920(4)) and other modules that have script-like invocation
capabilities (bpo-18922(5)).
* The CPython Windows installer now adds ‘.py’ to the ‘PATHEXT’
variable when extensions are registered, allowing users to run a
python script at the windows command prompt by just typing its name
without the ‘.py’ extension. (Contributed by Paul Moore in
bpo-18569(6).)
* A new ‘make’ target coverage-report(7) will build python, run the
test suite, and generate an HTML coverage report for the C codebase
using ‘gcov’ and lcov(8).
* The ‘-R’ option to the *note python regression test suite: 92d. now
also checks for memory allocation leaks, using *note
sys.getallocatedblocks(): 8e2. (Contributed by Antoine Pitrou in
bpo-13390(9).)
* ‘python -m’ now works with namespace packages.
* The *note stat: f4. module is now implemented in C, which means it
gets the values for its constants from the C header files, instead
of having the values hard-coded in the python module as was
previously the case.
* Loading multiple python modules from a single OS module (‘.so’,
‘.dll’) now works correctly (previously it silently returned the
first python module in the file). (Contributed by Václav Šmilauer
in bpo-16421(10).)
* A new opcode, *note LOAD_CLASSDEREF: 92e, has been added to fix a
bug in the loading of free variables in class bodies that could be
triggered by certain uses of *note __prepare__: 4fd. (Contributed
by Benjamin Peterson in bpo-17853(11).)
* A number of MemoryError-related crashes were identified and fixed
by Victor Stinner using his PEP 445(12)-based ‘pyfailmalloc’ tool
(bpo-18408(13), bpo-18520(14)).
* The ‘pyvenv’ command now accepts a ‘--copies’ option to use copies
rather than symlinks even on systems where symlinks are the
default. (Contributed by Vinay Sajip in bpo-18807(15).)
* The ‘pyvenv’ command also accepts a ‘--without-pip’ option to
suppress the otherwise-automatic bootstrapping of pip into the
virtual environment. (Contributed by Nick Coghlan in bpo-19552(16)
as part of the PEP 453(17) implementation.)
* The encoding name is now optional in the value set for the *note
PYTHONIOENCODING: 92f. environment variable. This makes it
possible to set just the error handler, without changing the
default encoding. (Contributed by Serhiy Storchaka in
bpo-18818(18).)
* The *note bz2: 14, *note lzma: ad, and *note gzip: 8c. module
‘open’ functions now support ‘x’ (exclusive creation) mode.
(Contributed by Tim Heaney and Vajrasky Kok in bpo-19201(19),
bpo-19222(20), and bpo-19223(21).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue16499
(2) https://bugs.python.org/issue5845
(3) https://bugs.python.org/issue18338
(4) https://bugs.python.org/issue18920
(5) https://bugs.python.org/issue18922
(6) https://bugs.python.org/issue18569
(7)
https://devguide.python.org/coverage/#measuring-coverage-of-c-code-with-gcov-and-lcov
(8) http://ltp.sourceforge.net/coverage/lcov.php
(9) https://bugs.python.org/issue13390
(10) https://bugs.python.org/issue16421
(11) https://bugs.python.org/issue17853
(12) https://www.python.org/dev/peps/pep-0445
(13) https://bugs.python.org/issue18408
(14) https://bugs.python.org/issue18520
(15) https://bugs.python.org/issue18807
(16) https://bugs.python.org/issue19552
(17) https://www.python.org/dev/peps/pep-0453
(18) https://bugs.python.org/issue18818
(19) https://bugs.python.org/issue19201
(20) https://bugs.python.org/issue19222
(21) https://bugs.python.org/issue19223
File: python.info, Node: Significant Optimizations, Prev: Other Improvements<2>, Up: CPython Implementation Changes
1.5.5.7 Significant Optimizations
.................................
* The UTF-32 decoder is now 3x to 4x faster. (Contributed by Serhiy
Storchaka in bpo-14625(1).)
* The cost of hash collisions for sets is now reduced. Each hash
table probe now checks a series of consecutive, adjacent key/hash
pairs before continuing to make random probes through the hash
table. This exploits cache locality to make collision resolution
less expensive. The collision resolution scheme can be described
as a hybrid of linear probing and open addressing. The number of
additional linear probes defaults to nine. This can be changed at
compile-time by defining LINEAR_PROBES to be any value. Set
LINEAR_PROBES=0 to turn-off linear probing entirely. (Contributed
by Raymond Hettinger in bpo-18771(2).)
* The interpreter starts about 30% faster. A couple of measures lead
to the speedup. The interpreter loads fewer modules on startup,
e.g. the *note re: dd, *note collections: 1e. and *note locale:
a9. modules and their dependencies are no longer imported by
default. The marshal module has been improved to load compiled
Python code faster. (Contributed by Antoine Pitrou, Christian
Heimes and Victor Stinner in bpo-19219(3), bpo-19218(4),
bpo-19209(5), bpo-19205(6) and bpo-9548(7).)
* *note bz2.BZ2File: 931. is now as fast or faster than the Python2
version for most cases. *note lzma.LZMAFile: 932. has also been
optimized. (Contributed by Serhiy Storchaka and Nadeem Vawda in
bpo-16034(8).)
* *note random.getrandbits(): 933. is 20%-40% faster for small
integers (the most common use case). (Contributed by Serhiy
Storchaka in bpo-16674(9).)
* By taking advantage of the new storage format for strings, pickling
of strings is now significantly faster. (Contributed by Victor
Stinner and Antoine Pitrou in bpo-15596(10).)
* A performance issue in ‘io.FileIO.readall()’ has been solved. This
particularly affects Windows, and significantly speeds up the case
of piping significant amounts of data through *note subprocess: f9.
(Contributed by Richard Oudkerk in bpo-15758(11).)
* *note html.escape(): 934. is now 10x faster. (Contributed by Matt
Bryant in bpo-18020(12).)
* On Windows, the native ‘VirtualAlloc’ is now used instead of the
CRT ‘malloc’ in ‘obmalloc’. Artificial benchmarks show about a 3%
memory savings.
* *note os.urandom(): 4d1. now uses a lazily-opened persistent file
descriptor so as to avoid using many file descriptors when run in
parallel from multiple threads. (Contributed by Antoine Pitrou in
bpo-18756(13).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14625
(2) https://bugs.python.org/issue18771
(3) https://bugs.python.org/issue19219
(4) https://bugs.python.org/issue19218
(5) https://bugs.python.org/issue19209
(6) https://bugs.python.org/issue19205
(7) https://bugs.python.org/issue9548
(8) https://bugs.python.org/issue16034
(9) https://bugs.python.org/issue16674
(10) https://bugs.python.org/issue15596
(11) https://bugs.python.org/issue15758
(12) https://bugs.python.org/issue18020
(13) https://bugs.python.org/issue18756
File: python.info, Node: Deprecated<4>, Next: Removed<3>, Prev: CPython Implementation Changes, Up: What’s New In Python 3 4
1.5.6 Deprecated
----------------
This section covers various APIs and other features that have been
deprecated in Python 3.4, and will be removed in Python 3.5 or later.
In most (but not all) cases, using the deprecated APIs will produce a
*note DeprecationWarning: 278. when the interpreter is run with
deprecation warnings enabled (for example, by using ‘-Wd’).
* Menu:
* Deprecations in the Python API::
* Deprecated Features::
File: python.info, Node: Deprecations in the Python API, Next: Deprecated Features, Up: Deprecated<4>
1.5.6.1 Deprecations in the Python API
......................................
* As mentioned in *note PEP 451; A ModuleSpec Type for the Import
System: 7d8, a number of *note importlib: 9b. methods and functions
are deprecated: *note importlib.find_loader(): 937. is replaced by
*note importlib.util.find_spec(): 938.; *note
importlib.machinery.PathFinder.find_module(): 939. is replaced by
*note importlib.machinery.PathFinder.find_spec(): 93a.; *note
importlib.abc.MetaPathFinder.find_module(): 462. is replaced by
*note importlib.abc.MetaPathFinder.find_spec(): 463.; *note
importlib.abc.PathEntryFinder.find_loader(): 464. and *note
find_module(): 93b. are replaced by *note
importlib.abc.PathEntryFinder.find_spec(): 465.; all of the
‘xxxLoader’ ABC ‘load_module’ methods (*note
importlib.abc.Loader.load_module(): 5e3, *note
importlib.abc.InspectLoader.load_module(): 93c, *note
importlib.abc.FileLoader.load_module(): 93d, *note
importlib.abc.SourceLoader.load_module(): 93e.) should no longer be
implemented, instead loaders should implement an ‘exec_module’
method (*note importlib.abc.Loader.exec_module(): 5e4, *note
importlib.abc.InspectLoader.exec_module(): 93f. *note
importlib.abc.SourceLoader.exec_module(): 940.) and let the import
system take care of the rest; and *note
importlib.abc.Loader.module_repr(): 941, *note
importlib.util.module_for_loader(): 942, *note
importlib.util.set_loader(): 943, and *note
importlib.util.set_package(): 944. are no longer needed because
their functions are now handled automatically by the import system.
* The *note imp: 9a. module is pending deprecation. To keep
compatibility with Python 2/3 code bases, the module’s removal is
currently not scheduled.
* The *note formatter: 82. module is pending deprecation and is
slated for removal in Python 3.6.
* ‘MD5’ as the default `digestmod' for the *note hmac.new(): 854.
function is deprecated. Python 3.6 will require an explicit digest
name or constructor as `digestmod' argument.
* The internal ‘Netrc’ class in the *note ftplib: 84. module has been
documented as deprecated in its docstring for quite some time. It
now emits a *note DeprecationWarning: 278. and will be removed
completely in Python 3.5.
* The undocumented `endtime' argument to *note
subprocess.Popen.wait(): 592. should not have been exposed and is
hopefully not in use; it is deprecated and will mostly likely be
removed in Python 3.5.
* The `strict' argument of *note HTMLParser: 7bf. is deprecated.
* The *note plistlib: cf. *note readPlist(): 47f, *note writePlist():
945, *note readPlistFromBytes(): 480, and *note
writePlistToBytes(): 946. functions are deprecated in favor of the
corresponding new functions *note load(): 88b, *note dump(): 88c,
*note loads(): 88d, and *note dumps(): 88e. *note Data(): 947. is
deprecated in favor of just using the *note bytes: 331.
constructor.
* The *note sysconfig: fe. key ‘SO’ is deprecated, it has been
replaced by ‘EXT_SUFFIX’.
* The ‘U’ mode accepted by various ‘open’ functions is deprecated.
In Python3 it does not do anything useful, and should be replaced
by appropriate uses of *note io.TextIOWrapper: 5f3. (if needed) and
its `newline' argument.
* The `parser' argument of *note xml.etree.ElementTree.iterparse():
948. has been deprecated, as has the `html' argument of *note
XMLParser(): 252. To prepare for the removal of the latter, all
arguments to ‘XMLParser’ should be passed by keyword.
File: python.info, Node: Deprecated Features, Prev: Deprecations in the Python API, Up: Deprecated<4>
1.5.6.2 Deprecated Features
...........................
* Running *note IDLE: 94a. with the ‘-n’ flag (no subprocess) is
deprecated. However, the feature will not be removed until
bpo-18823(1) is resolved.
* The site module adding a “site-python” directory to sys.path, if it
exists, is deprecated (bpo-19375(2)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue18823
(2) https://bugs.python.org/issue19375
File: python.info, Node: Removed<3>, Next: Porting to Python 3 4, Prev: Deprecated<4>, Up: What’s New In Python 3 4
1.5.7 Removed
-------------
* Menu:
* Operating Systems No Longer Supported::
* API and Feature Removals: API and Feature Removals<5>.
* Code Cleanups::
File: python.info, Node: Operating Systems No Longer Supported, Next: API and Feature Removals<5>, Up: Removed<3>
1.5.7.1 Operating Systems No Longer Supported
.............................................
Support for the following operating systems has been removed from the
source and build tools:
* OS/2 (bpo-16135(1)).
* Windows 2000 (changeset e52df05b496a).
* Windows systems where ‘COMSPEC’ points to ‘command.com’
(bpo-14470(2)).
* VMS (bpo-16136(3)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue16135
(2) https://bugs.python.org/issue14470
(3) https://bugs.python.org/issue16136
File: python.info, Node: API and Feature Removals<5>, Next: Code Cleanups, Prev: Operating Systems No Longer Supported, Up: Removed<3>
1.5.7.2 API and Feature Removals
................................
The following obsolete and previously deprecated APIs and features have
been removed:
* The unmaintained ‘Misc/TextMate’ and ‘Misc/vim’ directories have
been removed (see the devguide(1) for suggestions on what to use
instead).
* The ‘SO’ makefile macro is removed (it was replaced by the
‘SHLIB_SUFFIX’ and ‘EXT_SUFFIX’ macros) (bpo-16754(2)).
* The ‘PyThreadState.tick_counter’ field has been removed; its value
has been meaningless since Python 3.2, when the “new GIL” was
introduced (bpo-19199(3)).
* ‘PyLoader’ and ‘PyPycLoader’ have been removed from *note
importlib: 9b. (Contributed by Taras Lyapun in bpo-15641(4).)
* The `strict' argument to *note HTTPConnection: 392. and *note
HTTPSConnection: 393. has been removed. HTTP 0.9-style “Simple
Responses” are no longer supported.
* The deprecated *note urllib.request.Request: 8f6. getter and setter
methods ‘add_data’, ‘has_data’, ‘get_data’, ‘get_type’, ‘get_host’,
‘get_selector’, ‘set_proxy’, ‘get_origin_req_host’, and
‘is_unverifiable’ have been removed (use direct attribute access
instead).
* Support for loading the deprecated ‘TYPE_INT64’ has been removed
from *note marshal: b0. (Contributed by Dan Riti in bpo-15480(5).)
* *note inspect.Signature: 6f3.: positional-only parameters are now
required to have a valid name.
* *note object.__format__(): 94e. no longer accepts non-empty format
strings, it now raises a *note TypeError: 192. instead. Using a
non-empty string has been deprecated since Python 3.2. This change
has been made to prevent a situation where previously working (but
incorrect) code would start failing if an object gained a
__format__ method, which means that your code may now raise a *note
TypeError: 192. if you are using an ‘'s'’ format code with objects
that do not have a __format__ method that handles it. See
bpo-7994(6) for background.
* ‘difflib.SequenceMatcher.isbjunk()’ and
‘difflib.SequenceMatcher.isbpopular()’ were deprecated in 3.2, and
have now been removed: use ‘x in sm.bjunk’ and ‘x in sm.bpopular’,
where `sm' is a *note SequenceMatcher: 94f. object (bpo-13248(7)).
---------- Footnotes ----------
(1) https://devguide.python.org
(2) https://bugs.python.org/issue16754
(3) https://bugs.python.org/issue19199
(4) https://bugs.python.org/issue15641
(5) https://bugs.python.org/issue15480
(6) https://bugs.python.org/issue7994
(7) https://bugs.python.org/issue13248
File: python.info, Node: Code Cleanups, Prev: API and Feature Removals<5>, Up: Removed<3>
1.5.7.3 Code Cleanups
.....................
* The unused and undocumented internal ‘Scanner’ class has been
removed from the *note pydoc: d9. module.
* The private and effectively unused ‘_gestalt’ module has been
removed, along with the private *note platform: ce. functions
‘_mac_ver_lookup’, ‘_mac_ver_gstalt’, and ‘_bcd2str’, which would
only have ever been called on badly broken OSX systems (see
bpo-18393(1)).
* The hardcoded copies of certain *note stat: f4. constants that were
included in the *note tarfile: 101. module namespace have been
removed.
---------- Footnotes ----------
(1) https://bugs.python.org/issue18393
File: python.info, Node: Porting to Python 3 4, Next: Changed in 3 4 3, Prev: Removed<3>, Up: What’s New In Python 3 4
1.5.8 Porting to Python 3.4
---------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code.
* Menu:
* Changes in ‘python’ Command Behavior: Changes in ‘python’ Command Behavior<2>.
* Changes in the Python API: Changes in the Python API<5>.
* Changes in the C API: Changes in the C API<5>.
File: python.info, Node: Changes in ‘python’ Command Behavior<2>, Next: Changes in the Python API<5>, Up: Porting to Python 3 4
1.5.8.1 Changes in ‘python’ Command Behavior
............................................
* In a posix shell, setting the ‘PATH’ environment variable to an
empty value is equivalent to not setting it at all. However,
setting *note PYTHONPATH: 953. to an empty value was `not'
equivalent to not setting it at all: setting *note PYTHONPATH: 953.
to an empty value was equivalent to setting it to ‘.’, which leads
to confusion when reasoning by analogy to how ‘PATH’ works. The
behavior now conforms to the posix convention for ‘PATH’.
* The [X refs, Y blocks] output of a debug (‘--with-pydebug’) build
of the CPython interpreter is now off by default. It can be
re-enabled using the ‘-X showrefcount’ option. (Contributed by
Ezio Melotti in bpo-17323(1).)
* The python command and most stdlib scripts (as well as *note
argparse: 6.) now output ‘--version’ information to ‘stdout’
instead of ‘stderr’ (for issue list see *note Other Improvements:
92a. above).
---------- Footnotes ----------
(1) https://bugs.python.org/issue17323
File: python.info, Node: Changes in the Python API<5>, Next: Changes in the C API<5>, Prev: Changes in ‘python’ Command Behavior<2>, Up: Porting to Python 3 4
1.5.8.2 Changes in the Python API
.................................
* The ABCs defined in *note importlib.abc: 9c. now either raise the
appropriate exception or return a default value instead of raising
*note NotImplementedError: 60e. blindly. This will only affect
code calling *note super(): 4e2. and falling through all the way to
the ABCs. For compatibility, catch both *note NotImplementedError:
60e. or the appropriate exception as needed.
* The module type now initializes the *note __package__: 955. and
*note __loader__: 956. attributes to ‘None’ by default. To
determine if these attributes were set in a backwards-compatible
fashion, use e.g. ‘getattr(module, '__loader__', None) is not
None’. (bpo-17115(1).)
* *note importlib.util.module_for_loader(): 942. now sets
‘__loader__’ and ‘__package__’ unconditionally to properly support
reloading. If this is not desired then you will need to set these
attributes manually. You can use ‘importlib.util.module_to_load()’
for module management.
* Import now resets relevant attributes (e.g. ‘__name__’,
‘__loader__’, ‘__package__’, ‘__file__’, ‘__cached__’)
unconditionally when reloading. Note that this restores a pre-3.3
behavior in that it means a module is re-found when re-loaded
(bpo-19413(2)).
* Frozen packages no longer set ‘__path__’ to a list containing the
package name, they now set it to an empty list. The previous
behavior could cause the import system to do the wrong thing on
submodule imports if there was also a directory with the same name
as the frozen package. The correct way to determine if a module is
a package or not is to use ‘hasattr(module, '__path__')’
(bpo-18065(3)).
* Frozen modules no longer define a ‘__file__’ attribute. It’s
semantically incorrect for frozen modules to set the attribute as
they are not loaded from any explicit location. If you must know
that a module comes from frozen code then you can see if the
module’s ‘__spec__.location’ is set to ‘'frozen'’, check if the
loader is a subclass of *note importlib.machinery.FrozenImporter:
957, or if Python 2 compatibility is necessary you can use
‘imp.is_frozen()’.
* *note py_compile.compile(): 215. now raises *note FileExistsError:
958. if the file path it would write to is a symlink or a
non-regular file. This is to act as a warning that import will
overwrite those files with a regular file regardless of what type
of file path they were originally.
* *note importlib.abc.SourceLoader.get_source(): 959. no longer
raises *note ImportError: 334. when the source code being loaded
triggers a *note SyntaxError: 458. or *note UnicodeDecodeError:
1fd. As *note ImportError: 334. is meant to be raised only when
source code cannot be found but it should, it was felt to be
over-reaching/overloading of that meaning when the source code is
found but improperly structured. If you were catching ImportError
before and wish to continue to ignore syntax or decoding issues,
catch all three exceptions now.
* *note functools.update_wrapper(): 95a. and *note functools.wraps():
86a. now correctly set the ‘__wrapped__’ attribute to the function
being wrapped, even if that function also had its ‘__wrapped__’
attribute set. This means ‘__wrapped__’ attributes now correctly
link a stack of decorated functions rather than every ‘__wrapped__’
attribute in the chain referring to the innermost function.
Introspection libraries that assumed the previous behaviour was
intentional can use *note inspect.unwrap(): 869. to access the
first function in the chain that has no ‘__wrapped__’ attribute.
* *note inspect.getfullargspec(): 55d. has been reimplemented on top
of *note inspect.signature(): 55b. and hence handles a much wider
variety of callable objects than it did in the past. It is
expected that additional builtin and extension module callables
will gain signature metadata over the course of the Python 3.4
series. Code that assumes that *note inspect.getfullargspec():
55d. will fail on non-Python callables may need to be adjusted
accordingly.
* *note importlib.machinery.PathFinder: 95b. now passes on the
current working directory to objects in *note sys.path_hooks: 95c.
for the empty string. This results in *note
sys.path_importer_cache: 49d. never containing ‘''’, thus iterating
through *note sys.path_importer_cache: 49d. based on *note
sys.path: 488. will not find all keys. A module’s ‘__file__’ when
imported in the current working directory will also now have an
absolute path, including when using ‘-m’ with the interpreter
(except for ‘__main__.__file__’ when a script has been executed
directly using a relative path) (Contributed by Brett Cannon in
bpo-18416(4)). is specified on the command-line) (bpo-18416(5)).
* The removal of the `strict' argument to *note HTTPConnection: 392.
and *note HTTPSConnection: 393. changes the meaning of the
remaining arguments if you are specifying them positionally rather
than by keyword. If you’ve been paying attention to deprecation
warnings your code should already be specifying any additional
arguments via keywords.
* Strings between ‘from __future__ import ...’ statements now
`always' raise a *note SyntaxError: 458. Previously if there was
no leading docstring, an interstitial string would sometimes be
ignored. This brings CPython into compliance with the language
spec; Jython and PyPy already were. (bpo-17434(6)).
* *note ssl.SSLSocket.getpeercert(): 8d1. and *note
ssl.SSLSocket.do_handshake(): 75a. now raise an *note OSError: 1d3.
with ‘ENOTCONN’ when the ‘SSLSocket’ is not connected, instead of
the previous behavior of raising an *note AttributeError: 39b. In
addition, *note getpeercert(): 8d1. will raise a *note ValueError:
1fb. if the handshake has not yet been done.
* *note base64.b32decode(): 95d. now raises a *note binascii.Error:
95e. when the input string contains non-b32-alphabet characters,
instead of a *note TypeError: 192. This particular *note
TypeError: 192. was missed when the other *note TypeError: 192.s
were converted. (Contributed by Serhiy Storchaka in bpo-18011(7).)
Note: this change was also inadvertently applied in Python 3.3.3.
* The ‘file’ attribute is now automatically closed when the creating
‘cgi.FieldStorage’ instance is garbage collected. If you were
pulling the file object out separately from the ‘cgi.FieldStorage’
instance and not keeping the instance alive, then you should either
store the entire ‘cgi.FieldStorage’ instance or read the contents
of the file before the ‘cgi.FieldStorage’ instance is garbage
collected.
* Calling ‘read’ or ‘write’ on a closed SSL socket now raises an
informative *note ValueError: 1fb. rather than the previous more
mysterious *note AttributeError: 39b. (bpo-9177(8)).
* *note slice.indices(): 95f. no longer produces an *note
OverflowError: 960. for huge values. As a consequence of this fix,
*note slice.indices(): 95f. now raises a *note ValueError: 1fb. if
given a negative length; previously it returned nonsense values
(bpo-14794(9)).
* The *note complex: 189. constructor, unlike the *note cmath: 19.
functions, was incorrectly accepting *note float: 187. values if an
object’s ‘__complex__’ special method returned one. This now
raises a *note TypeError: 192. (bpo-16290(10).)
* The *note int: 184. constructor in 3.2 and 3.3 erroneously accepts
*note float: 187. values for the `base' parameter. It is unlikely
anyone was doing this, but if so, it will now raise a *note
TypeError: 192. (bpo-16772(11)).
* Defaults for keyword-only arguments are now evaluated `after'
defaults for regular keyword arguments, instead of before.
Hopefully no one wrote any code that depends on the previous buggy
behavior (bpo-16967(12)).
* Stale thread states are now cleared after *note fork(): 961. This
may cause some system resources to be released that previously were
incorrectly kept perpetually alive (for example, database
connections kept in thread-local storage). (bpo-17094(13).)
* Parameter names in ‘__annotations__’ dicts are now mangled
properly, similarly to ‘__kwdefaults__’. (Contributed by Yury
Selivanov in bpo-20625(14).)
* *note hashlib.hash.name: 852. now always returns the identifier in
lower case. Previously some builtin hashes had uppercase names,
but now that it is a formal public interface the naming has been
made consistent (bpo-18532(15)).
* Because *note unittest.TestSuite: 6ce. now drops references to
tests after they are run, test harnesses that re-use a *note
TestSuite: 6ce. to re-run a set of tests may fail. Test suites
should not be re-used in this fashion since it means state is
retained between test runs, breaking the test isolation that *note
unittest: 11b. is designed to provide. However, if the lack of
isolation is considered acceptable, the old behavior can be
restored by creating a *note TestSuite: 6ce. subclass that defines
a ‘_removeTestAtIndex’ method that does nothing (see *note
TestSuite.__iter__(): 962.) (bpo-11798(16)).
* *note unittest: 11b. now uses *note argparse: 6. for command line
parsing. There are certain invalid command forms that used to work
that are no longer allowed; in theory this should not cause
backward compatibility issues since the disallowed command forms
didn’t make any sense and are unlikely to be in use.
* The *note re.split(): 3ca, *note re.findall(): 963, and *note
re.sub(): 47b. functions, and the ‘group()’ and ‘groups()’ methods
of ‘match’ objects now always return a `bytes' object when the
string to be matched is a *note bytes-like object: 5e8. Previously
the return type matched the input type, so if your code was
depending on the return value being, say, a ‘bytearray’, you will
need to change your code.
* *note audioop: d. functions now raise an error immediately if
passed string input, instead of failing randomly later on
(bpo-16685(17)).
* The new `convert_charrefs' argument to *note HTMLParser: 7bf.
currently defaults to ‘False’ for backward compatibility, but will
eventually be changed to default to ‘True’. It is recommended that
you add this keyword, with the appropriate value, to any *note
HTMLParser: 7bf. calls in your code (bpo-13633(18)).
* Since the `digestmod' argument to the *note hmac.new(): 854.
function will in the future have no default, all calls to *note
hmac.new(): 854. should be changed to explicitly specify a
`digestmod' (bpo-17276(19)).
* Calling *note sysconfig.get_config_var(): 964. with the ‘SO’ key,
or looking ‘SO’ up in the results of a call to *note
sysconfig.get_config_vars(): 965. is deprecated. This key should
be replaced by ‘EXT_SUFFIX’ or ‘SHLIB_SUFFIX’, depending on the
context (bpo-19555(20)).
* Any calls to ‘open’ functions that specify ‘U’ should be modified.
‘U’ is ineffective in Python3 and will eventually raise an error if
used. Depending on the function, the equivalent of its old Python2
behavior can be achieved using either a `newline' argument, or if
necessary by wrapping the stream in *note TextIOWrapper: 5f3. to
use its `newline' argument (bpo-15204(21)).
* If you use ‘pyvenv’ in a script and desire that pip `not' be
installed, you must add ‘--without-pip’ to your command invocation.
* The default behavior of *note json.dump(): 604. and *note
json.dumps(): 605. when an indent is specified has changed: it no
longer produces trailing spaces after the item separating commas at
the ends of lines. This will matter only if you have tests that
are doing white-space-sensitive comparisons of such output
(bpo-16333(22)).
* *note doctest: 67. now looks for doctests in extension module
‘__doc__’ strings, so if your doctest test discovery includes
extension modules that have things that look like doctests in them
you may see test failures you’ve never seen before when running
your tests (bpo-3158(23)).
* The *note collections.abc: 1f. module has been slightly refactored
as part of the Python startup improvements. As a consequence of
this, it is no longer the case that importing *note collections:
1e. automatically imports *note collections.abc: 1f. If your
program depended on the (undocumented) implicit import, you will
need to add an explicit ‘import collections.abc’ (bpo-20784(24)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue17115
(2) https://bugs.python.org/issue19413
(3) https://bugs.python.org/issue18065
(4) https://bugs.python.org/issue18416
(5) https://bugs.python.org/issue18416
(6) https://bugs.python.org/issue17434
(7) https://bugs.python.org/issue18011
(8) https://bugs.python.org/issue9177
(9) https://bugs.python.org/issue14794
(10) https://bugs.python.org/issue16290
(11) https://bugs.python.org/issue16772
(12) https://bugs.python.org/issue16967
(13) https://bugs.python.org/issue17094
(14) https://bugs.python.org/issue20625
(15) https://bugs.python.org/issue18532
(16) https://bugs.python.org/issue11798
(17) https://bugs.python.org/issue16685
(18) https://bugs.python.org/issue13633
(19) https://bugs.python.org/issue17276
(20) https://bugs.python.org/issue19555
(21) https://bugs.python.org/issue15204
(22) https://bugs.python.org/issue16333
(23) https://bugs.python.org/issue3158
(24) https://bugs.python.org/issue20784
File: python.info, Node: Changes in the C API<5>, Prev: Changes in the Python API<5>, Up: Porting to Python 3 4
1.5.8.3 Changes in the C API
............................
* *note PyEval_EvalFrameEx(): 967, *note PyObject_Repr(): 968, and
*note PyObject_Str(): 969, along with some other internal C APIs,
now include a debugging assertion that ensures they are not used in
situations where they may silently discard a currently active
exception. In cases where discarding the active exception is
expected and desired (for example, because it has already been
saved locally with *note PyErr_Fetch(): 96a. or is being
deliberately replaced with a different exception), an explicit
*note PyErr_Clear(): 96b. call will be needed to avoid triggering
the assertion when invoking these operations (directly or
indirectly) and running against a version of Python that is
compiled with assertions enabled.
* *note PyErr_SetImportError(): 5f8. now sets *note TypeError: 192.
when its `msg' argument is not set. Previously only ‘NULL’ was
returned with no exception set.
* The result of the *note PyOS_ReadlineFunctionPointer: 96c. callback
must now be a string allocated by *note PyMem_RawMalloc(): 96d. or
*note PyMem_RawRealloc(): 96e, or ‘NULL’ if an error occurred,
instead of a string allocated by *note PyMem_Malloc(): 505. or
*note PyMem_Realloc(): 96f. (bpo-16742(1))
* *note PyThread_set_key_value(): 970. now always set the value. In
Python 3.3, the function did nothing if the key already exists (if
the current value is a non-‘NULL’ pointer).
* The ‘f_tstate’ (thread state) field of the *note PyFrameObject:
971. structure has been removed to fix a bug: see bpo-14432(2) for
the rationale.
---------- Footnotes ----------
(1) https://bugs.python.org/issue16742
(2) https://bugs.python.org/issue14432
File: python.info, Node: Changed in 3 4 3, Prev: Porting to Python 3 4, Up: What’s New In Python 3 4
1.5.9 Changed in 3.4.3
----------------------
* Menu:
* PEP 476; Enabling certificate verification by default for stdlib http clients: PEP 476 Enabling certificate verification by default for stdlib http clients.
File: python.info, Node: PEP 476 Enabling certificate verification by default for stdlib http clients, Up: Changed in 3 4 3
1.5.9.1 PEP 476: Enabling certificate verification by default for stdlib http clients
.....................................................................................
*note http.client: 94. and modules which use it, such as *note
urllib.request: 120. and *note xmlrpc.client: 13f, will now verify that
the server presents a certificate which is signed by a CA in the
platform trust store and whose hostname matches the hostname being
requested by default, significantly improving security for many
applications.
For applications which require the old previous behavior, they can pass
an alternate context:
import urllib.request
import ssl
# This disables all verification
context = ssl._create_unverified_context()
# This allows using a specific certificate for the host, which doesn't need
# to be in the trust store
context = ssl.create_default_context(cafile="/path/to/file.crt")
urllib.request.urlopen("https://invalid-cert", context=context)
File: python.info, Node: What’s New In Python 3 3, Next: What’s New In Python 3 2, Prev: What’s New In Python 3 4, Up: What’s New in Python
1.6 What’s New In Python 3.3
============================
This article explains the new features in Python 3.3, compared to 3.2.
Python 3.3 was released on September 29, 2012. For full details, see
the changelog(1).
See also
........
PEP 398(2) - Python 3.3 Release Schedule
* Menu:
* Summary – Release highlights: Summary – Release highlights<4>.
* PEP 405; Virtual Environments: PEP 405 Virtual Environments.
* PEP 420; Implicit Namespace Packages: PEP 420 Implicit Namespace Packages.
* PEP 3118; New memoryview implementation and buffer protocol documentation: PEP 3118 New memoryview implementation and buffer protocol documentation.
* PEP 393; Flexible String Representation: PEP 393 Flexible String Representation.
* PEP 397; Python Launcher for Windows: PEP 397 Python Launcher for Windows.
* PEP 3151; Reworking the OS and IO exception hierarchy: PEP 3151 Reworking the OS and IO exception hierarchy.
* PEP 380; Syntax for Delegating to a Subgenerator: PEP 380 Syntax for Delegating to a Subgenerator.
* PEP 409; Suppressing exception context: PEP 409 Suppressing exception context.
* PEP 414; Explicit Unicode literals: PEP 414 Explicit Unicode literals.
* PEP 3155; Qualified name for classes and functions: PEP 3155 Qualified name for classes and functions.
* PEP 412; Key-Sharing Dictionary: PEP 412 Key-Sharing Dictionary.
* PEP 362; Function Signature Object: PEP 362 Function Signature Object.
* PEP 421; Adding sys.implementation: PEP 421 Adding sys implementation.
* Using importlib as the Implementation of Import::
* Other Language Changes: Other Language Changes<6>.
* A Finer-Grained Import Lock::
* Builtin functions and types::
* New Modules: New Modules<6>.
* Improved Modules: Improved Modules<6>.
* Optimizations: Optimizations<5>.
* Build and C API Changes: Build and C API Changes<4>.
* Deprecated: Deprecated<5>.
* Porting to Python 3.3: Porting to Python 3 3.
---------- Footnotes ----------
(1) https://docs.python.org/3.3/whatsnew/changelog.html
(2) https://www.python.org/dev/peps/pep-0398
File: python.info, Node: Summary – Release highlights<4>, Next: PEP 405 Virtual Environments, Up: What’s New In Python 3 3
1.6.1 Summary – Release highlights
----------------------------------
New syntax features:
* New ‘yield from’ expression for *note generator delegation: 978.
* The ‘u'unicode'’ syntax is accepted again for *note str: 330.
objects.
New library modules:
* *note faulthandler: 7d. (helps debugging low-level crashes)
* *note ipaddress: a2. (high-level objects representing IP addresses
and masks)
* *note lzma: ad. (compress data using the XZ / LZMA algorithm)
* *note unittest.mock: 11c. (replace parts of your system under test
with mock objects)
* *note venv: 125. (Python *note virtual environments: 979, as in the
popular ‘virtualenv’ package)
New built-in features:
* Reworked *note I/O exception hierarchy: 97a.
Implementation improvements:
* Rewritten *note import machinery: 97b. based on *note importlib:
9b.
* More compact *note unicode strings: 97c.
* More compact *note attribute dictionaries: 97d.
Significantly Improved Library Modules:
* C Accelerator for the *note decimal: 97e. module.
* Better unicode handling in the *note email: 97f. module (*note
provisional: 980.).
Security improvements:
* Hash randomization is switched on by default.
Please read on for a comprehensive list of user-facing changes.
File: python.info, Node: PEP 405 Virtual Environments, Next: PEP 420 Implicit Namespace Packages, Prev: Summary – Release highlights<4>, Up: What’s New In Python 3 3
1.6.2 PEP 405: Virtual Environments
-----------------------------------
Virtual environments help create separate Python setups while sharing a
system-wide base install, for ease of maintenance. Virtual environments
have their own set of private site packages (i.e. locally-installed
libraries), and are optionally segregated from the system-wide site
packages. Their concept and implementation are inspired by the popular
‘virtualenv’ third-party package, but benefit from tighter integration
with the interpreter core.
This PEP adds the *note venv: 125. module for programmatic access, and
the ‘pyvenv’ script for command-line access and administration. The
Python interpreter checks for a ‘pyvenv.cfg’, file whose existence
signals the base of a virtual environment’s directory tree.
See also
........
PEP 405(1) - Python Virtual Environments
PEP written by Carl Meyer; implementation by Carl Meyer and Vinay
Sajip
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0405
File: python.info, Node: PEP 420 Implicit Namespace Packages, Next: PEP 3118 New memoryview implementation and buffer protocol documentation, Prev: PEP 405 Virtual Environments, Up: What’s New In Python 3 3
1.6.3 PEP 420: Implicit Namespace Packages
------------------------------------------
Native support for package directories that don’t require ‘__init__.py’
marker files and can automatically span multiple path segments (inspired
by various third party approaches to namespace packages, as described in
PEP 420(1))
See also
........
PEP 420(2) - Implicit Namespace Packages
PEP written by Eric V. Smith; implementation by Eric V. Smith and
Barry Warsaw
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0420
(2) https://www.python.org/dev/peps/pep-0420
File: python.info, Node: PEP 3118 New memoryview implementation and buffer protocol documentation, Next: PEP 393 Flexible String Representation, Prev: PEP 420 Implicit Namespace Packages, Up: What’s New In Python 3 3
1.6.4 PEP 3118: New memoryview implementation and buffer protocol documentation
-------------------------------------------------------------------------------
The implementation of PEP 3118(1) has been significantly improved.
The new memoryview implementation comprehensively fixes all ownership
and lifetime issues of dynamically allocated fields in the Py_buffer
struct that led to multiple crash reports. Additionally, several
functions that crashed or returned incorrect results for non-contiguous
or multi-dimensional input have been fixed.
The memoryview object now has a PEP-3118 compliant getbufferproc() that
checks the consumer’s request type. Many new features have been added,
most of them work in full generality for non-contiguous arrays and
arrays with suboffsets.
The documentation has been updated, clearly spelling out
responsibilities for both exporters and consumers. Buffer request flags
are grouped into basic and compound flags. The memory layout of
non-contiguous and multi-dimensional NumPy-style arrays is explained.
* Menu:
* Features::
* API changes::
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3118
File: python.info, Node: Features, Next: API changes, Up: PEP 3118 New memoryview implementation and buffer protocol documentation
1.6.4.1 Features
................
* All native single character format specifiers in struct module
syntax (optionally prefixed with ‘@’) are now supported.
* With some restrictions, the cast() method allows changing of format
and shape of C-contiguous arrays.
* Multi-dimensional list representations are supported for any array
type.
* Multi-dimensional comparisons are supported for any array type.
* One-dimensional memoryviews of hashable (read-only) types with
formats B, b or c are now hashable. (Contributed by Antoine Pitrou
in bpo-13411(1).)
* Arbitrary slicing of any 1-D arrays type is supported. For
example, it is now possible to reverse a memoryview in O(1) by
using a negative step.
---------- Footnotes ----------
(1) https://bugs.python.org/issue13411
File: python.info, Node: API changes, Prev: Features, Up: PEP 3118 New memoryview implementation and buffer protocol documentation
1.6.4.2 API changes
...................
* The maximum number of dimensions is officially limited to 64.
* The representation of empty shape, strides and suboffsets is now an
empty tuple instead of ‘None’.
* Accessing a memoryview element with format ‘B’ (unsigned bytes) now
returns an integer (in accordance with the struct module syntax).
For returning a bytes object the view must be cast to ‘c’ first.
* memoryview comparisons now use the logical structure of the
operands and compare all array elements by value. All format
strings in struct module syntax are supported. Views with
unrecognised format strings are still permitted, but will always
compare as unequal, regardless of view contents.
* For further changes see *note Build and C API Changes: 987. and
*note Porting C code: 988.
(Contributed by Stefan Krah in bpo-10181(1).)
See also
........
PEP 3118(2) - Revising the Buffer Protocol
---------- Footnotes ----------
(1) https://bugs.python.org/issue10181
(2) https://www.python.org/dev/peps/pep-3118
File: python.info, Node: PEP 393 Flexible String Representation, Next: PEP 397 Python Launcher for Windows, Prev: PEP 3118 New memoryview implementation and buffer protocol documentation, Up: What’s New In Python 3 3
1.6.5 PEP 393: Flexible String Representation
---------------------------------------------
The Unicode string type is changed to support multiple internal
representations, depending on the character with the largest Unicode
ordinal (1, 2, or 4 bytes) in the represented string. This allows a
space-efficient representation in common cases, but gives access to full
UCS-4 on all systems. For compatibility with existing APIs, several
representations may exist in parallel; over time, this compatibility
should be phased out.
On the Python side, there should be no downside to this change.
On the C API side, PEP 393(1) is fully backward compatible. The legacy
API should remain available at least five years. Applications using the
legacy API will not fully benefit of the memory reduction, or - worse -
may use a bit more memory, because Python may have to maintain two
versions of each string (in the legacy format and in the new efficient
storage).
* Menu:
* Functionality::
* Performance and resource usage::
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0393
File: python.info, Node: Functionality, Next: Performance and resource usage, Up: PEP 393 Flexible String Representation
1.6.5.1 Functionality
.....................
Changes introduced by PEP 393(1) are the following:
* Python now always supports the full range of Unicode code points,
including non-BMP ones (i.e. from ‘U+0000’ to ‘U+10FFFF’). The
distinction between narrow and wide builds no longer exists and
Python now behaves like a wide build, even under Windows.
* With the death of narrow builds, the problems specific to narrow
builds have also been fixed, for example:
* *note len(): 150. now always returns 1 for non-BMP characters,
so ‘len('\U0010FFFF') == 1’;
* surrogate pairs are not recombined in string literals, so
‘'\uDBFF\uDFFF' != '\U0010FFFF'’;
* indexing or slicing non-BMP characters returns the expected
value, so ‘'\U0010FFFF'[0]’ now returns ‘'\U0010FFFF'’ and not
‘'\uDBFF'’;
* all other functions in the standard library now correctly
handle non-BMP code points.
* The value of *note sys.maxunicode: 98b. is now always ‘1114111’
(‘0x10FFFF’ in hexadecimal). The ‘PyUnicode_GetMax()’ function
still returns either ‘0xFFFF’ or ‘0x10FFFF’ for backward
compatibility, and it should not be used with the new Unicode API
(see bpo-13054(2)).
* The ‘./configure’ flag ‘--with-wide-unicode’ has been removed.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0393
(2) https://bugs.python.org/issue13054
File: python.info, Node: Performance and resource usage, Prev: Functionality, Up: PEP 393 Flexible String Representation
1.6.5.2 Performance and resource usage
......................................
The storage of Unicode strings now depends on the highest code point in
the string:
* pure ASCII and Latin1 strings (‘U+0000-U+00FF’) use 1 byte per code
point;
* BMP strings (‘U+0000-U+FFFF’) use 2 bytes per code point;
* non-BMP strings (‘U+10000-U+10FFFF’) use 4 bytes per code point.
The net effect is that for most applications, memory usage of string
storage should decrease significantly - especially compared to former
wide unicode builds - as, in many cases, strings will be pure ASCII even
in international contexts (because many strings store non-human language
data, such as XML fragments, HTTP headers, JSON-encoded data, etc.). We
also hope that it will, for the same reasons, increase CPU cache
efficiency on non-trivial applications. The memory usage of Python 3.3
is two to three times smaller than Python 3.2, and a little bit better
than Python 2.7, on a Django benchmark (see the PEP for details).
See also
........
PEP 393(1) - Flexible String Representation
PEP written by Martin von Löwis; implementation by Torsten Becker
and Martin von Löwis.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0393
File: python.info, Node: PEP 397 Python Launcher for Windows, Next: PEP 3151 Reworking the OS and IO exception hierarchy, Prev: PEP 393 Flexible String Representation, Up: What’s New In Python 3 3
1.6.6 PEP 397: Python Launcher for Windows
------------------------------------------
The Python 3.3 Windows installer now includes a ‘py’ launcher
application that can be used to launch Python applications in a version
independent fashion.
This launcher is invoked implicitly when double-clicking ‘*.py’ files.
If only a single Python version is installed on the system, that version
will be used to run the file. If multiple versions are installed, the
most recent version is used by default, but this can be overridden by
including a Unix-style “shebang line” in the Python script.
The launcher can also be used explicitly from the command line as the
‘py’ application. Running ‘py’ follows the same version selection rules
as implicitly launching scripts, but a more specific version can be
selected by passing appropriate arguments (such as ‘-3’ to request
Python 3 when Python 2 is also installed, or ‘-2.6’ to specifically
request an earlier Python version when a more recent version is
installed).
In addition to the launcher, the Windows installer now includes an
option to add the newly installed Python to the system PATH.
(Contributed by Brian Curtin in bpo-3561(1).)
See also
........
PEP 397(2) - Python Launcher for Windows
PEP written by Mark Hammond and Martin v. Löwis; implementation by
Vinay Sajip.
Launcher documentation: *note Python Launcher for Windows: 98f.
Installer PATH modification: *note Finding the Python executable: 990.
---------- Footnotes ----------
(1) https://bugs.python.org/issue3561
(2) https://www.python.org/dev/peps/pep-0397
File: python.info, Node: PEP 3151 Reworking the OS and IO exception hierarchy, Next: PEP 380 Syntax for Delegating to a Subgenerator, Prev: PEP 397 Python Launcher for Windows, Up: What’s New In Python 3 3
1.6.7 PEP 3151: Reworking the OS and IO exception hierarchy
-----------------------------------------------------------
The hierarchy of exceptions raised by operating system errors is now
both simplified and finer-grained.
You don’t have to worry anymore about choosing the appropriate exception
type between *note OSError: 1d3, *note IOError: 992, *note
EnvironmentError: 993, *note WindowsError: 994, ‘mmap.error’, *note
socket.error: 995. or *note select.error: 996. All these exception
types are now only one: *note OSError: 1d3. The other names are kept as
aliases for compatibility reasons.
Also, it is now easier to catch a specific error condition. Instead of
inspecting the ‘errno’ attribute (or ‘args[0]’) for a particular
constant from the *note errno: 7c. module, you can catch the adequate
*note OSError: 1d3. subclass. The available subclasses are the
following:
* *note BlockingIOError: 997.
* *note ChildProcessError: 998.
* *note ConnectionError: 6e6.
* *note FileExistsError: 958.
* *note FileNotFoundError: 7c0.
* *note InterruptedError: 659.
* *note IsADirectoryError: 999.
* *note NotADirectoryError: 99a.
* *note PermissionError: 5ff.
* *note ProcessLookupError: 99b.
* *note TimeoutError: 99c.
And the *note ConnectionError: 6e6. itself has finer-grained subclasses:
* *note BrokenPipeError: 99d.
* *note ConnectionAbortedError: 99e.
* *note ConnectionRefusedError: 99f.
* *note ConnectionResetError: 9a0.
Thanks to the new exceptions, common usages of the *note errno: 7c. can
now be avoided. For example, the following code written for Python 3.2:
from errno import ENOENT, EACCES, EPERM
try:
with open("document.txt") as f:
content = f.read()
except IOError as err:
if err.errno == ENOENT:
print("document.txt file is missing")
elif err.errno in (EACCES, EPERM):
print("You are not allowed to read document.txt")
else:
raise
can now be written without the *note errno: 7c. import and without
manual inspection of exception attributes:
try:
with open("document.txt") as f:
content = f.read()
except FileNotFoundError:
print("document.txt file is missing")
except PermissionError:
print("You are not allowed to read document.txt")
See also
........
PEP 3151(1) - Reworking the OS and IO Exception Hierarchy
PEP written and implemented by Antoine Pitrou
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3151
File: python.info, Node: PEP 380 Syntax for Delegating to a Subgenerator, Next: PEP 409 Suppressing exception context, Prev: PEP 3151 Reworking the OS and IO exception hierarchy, Up: What’s New In Python 3 3
1.6.8 PEP 380: Syntax for Delegating to a Subgenerator
------------------------------------------------------
PEP 380 adds the ‘yield from’ expression, allowing a *note generator:
9a2. to delegate part of its operations to another generator. This
allows a section of code containing *note yield: 18f. to be factored out
and placed in another generator. Additionally, the subgenerator is
allowed to return with a value, and the value is made available to the
delegating generator.
While designed primarily for use in delegating to a subgenerator, the
‘yield from’ expression actually allows delegation to arbitrary
subiterators.
For simple iterators, ‘yield from iterable’ is essentially just a
shortened form of ‘for item in iterable: yield item’:
>>> def g(x):
... yield from range(x, 0, -1)
... yield from range(x)
...
>>> list(g(5))
[5, 4, 3, 2, 1, 0, 1, 2, 3, 4]
However, unlike an ordinary loop, ‘yield from’ allows subgenerators to
receive sent and thrown values directly from the calling scope, and
return a final value to the outer generator:
>>> def accumulate():
... tally = 0
... while 1:
... next = yield
... if next is None:
... return tally
... tally += next
...
>>> def gather_tallies(tallies):
... while 1:
... tally = yield from accumulate()
... tallies.append(tally)
...
>>> tallies = []
>>> acc = gather_tallies(tallies)
>>> next(acc) # Ensure the accumulator is ready to accept values
>>> for i in range(4):
... acc.send(i)
...
>>> acc.send(None) # Finish the first tally
>>> for i in range(5):
... acc.send(i)
...
>>> acc.send(None) # Finish the second tally
>>> tallies
[6, 10]
The main principle driving this change is to allow even generators that
are designed to be used with the ‘send’ and ‘throw’ methods to be split
into multiple subgenerators as easily as a single large function can be
split into multiple subfunctions.
See also
........
PEP 380(1) - Syntax for Delegating to a Subgenerator
PEP written by Greg Ewing; implementation by Greg Ewing, integrated
into 3.3 by Renaud Blanch, Ryan Kelly and Nick Coghlan;
documentation by Zbigniew Jędrzejewski-Szmek and Nick Coghlan
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0380
File: python.info, Node: PEP 409 Suppressing exception context, Next: PEP 414 Explicit Unicode literals, Prev: PEP 380 Syntax for Delegating to a Subgenerator, Up: What’s New In Python 3 3
1.6.9 PEP 409: Suppressing exception context
--------------------------------------------
PEP 409 introduces new syntax that allows the display of the chained
exception context to be disabled. This allows cleaner error messages in
applications that convert between exception types:
>>> class D:
... def __init__(self, extra):
... self._extra_attributes = extra
... def __getattr__(self, attr):
... try:
... return self._extra_attributes[attr]
... except KeyError:
... raise AttributeError(attr) from None
...
>>> D({}).x
Traceback (most recent call last):
File "", line 1, in
File "", line 8, in __getattr__
AttributeError: x
Without the ‘from None’ suffix to suppress the cause, the original
exception would be displayed by default:
>>> class C:
... def __init__(self, extra):
... self._extra_attributes = extra
... def __getattr__(self, attr):
... try:
... return self._extra_attributes[attr]
... except KeyError:
... raise AttributeError(attr)
...
>>> C({}).x
Traceback (most recent call last):
File "", line 6, in __getattr__
KeyError: 'x'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "", line 8, in __getattr__
AttributeError: x
No debugging capability is lost, as the original exception context
remains available if needed (for example, if an intervening library has
incorrectly suppressed valuable underlying details):
>>> try:
... D({}).x
... except AttributeError as exc:
... print(repr(exc.__context__))
...
KeyError('x',)
See also
........
PEP 409(1) - Suppressing exception context
PEP written by Ethan Furman; implemented by Ethan Furman and Nick
Coghlan.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0409
File: python.info, Node: PEP 414 Explicit Unicode literals, Next: PEP 3155 Qualified name for classes and functions, Prev: PEP 409 Suppressing exception context, Up: What’s New In Python 3 3
1.6.10 PEP 414: Explicit Unicode literals
-----------------------------------------
To ease the transition from Python 2 for Unicode aware Python
applications that make heavy use of Unicode literals, Python 3.3 once
again supports the “‘u’” prefix for string literals. This prefix has no
semantic significance in Python 3, it is provided solely to reduce the
number of purely mechanical changes in migrating to Python 3, making it
easier for developers to focus on the more significant semantic changes
(such as the stricter default separation of binary and text data).
See also
........
PEP 414(1) - Explicit Unicode literals
PEP written by Armin Ronacher.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0414
File: python.info, Node: PEP 3155 Qualified name for classes and functions, Next: PEP 412 Key-Sharing Dictionary, Prev: PEP 414 Explicit Unicode literals, Up: What’s New In Python 3 3
1.6.11 PEP 3155: Qualified name for classes and functions
---------------------------------------------------------
Functions and class objects have a new ‘__qualname__’ attribute
representing the “path” from the module top-level to their definition.
For global functions and classes, this is the same as ‘__name__’. For
other functions and classes, it provides better information about where
they were actually defined, and how they might be accessible from the
global scope.
Example with (non-bound) methods:
>>> class C:
... def meth(self):
... pass
>>> C.meth.__name__
'meth'
>>> C.meth.__qualname__
'C.meth'
Example with nested classes:
>>> class C:
... class D:
... def meth(self):
... pass
...
>>> C.D.__name__
'D'
>>> C.D.__qualname__
'C.D'
>>> C.D.meth.__name__
'meth'
>>> C.D.meth.__qualname__
'C.D.meth'
Example with nested functions:
>>> def outer():
... def inner():
... pass
... return inner
...
>>> outer().__name__
'inner'
>>> outer().__qualname__
'outer..inner'
The string representation of those objects is also changed to include
the new, more precise information:
>>> str(C.D)
""
>>> str(C.D.meth)
''
See also
........
PEP 3155(1) - Qualified name for classes and functions
PEP written and implemented by Antoine Pitrou.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3155
File: python.info, Node: PEP 412 Key-Sharing Dictionary, Next: PEP 362 Function Signature Object, Prev: PEP 3155 Qualified name for classes and functions, Up: What’s New In Python 3 3
1.6.12 PEP 412: Key-Sharing Dictionary
--------------------------------------
Dictionaries used for the storage of objects’ attributes are now able to
share part of their internal storage between each other (namely, the
part which stores the keys and their respective hashes). This reduces
the memory consumption of programs creating many instances of
non-builtin types.
See also
........
PEP 412(1) - Key-Sharing Dictionary
PEP written and implemented by Mark Shannon.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0412
File: python.info, Node: PEP 362 Function Signature Object, Next: PEP 421 Adding sys implementation, Prev: PEP 412 Key-Sharing Dictionary, Up: What’s New In Python 3 3
1.6.13 PEP 362: Function Signature Object
-----------------------------------------
A new function *note inspect.signature(): 55b. makes introspection of
python callables easy and straightforward. A broad range of callables
is supported: python functions, decorated or not, classes, and *note
functools.partial(): 7c8. objects. New classes *note inspect.Signature:
6f3, *note inspect.Parameter: 6f4. and *note inspect.BoundArguments:
9a8. hold information about the call signatures, such as, annotations,
default values, parameters kinds, and bound arguments, which
considerably simplifies writing decorators and any code that validates
or amends calling signatures or arguments.
See also
........
PEP 362(1): - Function Signature Object
PEP written by Brett Cannon, Yury Selivanov, Larry Hastings, Jiwon
Seo; implemented by Yury Selivanov.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0362
File: python.info, Node: PEP 421 Adding sys implementation, Next: Using importlib as the Implementation of Import, Prev: PEP 362 Function Signature Object, Up: What’s New In Python 3 3
1.6.14 PEP 421: Adding sys.implementation
-----------------------------------------
A new attribute on the *note sys: fd. module exposes details specific to
the implementation of the currently running interpreter. The initial
set of attributes on *note sys.implementation: 9aa. are ‘name’,
‘version’, ‘hexversion’, and ‘cache_tag’.
The intention of ‘sys.implementation’ is to consolidate into one
namespace the implementation-specific data used by the standard library.
This allows different Python implementations to share a single standard
library code base much more easily. In its initial state,
‘sys.implementation’ holds only a small portion of the
implementation-specific data. Over time that ratio will shift in order
to make the standard library more portable.
One example of improved standard library portability is ‘cache_tag’. As
of Python 3.3, ‘sys.implementation.cache_tag’ is used by *note
importlib: 9b. to support PEP 3147(1) compliance. Any Python
implementation that uses ‘importlib’ for its built-in import system may
use ‘cache_tag’ to control the caching behavior for modules.
* Menu:
* SimpleNamespace::
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3147
File: python.info, Node: SimpleNamespace, Up: PEP 421 Adding sys implementation
1.6.14.1 SimpleNamespace
........................
The implementation of ‘sys.implementation’ also introduces a new type to
Python: *note types.SimpleNamespace: 9ac. In contrast to a
mapping-based namespace, like *note dict: 1b8, ‘SimpleNamespace’ is
attribute-based, like *note object: 2b0. However, unlike ‘object’,
‘SimpleNamespace’ instances are writable. This means that you can add,
remove, and modify the namespace through normal attribute access.
See also
........
PEP 421(1) - Adding sys.implementation
PEP written and implemented by Eric Snow.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0421
File: python.info, Node: Using importlib as the Implementation of Import, Next: Other Language Changes<6>, Prev: PEP 421 Adding sys implementation, Up: What’s New In Python 3 3
1.6.15 Using importlib as the Implementation of Import
------------------------------------------------------
bpo-2377(1) - Replace __import__ w/ importlib.__import__ bpo-13959(2) -
Re-implement parts of *note imp: 9a. in pure Python bpo-14605(3) - Make
import machinery explicit bpo-14646(4) - Require loaders set __loader__
and __package__
The *note __import__(): 610. function is now powered by *note
importlib.__import__(): 9ae. This work leads to the completion of
“phase 2” of PEP 302(5). There are multiple benefits to this change.
First, it has allowed for more of the machinery powering import to be
exposed instead of being implicit and hidden within the C code. It also
provides a single implementation for all Python VMs supporting Python
3.3 to use, helping to end any VM-specific deviations in import
semantics. And finally it eases the maintenance of import, allowing for
future growth to occur.
For the common user, there should be no visible change in semantics.
For those whose code currently manipulates import or calls import
programmatically, the code changes that might possibly be required are
covered in the *note Porting Python code: 9af. section of this document.
* Menu:
* New APIs::
* Visible Changes::
---------- Footnotes ----------
(1) https://bugs.python.org/issue2377
(2) https://bugs.python.org/issue13959
(3) https://bugs.python.org/issue14605
(4) https://bugs.python.org/issue14646
(5) https://www.python.org/dev/peps/pep-0302
File: python.info, Node: New APIs, Next: Visible Changes, Up: Using importlib as the Implementation of Import
1.6.15.1 New APIs
.................
One of the large benefits of this work is the exposure of what goes into
making the import statement work. That means the various importers that
were once implicit are now fully exposed as part of the *note importlib:
9b. package.
The abstract base classes defined in *note importlib.abc: 9c. have been
expanded to properly delineate between *note meta path finders: 9b1. and
*note path entry finders: 9b2. by introducing *note
importlib.abc.MetaPathFinder: 9b3. and *note
importlib.abc.PathEntryFinder: 9b4, respectively. The old ABC of *note
importlib.abc.Finder: 9b5. is now only provided for
backwards-compatibility and does not enforce any method requirements.
In terms of finders, *note importlib.machinery.FileFinder: 9b6. exposes
the mechanism used to search for source and bytecode files of a module.
Previously this class was an implicit member of *note sys.path_hooks:
95c.
For loaders, the new abstract base class *note importlib.abc.FileLoader:
9b7. helps write a loader that uses the file system as the storage
mechanism for a module’s code. The loader for source files (*note
importlib.machinery.SourceFileLoader: 9b8.), sourceless bytecode files
(*note importlib.machinery.SourcelessFileLoader: 9b9.), and extension
modules (*note importlib.machinery.ExtensionFileLoader: 556.) are now
available for direct use.
*note ImportError: 334. now has ‘name’ and ‘path’ attributes which are
set when there is relevant data to provide. The message for failed
imports will also provide the full name of the module now instead of
just the tail end of the module’s name.
The *note importlib.invalidate_caches(): 49c. function will now call the
method with the same name on all finders cached in *note
sys.path_importer_cache: 49d. to help clean up any stored state as
necessary.
File: python.info, Node: Visible Changes, Prev: New APIs, Up: Using importlib as the Implementation of Import
1.6.15.2 Visible Changes
........................
For potential required changes to code, see the *note Porting Python
code: 9af. section.
Beyond the expanse of what *note importlib: 9b. now exposes, there are
other visible changes to import. The biggest is that *note
sys.meta_path: 5e6. and *note sys.path_hooks: 95c. now store all of the
meta path finders and path entry hooks used by import. Previously the
finders were implicit and hidden within the C code of import instead of
being directly exposed. This means that one can now easily remove or
change the order of the various finders to fit one’s needs.
Another change is that all modules have a ‘__loader__’ attribute,
storing the loader used to create the module. PEP 302(1) has been
updated to make this attribute mandatory for loaders to implement, so in
the future once 3rd-party loaders have been updated people will be able
to rely on the existence of the attribute. Until such time, though,
import is setting the module post-load.
Loaders are also now expected to set the ‘__package__’ attribute from
PEP 366(2). Once again, import itself is already setting this on all
loaders from *note importlib: 9b. and import itself is setting the
attribute post-load.
‘None’ is now inserted into *note sys.path_importer_cache: 49d. when no
finder can be found on *note sys.path_hooks: 95c. Since *note
imp.NullImporter: 9bb. is not directly exposed on *note sys.path_hooks:
95c. it could no longer be relied upon to always be available to use as
a value representing no finder found.
All other changes relate to semantic changes which should be taken into
consideration when updating code for Python 3.3, and thus should be read
about in the *note Porting Python code: 9af. section of this document.
(Implementation by Brett Cannon)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0302
(2) https://www.python.org/dev/peps/pep-0366
File: python.info, Node: Other Language Changes<6>, Next: A Finer-Grained Import Lock, Prev: Using importlib as the Implementation of Import, Up: What’s New In Python 3 3
1.6.16 Other Language Changes
-----------------------------
Some smaller changes made to the core Python language are:
* Added support for Unicode name aliases and named sequences. Both
*note unicodedata.lookup(): 9bd. and ‘'\N{...}'’ now resolve name
aliases, and *note unicodedata.lookup(): 9bd. resolves named
sequences too.
(Contributed by Ezio Melotti in bpo-12753(1).)
* Unicode database updated to UCD version 6.1.0
* Equality comparisons on *note range(): 9be. objects now return a
result reflecting the equality of the underlying sequences
generated by those range objects. (bpo-13201(2))
* The ‘count()’, ‘find()’, ‘rfind()’, ‘index()’ and ‘rindex()’
methods of *note bytes: 331. and *note bytearray: 332. objects now
accept an integer between 0 and 255 as their first argument.
(Contributed by Petri Lehtinen in bpo-12170(3).)
* The ‘rjust()’, ‘ljust()’, and ‘center()’ methods of *note bytes:
331. and *note bytearray: 332. now accept a *note bytearray: 332.
for the ‘fill’ argument. (Contributed by Petri Lehtinen in
bpo-12380(4).)
* New methods have been added to *note list: 262. and *note
bytearray: 332.: ‘copy()’ and ‘clear()’ (bpo-10516(5)).
Consequently, *note MutableSequence: 6ac. now also defines a
‘clear()’ method (bpo-11388(6)).
* Raw bytes literals can now be written ‘rb"..."’ as well as
‘br"..."’.
(Contributed by Antoine Pitrou in bpo-13748(7).)
* *note dict.setdefault(): 9bf. now does only one lookup for the
given key, making it atomic when used with built-in types.
(Contributed by Filip Gruszczyński in bpo-13521(8).)
* The error messages produced when a function call does not match the
function signature have been significantly improved.
(Contributed by Benjamin Peterson.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12753
(2) https://bugs.python.org/issue13201
(3) https://bugs.python.org/issue12170
(4) https://bugs.python.org/issue12380
(5) https://bugs.python.org/issue10516
(6) https://bugs.python.org/issue11388
(7) https://bugs.python.org/issue13748
(8) https://bugs.python.org/issue13521
File: python.info, Node: A Finer-Grained Import Lock, Next: Builtin functions and types, Prev: Other Language Changes<6>, Up: What’s New In Python 3 3
1.6.17 A Finer-Grained Import Lock
----------------------------------
Previous versions of CPython have always relied on a global import lock.
This led to unexpected annoyances, such as deadlocks when importing a
module would trigger code execution in a different thread as a
side-effect. Clumsy workarounds were sometimes employed, such as the
*note PyImport_ImportModuleNoBlock(): 9c1. C API function.
In Python 3.3, importing a module takes a per-module lock. This
correctly serializes importation of a given module from multiple threads
(preventing the exposure of incompletely initialized modules), while
eliminating the aforementioned annoyances.
(Contributed by Antoine Pitrou in bpo-9260(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9260
File: python.info, Node: Builtin functions and types, Next: New Modules<6>, Prev: A Finer-Grained Import Lock, Up: What’s New In Python 3 3
1.6.18 Builtin functions and types
----------------------------------
* *note open(): 4f0. gets a new `opener' parameter: the underlying
file descriptor for the file object is then obtained by calling
`opener' with (`file', `flags'). It can be used to use custom
flags like *note os.O_CLOEXEC: 9c3. for example. The ‘'x'’ mode
was added: open for exclusive creation, failing if the file already
exists.
* *note print(): 886.: added the `flush' keyword argument. If the
`flush' keyword argument is true, the stream is forcibly flushed.
* *note hash(): 9c4.: hash randomization is enabled by default, see
*note object.__hash__(): 340. and *note PYTHONHASHSEED: 9c5.
* The *note str: 330. type gets a new *note casefold(): 6b0. method:
return a casefolded copy of the string, casefolded strings may be
used for caseless matching. For example, ‘'ß'.casefold()’ returns
‘'ss'’.
* The sequence documentation has been substantially rewritten to
better explain the binary/text sequence distinction and to provide
specific documentation sections for the individual builtin sequence
types (bpo-4966(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue4966
File: python.info, Node: New Modules<6>, Next: Improved Modules<6>, Prev: Builtin functions and types, Up: What’s New In Python 3 3
1.6.19 New Modules
------------------
* Menu:
* faulthandler: faulthandler<3>.
* ipaddress: ipaddress<4>.
* lzma: lzma<2>.
File: python.info, Node: faulthandler<3>, Next: ipaddress<4>, Up: New Modules<6>
1.6.19.1 faulthandler
.....................
This new debug module *note faulthandler: 7d. contains functions to dump
Python tracebacks explicitly, on a fault (a crash like a segmentation
fault), after a timeout, or on a user signal. Call *note
faulthandler.enable(): 548. to install fault handlers for the ‘SIGSEGV’,
‘SIGFPE’, ‘SIGABRT’, ‘SIGBUS’, and ‘SIGILL’ signals. You can also
enable them at startup by setting the *note PYTHONFAULTHANDLER: 9c8.
environment variable or by using *note -X: 155. ‘faulthandler’ command
line option.
Example of a segmentation fault on Linux:
$ python -q -X faulthandler
>>> import ctypes
>>> ctypes.string_at(0)
Fatal Python error: Segmentation fault
Current thread 0x00007fb899f39700:
File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
File "", line 1 in
Segmentation fault
File: python.info, Node: ipaddress<4>, Next: lzma<2>, Prev: faulthandler<3>, Up: New Modules<6>
1.6.19.2 ipaddress
..................
The new *note ipaddress: a2. module provides tools for creating and
manipulating objects representing IPv4 and IPv6 addresses, networks and
interfaces (i.e. an IP address associated with a specific IP subnet).
(Contributed by Google and Peter Moody in PEP 3144(1).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3144
File: python.info, Node: lzma<2>, Prev: ipaddress<4>, Up: New Modules<6>
1.6.19.3 lzma
.............
The newly-added *note lzma: ad. module provides data compression and
decompression using the LZMA algorithm, including support for the ‘.xz’
and ‘.lzma’ file formats.
(Contributed by Nadeem Vawda and Per Øyvind Karlsen in bpo-6715(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6715
File: python.info, Node: Improved Modules<6>, Next: Optimizations<5>, Prev: New Modules<6>, Up: What’s New In Python 3 3
1.6.20 Improved Modules
-----------------------
* Menu:
* abc: abc<2>.
* array: array<2>.
* base64: base64<2>.
* binascii: binascii<3>.
* bz2: bz2<2>.
* codecs::
* collections: collections<7>.
* contextlib: contextlib<5>.
* crypt: crypt<2>.
* curses: curses<3>.
* datetime: datetime<4>.
* decimal: decimal<3>.
* email: email<4>.
* ftplib::
* functools: functools<5>.
* gc: gc<4>.
* hmac: hmac<3>.
* http: http<3>.
* html: html<2>.
* imaplib: imaplib<2>.
* inspect: inspect<5>.
* io: io<4>.
* itertools: itertools<3>.
* logging: logging<6>.
* math: math<5>.
* mmap: mmap<3>.
* multiprocessing: multiprocessing<6>.
* nntplib::
* os: os<7>.
* pdb: pdb<4>.
* pickle: pickle<5>.
* pydoc: pydoc<4>.
* re: re<6>.
* sched::
* select: select<2>.
* shlex: shlex<3>.
* shutil: shutil<4>.
* signal: signal<3>.
* smtpd: smtpd<3>.
* smtplib: smtplib<3>.
* socket: socket<7>.
* socketserver: socketserver<3>.
* sqlite3: sqlite3<5>.
* ssl: ssl<8>.
* stat: stat<2>.
* struct: struct<3>.
* subprocess: subprocess<5>.
* sys: sys<7>.
* tarfile: tarfile<4>.
* tempfile::
* textwrap: textwrap<2>.
* threading: threading<5>.
* time: time<5>.
* types: types<4>.
* unittest: unittest<5>.
* urllib: urllib<3>.
* webbrowser::
* xml.etree.ElementTree: xml etree ElementTree.
* zlib: zlib<2>.
File: python.info, Node: abc<2>, Next: array<2>, Up: Improved Modules<6>
1.6.20.1 abc
............
Improved support for abstract base classes containing descriptors
composed with abstract methods. The recommended approach to declaring
abstract descriptors is now to provide ‘__isabstractmethod__’ as a
dynamically updated property. The built-in descriptors have been
updated accordingly.
* *note abc.abstractproperty: 9cd. has been deprecated, use
*note property: 1d7. with *note abc.abstractmethod(): 9ce.
instead.
* *note abc.abstractclassmethod: 9cf. has been deprecated, use
*note classmethod: 1d8. with *note abc.abstractmethod(): 9ce.
instead.
* *note abc.abstractstaticmethod: 9d0. has been deprecated, use
*note staticmethod: 1d9. with *note abc.abstractmethod(): 9ce.
instead.
(Contributed by Darren Dale in bpo-11610(1).)
*note abc.ABCMeta.register(): 9d1. now returns the registered subclass,
which means it can now be used as a class decorator (bpo-10868(2)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue11610
(2) https://bugs.python.org/issue10868
File: python.info, Node: array<2>, Next: base64<2>, Prev: abc<2>, Up: Improved Modules<6>
1.6.20.2 array
..............
The *note array: 7. module supports the ‘long long’ type using ‘q’ and
‘Q’ type codes.
(Contributed by Oren Tirosh and Hirokazu Yamamoto in bpo-1172711(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1172711
File: python.info, Node: base64<2>, Next: binascii<3>, Prev: array<2>, Up: Improved Modules<6>
1.6.20.3 base64
...............
ASCII-only Unicode strings are now accepted by the decoding functions of
the *note base64: e. modern interface. For example,
‘base64.b64decode('YWJj')’ returns ‘b'abc'’. (Contributed by Catalin
Iacob in bpo-13641(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13641
File: python.info, Node: binascii<3>, Next: bz2<2>, Prev: base64<2>, Up: Improved Modules<6>
1.6.20.4 binascii
.................
In addition to the binary objects they normally accept, the ‘a2b_’
functions now all also accept ASCII-only strings as input. (Contributed
by Antoine Pitrou in bpo-13637(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13637
File: python.info, Node: bz2<2>, Next: codecs, Prev: binascii<3>, Up: Improved Modules<6>
1.6.20.5 bz2
............
The *note bz2: 14. module has been rewritten from scratch. In the
process, several new features have been added:
* New *note bz2.open(): 9d6. function: open a bzip2-compressed file
in binary or text mode.
* *note bz2.BZ2File: 931. can now read from and write to arbitrary
file-like objects, by means of its constructor’s `fileobj'
argument.
(Contributed by Nadeem Vawda in bpo-5863(1).)
* *note bz2.BZ2File: 931. and *note bz2.decompress(): 9d7. can now
decompress multi-stream inputs (such as those produced by the
‘pbzip2’ tool). *note bz2.BZ2File: 931. can now also be used to
create this type of file, using the ‘'a'’ (append) mode.
(Contributed by Nir Aides in bpo-1625(2).)
* *note bz2.BZ2File: 931. now implements all of the *note
io.BufferedIOBase: 588. API, except for the ‘detach()’ and
‘truncate()’ methods.
---------- Footnotes ----------
(1) https://bugs.python.org/issue5863
(2) https://bugs.python.org/issue1625
File: python.info, Node: codecs, Next: collections<7>, Prev: bz2<2>, Up: Improved Modules<6>
1.6.20.6 codecs
...............
The *note mbcs: 78. codec has been rewritten to handle correctly
‘replace’ and ‘ignore’ error handlers on all Windows versions. The
*note mbcs: 78. codec now supports all error handlers, instead of only
‘replace’ to encode and ‘ignore’ to decode.
A new Windows-only codec has been added: ‘cp65001’ (bpo-13216(1)). It
is the Windows code page 65001 (Windows UTF-8, ‘CP_UTF8’). For example,
it is used by ‘sys.stdout’ if the console output code page is set to
cp65001 (e.g., using ‘chcp 65001’ command).
Multibyte CJK decoders now resynchronize faster. They only ignore the
first byte of an invalid byte sequence. For example,
‘b'\xff\n'.decode('gb2312', 'replace')’ now returns a ‘\n’ after the
replacement character.
(bpo-12016(2))
Incremental CJK codec encoders are no longer reset at each call to their
encode() methods. For example:
>>> import codecs
>>> encoder = codecs.getincrementalencoder('hz')('strict')
>>> b''.join(encoder.encode(x) for x in '\u52ff\u65bd\u65bc\u4eba\u3002 Bye.')
b'~{NpJ)l6HK!#~} Bye.'
This example gives ‘b'~{Np~}~{J)~}~{l6~}~{HK~}~{!#~} Bye.'’ with older
Python versions.
(bpo-12100(3))
The ‘unicode_internal’ codec has been deprecated.
---------- Footnotes ----------
(1) https://bugs.python.org/issue13216
(2) https://bugs.python.org/issue12016
(3) https://bugs.python.org/issue12100
File: python.info, Node: collections<7>, Next: contextlib<5>, Prev: codecs, Up: Improved Modules<6>
1.6.20.7 collections
....................
Addition of a new *note ChainMap: 4a9. class to allow treating a number
of mappings as a single unit. (Written by Raymond Hettinger for
bpo-11089(1), made public in bpo-11297(2).)
The abstract base classes have been moved in a new *note
collections.abc: 1f. module, to better differentiate between the
abstract and the concrete collections classes. Aliases for ABCs are
still present in the *note collections: 1e. module to preserve existing
imports. (bpo-11085(3))
The *note Counter: 9da. class now supports the unary ‘+’ and ‘-’
operators, as well as the in-place operators ‘+=’, ‘-=’, ‘|=’, and ‘&=’.
(Contributed by Raymond Hettinger in bpo-13121(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11089
(2) https://bugs.python.org/issue11297
(3) https://bugs.python.org/issue11085
(4) https://bugs.python.org/issue13121
File: python.info, Node: contextlib<5>, Next: crypt<2>, Prev: collections<7>, Up: Improved Modules<6>
1.6.20.8 contextlib
...................
*note ExitStack: 376. now provides a solid foundation for programmatic
manipulation of context managers and similar cleanup functionality.
Unlike the previous ‘contextlib.nested’ API (which was deprecated and
removed), the new API is designed to work correctly regardless of
whether context managers acquire their resources in their ‘__init__’
method (for example, file objects) or in their ‘__enter__’ method (for
example, synchronisation objects from the *note threading: 109. module).
(bpo-13585(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue13585
File: python.info, Node: crypt<2>, Next: curses<3>, Prev: contextlib<5>, Up: Improved Modules<6>
1.6.20.9 crypt
..............
Addition of salt and modular crypt format (hashing method) and the *note
mksalt(): 37c. function to the *note crypt: 29. module.
(bpo-10924(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue10924
File: python.info, Node: curses<3>, Next: datetime<4>, Prev: crypt<2>, Up: Improved Modules<6>
1.6.20.10 curses
................
* If the *note curses: 2c. module is linked to the ncursesw
library, use Unicode functions when Unicode strings or
characters are passed (e.g. ‘waddwstr()’), and bytes
functions otherwise (e.g. ‘waddstr()’).
* Use the locale encoding instead of ‘utf-8’ to encode Unicode
strings.
* ‘curses.window’ has a new *note curses.window.encoding: 9de.
attribute.
* The ‘curses.window’ class has a new *note get_wch(): 9df.
method to get a wide character
* The *note curses: 2c. module has a new *note unget_wch(): 9e0.
function to push a wide character so the next *note get_wch():
9df. will return it
(Contributed by Iñigo Serna in bpo-6755(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6755
File: python.info, Node: datetime<4>, Next: decimal<3>, Prev: curses<3>, Up: Improved Modules<6>
1.6.20.11 datetime
..................
* Equality comparisons between naive and aware *note datetime:
194. instances now return *note False: 388. instead of raising
*note TypeError: 192. (bpo-15006(1)).
* New *note datetime.datetime.timestamp(): 9e2. method: Return
POSIX timestamp corresponding to the *note datetime: 194.
instance.
* The *note datetime.datetime.strftime(): 537. method supports
formatting years older than 1000.
* The *note datetime.datetime.astimezone(): 196. method can now
be called without arguments to convert datetime instance to
the system timezone.
---------- Footnotes ----------
(1) https://bugs.python.org/issue15006
File: python.info, Node: decimal<3>, Next: email<4>, Prev: datetime<4>, Up: Improved Modules<6>
1.6.20.12 decimal
.................
bpo-7652(1) - integrate fast native decimal arithmetic.
C-module and libmpdec written by Stefan Krah.
The new C version of the decimal module integrates the high speed
libmpdec library for arbitrary precision correctly-rounded decimal
floating point arithmetic. libmpdec conforms to IBM’s General Decimal
Arithmetic Specification.
Performance gains range from 10x for database applications to 100x for
numerically intensive applications. These numbers are expected gains
for standard precisions used in decimal floating point arithmetic.
Since the precision is user configurable, the exact figures may vary.
For example, in integer bignum arithmetic the differences can be
significantly higher.
The following table is meant as an illustration. Benchmarks are
available at ‘http://www.bytereef.org/mpdecimal/quickstart.html’.
decimal.py _decimal speedup
---------------------------------------------------------------------
pi 42.02s 0.345s 120x
telco 172.19s 5.68s 30x
psycopg 3.57s 0.29s 12x
* Menu:
* Features: Features<2>.
* API changes: API changes<2>.
---------- Footnotes ----------
(1) https://bugs.python.org/issue7652
File: python.info, Node: Features<2>, Next: API changes<2>, Up: decimal<3>
1.6.20.13 Features
..................
* The *note FloatOperation: 9e5. signal optionally enables stricter
semantics for mixing floats and Decimals.
* If Python is compiled without threads, the C version automatically
disables the expensive thread local context machinery. In this
case, the variable *note HAVE_THREADS: 9e6. is set to ‘False’.
File: python.info, Node: API changes<2>, Prev: Features<2>, Up: decimal<3>
1.6.20.14 API changes
.....................
* The C module has the following context limits, depending on the
machine architecture:
32-bit 64-bit
-------------------------------------------------------------------------------------
‘MAX_PREC’ ‘425000000’ ‘999999999999999999’
‘MAX_EMAX’ ‘425000000’ ‘999999999999999999’
‘MIN_EMIN’ ‘-425000000’ ‘-999999999999999999’
* In the context templates (*note DefaultContext: 9e8, *note
BasicContext: 9e9. and *note ExtendedContext: 9ea.) the magnitude
of ‘Emax’ and ‘Emin’ has changed to ‘999999’.
* The *note Decimal: 188. constructor in decimal.py does not observe
the context limits and converts values with arbitrary exponents or
precision exactly. Since the C version has internal limits, the
following scheme is used: If possible, values are converted
exactly, otherwise *note InvalidOperation: 9eb. is raised and the
result is NaN. In the latter case it is always possible to use
*note create_decimal(): 9ec. in order to obtain a rounded or
inexact value.
* The power function in decimal.py is always correctly-rounded. In
the C version, it is defined in terms of the correctly-rounded
*note exp(): 9ed. and *note ln(): 9ee. functions, but the final
result is only “almost always correctly rounded”.
* In the C version, the context dictionary containing the signals is
a *note MutableMapping: 9ef. For speed reasons, ‘flags’ and
‘traps’ always refer to the same *note MutableMapping: 9ef. that
the context was initialized with. If a new signal dictionary is
assigned, ‘flags’ and ‘traps’ are updated with the new values, but
they do not reference the RHS dictionary.
* Pickling a *note Context: 9f0. produces a different output in order
to have a common interchange format for the Python and C versions.
* The order of arguments in the *note Context: 9f0. constructor has
been changed to match the order displayed by *note repr(): 7cc.
* The ‘watchexp’ parameter in the *note quantize(): 9f1. method is
deprecated.
File: python.info, Node: email<4>, Next: ftplib, Prev: decimal<3>, Up: Improved Modules<6>
1.6.20.15 email
...............
* Menu:
* Policy Framework::
* Provisional Policy with New Header API::
* Other API Changes::
File: python.info, Node: Policy Framework, Next: Provisional Policy with New Header API, Up: email<4>
1.6.20.16 Policy Framework
..........................
The email package now has a *note policy: 75. framework. A *note
Policy: 9f4. is an object with several methods and properties that
control how the email package behaves. The primary policy for Python
3.3 is the *note Compat32: 9f5. policy, which provides backward
compatibility with the email package in Python 3.2. A ‘policy’ can be
specified when an email message is parsed by a *note parser: 74, or when
a *note Message: 541. object is created, or when an email is serialized
using a *note generator: 6e. Unless overridden, a policy passed to a
‘parser’ is inherited by all the ‘Message’ object and sub-objects
created by the ‘parser’. By default a ‘generator’ will use the policy
of the ‘Message’ object it is serializing. The default policy is *note
compat32: 540.
The minimum set of controls implemented by all ‘policy’ objects are:
max_line_length The maximum length, excluding the linesep character(s),
individual lines may have when a ‘Message’ is serialized.
Defaults to 78.
linesep The character used to separate individual lines when a
‘Message’ is serialized. Defaults to ‘\n’.
cte_type ‘7bit’ or ‘8bit’. ‘8bit’ applies only to a ‘Bytes’
‘generator’, and means that non-ASCII may be used where
allowed by the protocol (or where it exists in the
original input).
raise_on_defect Causes a ‘parser’ to raise error when defects are
encountered instead of adding them to the ‘Message’
object’s ‘defects’ list.
A new policy instance, with new settings, is created using the *note
clone(): 9f6. method of policy objects. ‘clone’ takes any of the above
controls as keyword arguments. Any control not specified in the call
retains its default value. Thus you can create a policy that uses
‘\r\n’ linesep characters like this:
mypolicy = compat32.clone(linesep='\r\n')
Policies can be used to make the generation of messages in the format
needed by your application simpler. Instead of having to remember to
specify ‘linesep='\r\n'’ in all the places you call a ‘generator’, you
can specify it once, when you set the policy used by the ‘parser’ or the
‘Message’, whichever your program uses to create ‘Message’ objects. On
the other hand, if you need to generate messages in multiple forms, you
can still specify the parameters in the appropriate ‘generator’ call.
Or you can have custom policy instances for your different cases, and
pass those in when you create the ‘generator’.
File: python.info, Node: Provisional Policy with New Header API, Next: Other API Changes, Prev: Policy Framework, Up: email<4>
1.6.20.17 Provisional Policy with New Header API
................................................
While the policy framework is worthwhile all by itself, the main
motivation for introducing it is to allow the creation of new policies
that implement new features for the email package in a way that
maintains backward compatibility for those who do not use the new
policies. Because the new policies introduce a new API, we are
releasing them in Python 3.3 as a *note provisional policy: 980.
Backwards incompatible changes (up to and including removal of the code)
may occur if deemed necessary by the core developers.
The new policies are instances of *note EmailPolicy: 9f8, and add the
following additional controls:
refold_source Controls whether or not headers parsed by a
*note parser: 74. are refolded by the
*note generator: 6e. It can be ‘none’, ‘long’, or ‘all’.
The default is ‘long’, which means that source headers
with a line longer than ‘max_line_length’ get refolded.
‘none’ means no line get refolded, and ‘all’ means that
all lines get refolded.
header_factory A callable that take a ‘name’ and ‘value’ and produces a
custom header object.
The ‘header_factory’ is the key to the new features provided by the new
policies. When one of the new policies is used, any header retrieved
from a ‘Message’ object is an object produced by the ‘header_factory’,
and any time you set a header on a ‘Message’ it becomes an object
produced by ‘header_factory’. All such header objects have a ‘name’
attribute equal to the header name. Address and Date headers have
additional attributes that give you access to the parsed data of the
header. This means you can now do things like this:
>>> m = Message(policy=SMTP)
>>> m['To'] = 'Éric '
>>> m['to']
'Éric '
>>> m['to'].addresses
(Address(display_name='Éric', username='foo', domain='example.com'),)
>>> m['to'].addresses[0].username
'foo'
>>> m['to'].addresses[0].display_name
'Éric'
>>> m['Date'] = email.utils.localtime()
>>> m['Date'].datetime
datetime.datetime(2012, 5, 25, 21, 39, 24, 465484, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000), 'EDT'))
>>> m['Date']
'Fri, 25 May 2012 21:44:27 -0400'
>>> print(m)
To: =?utf-8?q?=C3=89ric?=
Date: Fri, 25 May 2012 21:44:27 -0400
You will note that the unicode display name is automatically encoded as
‘utf-8’ when the message is serialized, but that when the header is
accessed directly, you get the unicode version. This eliminates any
need to deal with the *note email.header: 6f. *note decode_header():
9f9. or *note make_header(): 9fa. functions.
You can also create addresses from parts:
>>> m['cc'] = [Group('pals', [Address('Bob', 'bob', 'example.com'),
... Address('Sally', 'sally', 'example.com')]),
... Address('Bonzo', addr_spec='bonz@laugh.com')]
>>> print(m)
To: =?utf-8?q?=C3=89ric?=
Date: Fri, 25 May 2012 21:44:27 -0400
cc: pals: Bob , Sally ;, Bonzo
Decoding to unicode is done automatically:
>>> m2 = message_from_string(str(m))
>>> m2['to']
'Éric '
When you parse a message, you can use the ‘addresses’ and ‘groups’
attributes of the header objects to access the groups and individual
addresses:
>>> m2['cc'].addresses
(Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com'), Address(display_name='Bonzo', username='bonz', domain='laugh.com'))
>>> m2['cc'].groups
(Group(display_name='pals', addresses=(Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com')), Group(display_name=None, addresses=(Address(display_name='Bonzo', username='bonz', domain='laugh.com'),))
In summary, if you use one of the new policies, header manipulation
works the way it ought to: your application works with unicode strings,
and the email package transparently encodes and decodes the unicode to
and from the RFC standard Content Transfer Encodings.
File: python.info, Node: Other API Changes, Prev: Provisional Policy with New Header API, Up: email<4>
1.6.20.18 Other API Changes
...........................
New *note BytesHeaderParser: 9fc, added to the *note parser: 74. module
to complement *note HeaderParser: 9fd. and complete the Bytes API.
New utility functions:
* *note format_datetime(): 9fe.: given a *note datetime: 194,
produce a string formatted for use in an email header.
* *note parsedate_to_datetime(): 9ff.: given a date string from
an email header, convert it into an aware *note datetime: 194,
or a naive *note datetime: 194. if the offset is ‘-0000’.
* *note localtime(): a00.: With no argument, returns the current
local time as an aware *note datetime: 194. using the local
*note timezone: a01. Given an aware *note datetime: 194,
converts it into an aware *note datetime: 194. using the local
*note timezone: a01.
File: python.info, Node: ftplib, Next: functools<5>, Prev: email<4>, Up: Improved Modules<6>
1.6.20.19 ftplib
................
* *note ftplib.FTP: 2f0. now accepts a ‘source_address’ keyword
argument to specify the ‘(host, port)’ to use as the source address
in the bind call when creating the outgoing socket. (Contributed
by Giampaolo Rodolà in bpo-8594(1).)
* The *note FTP_TLS: a03. class now provides a new *note ccc(): a04.
function to revert control channel back to plaintext. This can be
useful to take advantage of firewalls that know how to handle NAT
with non-secure FTP without opening fixed ports. (Contributed by
Giampaolo Rodolà in bpo-12139(2).)
* Added *note ftplib.FTP.mlsd(): a05. method which provides a
parsable directory listing format and deprecates *note
ftplib.FTP.nlst(): a06. and *note ftplib.FTP.dir(): a07.
(Contributed by Giampaolo Rodolà in bpo-11072(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8594
(2) https://bugs.python.org/issue12139
(3) https://bugs.python.org/issue11072
File: python.info, Node: functools<5>, Next: gc<4>, Prev: ftplib, Up: Improved Modules<6>
1.6.20.20 functools
...................
The *note functools.lru_cache(): 1c7. decorator now accepts a ‘typed’
keyword argument (that defaults to ‘False’ to ensure that it caches
values of different types that compare equal in separate cache slots.
(Contributed by Raymond Hettinger in bpo-13227(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13227
File: python.info, Node: gc<4>, Next: hmac<3>, Prev: functools<5>, Up: Improved Modules<6>
1.6.20.21 gc
............
It is now possible to register callbacks invoked by the garbage
collector before and after collection using the new *note callbacks:
a0a. list.
File: python.info, Node: hmac<3>, Next: http<3>, Prev: gc<4>, Up: Improved Modules<6>
1.6.20.22 hmac
..............
A new *note compare_digest(): a0c. function has been added to prevent
side channel attacks on digests through timing analysis. (Contributed
by Nick Coghlan and Christian Heimes in bpo-15061(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15061
File: python.info, Node: http<3>, Next: html<2>, Prev: hmac<3>, Up: Improved Modules<6>
1.6.20.23 http
..............
*note http.server.BaseHTTPRequestHandler: a0e. now buffers the headers
and writes them all at once when *note end_headers(): a0f. is called. A
new method *note flush_headers(): a10. can be used to directly manage
when the accumulated headers are sent. (Contributed by Andrew Schaaf in
bpo-3709(1).)
*note http.server: 97. now produces valid ‘HTML 4.01 strict’ output.
(Contributed by Ezio Melotti in bpo-13295(2).)
*note http.client.HTTPResponse: a11. now has a *note readinto(): a12.
method, which means it can be used as an *note io.RawIOBase: a13. class.
(Contributed by John Kuhn in bpo-13464(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue3709
(2) https://bugs.python.org/issue13295
(3) https://bugs.python.org/issue13464
File: python.info, Node: html<2>, Next: imaplib<2>, Prev: http<3>, Up: Improved Modules<6>
1.6.20.24 html
..............
*note html.parser.HTMLParser: 7bf. is now able to parse broken markup
without raising errors, therefore the `strict' argument of the
constructor and the ‘HTMLParseError’ exception are now deprecated. The
ability to parse broken markup is the result of a number of bug fixes
that are also available on the latest bug fix releases of Python
2.7/3.2. (Contributed by Ezio Melotti in bpo-15114(1), and
bpo-14538(2), bpo-13993(3), bpo-13960(4), bpo-13358(5), bpo-1745761(6),
bpo-755670(7), bpo-13357(8), bpo-12629(9), bpo-1200313(10),
bpo-670664(11), bpo-13273(12), bpo-12888(13), bpo-7311(14).)
A new *note html5: a15. dictionary that maps HTML5 named character
references to the equivalent Unicode character(s) (e.g. ‘html5['gt;']
== '>'’) has been added to the *note html.entities: 91. module. The
dictionary is now also used by *note HTMLParser: 7bf. (Contributed by
Ezio Melotti in bpo-11113(15) and bpo-15156(16).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue15114
(2) https://bugs.python.org/issue14538
(3) https://bugs.python.org/issue13993
(4) https://bugs.python.org/issue13960
(5) https://bugs.python.org/issue13358
(6) https://bugs.python.org/issue1745761
(7) https://bugs.python.org/issue755670
(8) https://bugs.python.org/issue13357
(9) https://bugs.python.org/issue12629
(10) https://bugs.python.org/issue1200313
(11) https://bugs.python.org/issue670664
(12) https://bugs.python.org/issue13273
(13) https://bugs.python.org/issue12888
(14) https://bugs.python.org/issue7311
(15) https://bugs.python.org/issue11113
(16) https://bugs.python.org/issue15156
File: python.info, Node: imaplib<2>, Next: inspect<5>, Prev: html<2>, Up: Improved Modules<6>
1.6.20.25 imaplib
.................
The *note IMAP4_SSL: a17. constructor now accepts an SSLContext
parameter to control parameters of the secure channel.
(Contributed by Sijin Joseph in bpo-8808(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8808
File: python.info, Node: inspect<5>, Next: io<4>, Prev: imaplib<2>, Up: Improved Modules<6>
1.6.20.26 inspect
.................
A new *note getclosurevars(): a19. function has been added. This
function reports the current binding of all names referenced from the
function body and where those names were resolved, making it easier to
verify correct internal state when testing code that relies on stateful
closures.
(Contributed by Meador Inge and Nick Coghlan in bpo-13062(1).)
A new *note getgeneratorlocals(): a1a. function has been added. This
function reports the current binding of local variables in the
generator’s stack frame, making it easier to verify correct internal
state when testing generators.
(Contributed by Meador Inge in bpo-15153(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13062
(2) https://bugs.python.org/issue15153
File: python.info, Node: io<4>, Next: itertools<3>, Prev: inspect<5>, Up: Improved Modules<6>
1.6.20.27 io
............
The *note open(): 65a. function has a new ‘'x'’ mode that can be used to
exclusively create a new file, and raise a *note FileExistsError: 958.
if the file already exists. It is based on the C11 ‘x’ mode to fopen().
(Contributed by David Townshend in bpo-12760(1).)
The constructor of the *note TextIOWrapper: 5f3. class has a new
`write_through' optional argument. If `write_through' is ‘True’, calls
to ‘write()’ are guaranteed not to be buffered: any data written on the
*note TextIOWrapper: 5f3. object is immediately handled to its
underlying binary buffer.
---------- Footnotes ----------
(1) https://bugs.python.org/issue12760
File: python.info, Node: itertools<3>, Next: logging<6>, Prev: io<4>, Up: Improved Modules<6>
1.6.20.28 itertools
...................
*note accumulate(): 1dd. now takes an optional ‘func’ argument for
providing a user-supplied binary function.
File: python.info, Node: logging<6>, Next: math<5>, Prev: itertools<3>, Up: Improved Modules<6>
1.6.20.29 logging
.................
The *note basicConfig(): 1e0. function now supports an optional
‘handlers’ argument taking an iterable of handlers to be added to the
root logger.
A class level attribute ‘append_nul’ has been added to *note
SysLogHandler: a1e. to allow control of the appending of the ‘NUL’
(‘\000’) byte to syslog records, since for some daemons it is required
while for others it is passed through to the log.
File: python.info, Node: math<5>, Next: mmap<3>, Prev: logging<6>, Up: Improved Modules<6>
1.6.20.30 math
..............
The *note math: b1. module has a new function, *note log2(): a20, which
returns the base-2 logarithm of `x'.
(Written by Mark Dickinson in bpo-11888(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11888
File: python.info, Node: mmap<3>, Next: multiprocessing<6>, Prev: math<5>, Up: Improved Modules<6>
1.6.20.31 mmap
..............
The *note read(): a22. method is now more compatible with other
file-like objects: if the argument is omitted or specified as ‘None’, it
returns the bytes from the current file position to the end of the
mapping. (Contributed by Petri Lehtinen in bpo-12021(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12021
File: python.info, Node: multiprocessing<6>, Next: nntplib, Prev: mmap<3>, Up: Improved Modules<6>
1.6.20.32 multiprocessing
.........................
The new *note multiprocessing.connection.wait(): a24. function allows
polling multiple objects (such as connections, sockets and pipes) with a
timeout. (Contributed by Richard Oudkerk in bpo-12328(1).)
‘multiprocessing.Connection’ objects can now be transferred over
multiprocessing connections. (Contributed by Richard Oudkerk in
bpo-4892(2).)
*note multiprocessing.Process: 3b2. now accepts a ‘daemon’ keyword
argument to override the default behavior of inheriting the ‘daemon’
flag from the parent process (bpo-6064(3)).
New attribute *note multiprocessing.Process.sentinel: a25. allows a
program to wait on multiple *note Process: 3b2. objects at one time
using the appropriate OS primitives (for example, *note select: e5. on
posix systems).
New methods *note multiprocessing.pool.Pool.starmap(): a26. and *note
starmap_async(): a27. provide *note itertools.starmap(): a28.
equivalents to the existing *note multiprocessing.pool.Pool.map(): a29.
and *note map_async(): a2a. functions. (Contributed by Hynek Schlawack
in bpo-12708(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12328
(2) https://bugs.python.org/issue4892
(3) https://bugs.python.org/issue6064
(4) https://bugs.python.org/issue12708
File: python.info, Node: nntplib, Next: os<7>, Prev: multiprocessing<6>, Up: Improved Modules<6>
1.6.20.33 nntplib
.................
The *note nntplib.NNTP: a2c. class now supports the context management
protocol to unconditionally consume *note socket.error: 995. exceptions
and to close the NNTP connection when done:
>>> from nntplib import NNTP
>>> with NNTP('news.gmane.org') as n:
... n.group('gmane.comp.python.committers')
...
('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
>>>
(Contributed by Giampaolo Rodolà in bpo-9795(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9795
File: python.info, Node: os<7>, Next: pdb<4>, Prev: nntplib, Up: Improved Modules<6>
1.6.20.34 os
............
* The *note os: c4. module has a new *note pipe2(): a2e. function
that makes it possible to create a pipe with *note O_CLOEXEC: 9c3.
or *note O_NONBLOCK: 723. flags set atomically. This is especially
useful to avoid race conditions in multi-threaded programs.
* The *note os: c4. module has a new *note sendfile(): 359. function
which provides an efficient “zero-copy” way for copying data from
one file (or socket) descriptor to another. The phrase “zero-copy”
refers to the fact that all of the copying of data between the two
descriptors is done entirely by the kernel, with no copying of data
into userspace buffers. *note sendfile(): 359. can be used to
efficiently copy data from a file on disk to a network socket, e.g.
for downloading a file.
(Patch submitted by Ross Lagerwall and Giampaolo Rodolà in
bpo-10882(1).)
* To avoid race conditions like symlink attacks and issues with
temporary files and directories, it is more reliable (and also
faster) to manipulate file descriptors instead of file names.
Python 3.3 enhances existing functions and introduces new functions
to work on file descriptors (bpo-4761(2), bpo-10755(3) and
bpo-14626(4)).
- The *note os: c4. module has a new *note fwalk(): 3b4.
function similar to *note walk(): 654. except that it also
yields file descriptors referring to the directories visited.
This is especially useful to avoid symlink races.
- The following functions get new optional `dir_fd' (*note paths
relative to directory descriptors: a2f.) and/or
`follow_symlinks' (*note not following symlinks: a30.): *note
access(): a31, *note chflags(): a32, *note chmod(): a33, *note
chown(): a34, *note link(): a35, *note lstat(): 1f2, *note
mkdir(): a36, *note mkfifo(): 663, *note mknod(): 664, *note
open(): 665, *note readlink(): 1f3, *note remove(): a37, *note
rename(): a38, *note replace(): a39, *note rmdir(): a3a, *note
stat(): 1f1, *note symlink(): a3b, *note unlink(): a3c, *note
utime(): a3d. Platform support for using these parameters can
be checked via the sets *note os.supports_dir_fd: a3e. and
‘os.supports_follows_symlinks’.
- The following functions now support a file descriptor for
their path argument: *note chdir(): a3f, *note chmod(): a33,
*note chown(): a34, *note execve(): a40, *note listdir(): a41,
*note pathconf(): a42, *note exists(): 1f6, *note stat(): 1f1,
*note statvfs(): a43, *note utime(): a3d. Platform support
for this can be checked via the *note os.supports_fd: a44.
set.
* *note access(): a31. accepts an ‘effective_ids’ keyword argument to
turn on using the effective uid/gid rather than the real uid/gid in
the access check. Platform support for this can be checked via the
*note supports_effective_ids: a45. set.
* The *note os: c4. module has two new functions: *note
getpriority(): a46. and *note setpriority(): a47. They can be used
to get or set process niceness/priority in a fashion similar to
*note os.nice(): a48. but extended to all processes instead of just
the current one.
(Patch submitted by Giampaolo Rodolà in bpo-10784(5).)
* The new *note os.replace(): a39. function allows cross-platform
renaming of a file with overwriting the destination. With *note
os.rename(): a38, an existing destination file is overwritten under
POSIX, but raises an error under Windows. (Contributed by Antoine
Pitrou in bpo-8828(6).)
* The stat family of functions (*note stat(): 1f1, *note fstat():
65f, and *note lstat(): 1f2.) now support reading a file’s
timestamps with nanosecond precision. Symmetrically, *note
utime(): a3d. can now write file timestamps with nanosecond
precision. (Contributed by Larry Hastings in bpo-14127(7).)
* The new *note os.get_terminal_size(): a49. function queries the
size of the terminal attached to a file descriptor. See also *note
shutil.get_terminal_size(): a4a. (Contributed by Zbigniew
Jędrzejewski-Szmek in bpo-13609(8).)
* New functions to support Linux extended attributes (bpo-12720(9)):
*note getxattr(): a4b, *note listxattr(): a4c, *note removexattr():
a4d, *note setxattr(): a4e.
* New interface to the scheduler. These functions control how a
process is allocated CPU time by the operating system. New
functions: *note sched_get_priority_max(): a4f, *note
sched_get_priority_min(): a50, *note sched_getaffinity(): a51,
*note sched_getparam(): a52, *note sched_getscheduler(): a53, *note
sched_rr_get_interval(): a54, *note sched_setaffinity(): a55, *note
sched_setparam(): a56, *note sched_setscheduler(): a57, *note
sched_yield(): a58,
* New functions to control the file system:
* *note posix_fadvise(): 666.: Announces an intention to access
data in a specific pattern thus allowing the kernel to make
optimizations.
* *note posix_fallocate(): 667.: Ensures that enough disk space
is allocated for a file.
* *note sync(): a59.: Force write of everything to disk.
* Additional new posix functions:
* *note lockf(): a5a.: Apply, test or remove a POSIX lock on an
open file descriptor.
* *note pread(): 3b9.: Read from a file descriptor at an offset,
the file offset remains unchanged.
* *note pwrite(): 3bc.: Write to a file descriptor from an
offset, leaving the file offset unchanged.
* *note readv(): 3b8.: Read from a file descriptor into a number
of writable buffers.
* *note truncate(): 724.: Truncate the file corresponding to
`path', so that it is at most `length' bytes in size.
* *note waitid(): 66c.: Wait for the completion of one or more
child processes.
* *note writev(): 3bb.: Write the contents of `buffers' to a
file descriptor, where `buffers' is an arbitrary sequence of
buffers.
* *note getgrouplist(): a5b. (bpo-9344(10)): Return list of
group ids that specified user belongs to.
* *note times(): a5c. and *note uname(): a5d.: Return type changed
from a tuple to a tuple-like object with named attributes.
* Some platforms now support additional constants for the *note
lseek(): a5e. function, such as ‘os.SEEK_HOLE’ and ‘os.SEEK_DATA’.
* New constants *note RTLD_LAZY: a5f, *note RTLD_NOW: a60, *note
RTLD_GLOBAL: a61, *note RTLD_LOCAL: a62, *note RTLD_NODELETE: a63,
*note RTLD_NOLOAD: a64, and *note RTLD_DEEPBIND: a65. are available
on platforms that support them. These are for use with the *note
sys.setdlopenflags(): a66. function, and supersede the similar
constants defined in *note ctypes: 2b. and ‘DLFCN’. (Contributed
by Victor Stinner in bpo-13226(11).)
* *note os.symlink(): a3b. now accepts (and ignores) the
‘target_is_directory’ keyword argument on non-Windows platforms, to
ease cross-platform support.
---------- Footnotes ----------
(1) https://bugs.python.org/issue10882
(2) https://bugs.python.org/issue4761
(3) https://bugs.python.org/issue10755
(4) https://bugs.python.org/issue14626
(5) https://bugs.python.org/issue10784
(6) https://bugs.python.org/issue8828
(7) https://bugs.python.org/issue14127
(8) https://bugs.python.org/issue13609
(9) https://bugs.python.org/issue12720
(10) https://bugs.python.org/issue9344
(11) https://bugs.python.org/issue13226
File: python.info, Node: pdb<4>, Next: pickle<5>, Prev: os<7>, Up: Improved Modules<6>
1.6.20.35 pdb
.............
Tab-completion is now available not only for command names, but also
their arguments. For example, for the ‘break’ command, function and
file names are completed.
(Contributed by Georg Brandl in bpo-14210(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue14210
File: python.info, Node: pickle<5>, Next: pydoc<4>, Prev: pdb<4>, Up: Improved Modules<6>
1.6.20.36 pickle
................
*note pickle.Pickler: 20d. objects now have an optional *note
dispatch_table: a69. attribute allowing per-pickler reduction functions
to be set.
(Contributed by Richard Oudkerk in bpo-14166(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14166
File: python.info, Node: pydoc<4>, Next: re<6>, Prev: pickle<5>, Up: Improved Modules<6>
1.6.20.37 pydoc
...............
The Tk GUI and the ‘serve()’ function have been removed from the *note
pydoc: d9. module: ‘pydoc -g’ and ‘serve()’ have been deprecated in
Python 3.2.
File: python.info, Node: re<6>, Next: sched, Prev: pydoc<4>, Up: Improved Modules<6>
1.6.20.38 re
............
*note str: 330. regular expressions now support ‘\u’ and ‘\U’ escapes.
(Contributed by Serhiy Storchaka in bpo-3665(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue3665
File: python.info, Node: sched, Next: select<2>, Prev: re<6>, Up: Improved Modules<6>
1.6.20.39 sched
...............
* *note run(): a6d. now accepts a `blocking' parameter which when set
to false makes the method execute the scheduled events due to
expire soonest (if any) and then return immediately. This is
useful in case you want to use the *note scheduler: a6e. in
non-blocking applications. (Contributed by Giampaolo Rodolà in
bpo-13449(1).)
* *note scheduler: a6e. class can now be safely used in
multi-threaded environments. (Contributed by Josiah Carlson and
Giampaolo Rodolà in bpo-8684(2).)
* `timefunc' and `delayfunct' parameters of *note scheduler: a6e.
class constructor are now optional and defaults to *note
time.time(): 31d. and *note time.sleep(): 680. respectively.
(Contributed by Chris Clark in bpo-13245(3).)
* *note enter(): a6f. and *note enterabs(): a70. `argument' parameter
is now optional. (Contributed by Chris Clark in bpo-13245(4).)
* *note enter(): a6f. and *note enterabs(): a70. now accept a
`kwargs' parameter. (Contributed by Chris Clark in bpo-13245(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13449
(2) https://bugs.python.org/issue8684
(3) https://bugs.python.org/issue13245
(4) https://bugs.python.org/issue13245
(5) https://bugs.python.org/issue13245
File: python.info, Node: select<2>, Next: shlex<3>, Prev: sched, Up: Improved Modules<6>
1.6.20.40 select
................
Solaris and derivative platforms have a new class *note select.devpoll:
8ab. for high performance asynchronous sockets via ‘/dev/poll’.
(Contributed by Jesús Cea Avión in bpo-6397(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6397
File: python.info, Node: shlex<3>, Next: shutil<4>, Prev: select<2>, Up: Improved Modules<6>
1.6.20.41 shlex
...............
The previously undocumented helper function ‘quote’ from the *note
pipes: cc. modules has been moved to the *note shlex: e8. module and
documented. *note quote(): a73. properly escapes all characters in a
string that might be otherwise given special meaning by the shell.
File: python.info, Node: shutil<4>, Next: signal<3>, Prev: shlex<3>, Up: Improved Modules<6>
1.6.20.42 shutil
................
* New functions:
* *note disk_usage(): a75.: provides total, used and free disk
space statistics. (Contributed by Giampaolo Rodolà in
bpo-12442(1).)
* *note chown(): a76.: allows one to change user and/or group of
the given path also specifying the user/group names and not
only their numeric ids. (Contributed by Sandro Tosi in
bpo-12191(2).)
* *note shutil.get_terminal_size(): a4a.: returns the size of
the terminal window to which the interpreter is attached.
(Contributed by Zbigniew Jędrzejewski-Szmek in bpo-13609(3).)
* *note copy2(): 25a. and *note copystat(): a77. now preserve file
timestamps with nanosecond precision on platforms that support it.
They also preserve file “extended attributes” on Linux.
(Contributed by Larry Hastings in bpo-14127(4) and bpo-15238(5).)
* Several functions now take an optional ‘symlinks’ argument: when
that parameter is true, symlinks aren’t dereferenced and the
operation instead acts on the symlink itself (or creates one, if
relevant). (Contributed by Hynek Schlawack in bpo-12715(6).)
* When copying files to a different file system, *note move(): 25b.
now handles symlinks the way the posix ‘mv’ command does,
recreating the symlink rather than copying the target file
contents. (Contributed by Jonathan Niehof in bpo-9993(7).) *note
move(): 25b. now also returns the ‘dst’ argument as its result.
* *note rmtree(): 21c. is now resistant to symlink attacks on
platforms which support the new ‘dir_fd’ parameter in *note
os.open(): 665. and *note os.unlink(): a3c. (Contributed by Martin
von Löwis and Hynek Schlawack in bpo-4489(8).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12442
(2) https://bugs.python.org/issue12191
(3) https://bugs.python.org/issue13609
(4) https://bugs.python.org/issue14127
(5) https://bugs.python.org/issue15238
(6) https://bugs.python.org/issue12715
(7) https://bugs.python.org/issue9993
(8) https://bugs.python.org/issue4489
File: python.info, Node: signal<3>, Next: smtpd<3>, Prev: shutil<4>, Up: Improved Modules<6>
1.6.20.43 signal
................
* The *note signal: ea. module has new functions:
* *note pthread_sigmask(): a79.: fetch and/or change the signal
mask of the calling thread (Contributed by Jean-Paul Calderone
in bpo-8407(1));
* *note pthread_kill(): a7a.: send a signal to a thread;
* *note sigpending(): a7b.: examine pending functions;
* *note sigwait(): a7c.: wait a signal;
* *note sigwaitinfo(): 67f.: wait for a signal, returning
detailed information about it;
* *note sigtimedwait(): 67e.: like *note sigwaitinfo(): 67f. but
with a timeout.
* The signal handler writes the signal number as a single byte
instead of a nul byte into the wakeup file descriptor. So it is
possible to wait more than one signal and know which signals were
raised.
* *note signal.signal(): a7d. and *note signal.siginterrupt(): a7e.
raise an OSError, instead of a RuntimeError: OSError has an errno
attribute.
---------- Footnotes ----------
(1) https://bugs.python.org/issue8407
File: python.info, Node: smtpd<3>, Next: smtplib<3>, Prev: signal<3>, Up: Improved Modules<6>
1.6.20.44 smtpd
...............
The *note smtpd: ec. module now supports RFC 5321(1) (extended SMTP) and
RFC 1870(2) (size extension). Per the standard, these extensions are
enabled if and only if the client initiates the session with an ‘EHLO’
command.
(Initial ‘ELHO’ support by Alberto Trevino. Size extension by Juhana
Jauhiainen. Substantial additional work on the patch contributed by
Michele Orrù and Dan Boswell. bpo-8739(3))
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc5321.html
(2) https://tools.ietf.org/html/rfc1870.html
(3) https://bugs.python.org/issue8739
File: python.info, Node: smtplib<3>, Next: socket<7>, Prev: smtpd<3>, Up: Improved Modules<6>
1.6.20.45 smtplib
.................
The *note SMTP: a81, *note SMTP_SSL: a82, and *note LMTP: a83. classes
now accept a ‘source_address’ keyword argument to specify the ‘(host,
port)’ to use as the source address in the bind call when creating the
outgoing socket. (Contributed by Paulo Scardine in bpo-11281(1).)
*note SMTP: a81. now supports the context management protocol, allowing
an ‘SMTP’ instance to be used in a ‘with’ statement. (Contributed by
Giampaolo Rodolà in bpo-11289(2).)
The *note SMTP_SSL: a82. constructor and the *note starttls(): a84.
method now accept an SSLContext parameter to control parameters of the
secure channel. (Contributed by Kasun Herath in bpo-8809(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11281
(2) https://bugs.python.org/issue11289
(3) https://bugs.python.org/issue8809
File: python.info, Node: socket<7>, Next: socketserver<3>, Prev: smtplib<3>, Up: Improved Modules<6>
1.6.20.46 socket
................
* The *note socket: 674. class now exposes additional methods to
process ancillary data when supported by the underlying platform:
* *note sendmsg(): 67c.
* *note recvmsg(): 679.
* *note recvmsg_into(): a86.
(Contributed by David Watson in bpo-6560(1), based on an earlier
patch by Heiko Wundram)
* The *note socket: 674. class now supports the PF_CAN protocol
family (‘https://en.wikipedia.org/wiki/Socketcan’), on Linux
(‘https://lwn.net/Articles/253425’).
(Contributed by Matthias Fuchs, updated by Tiago Gonçalves in
bpo-10141(2).)
* The *note socket: 674. class now supports the PF_RDS protocol
family (‘https://en.wikipedia.org/wiki/Reliable_Datagram_Sockets’
and ‘https://oss.oracle.com/projects/rds/’).
* The *note socket: 674. class now supports the ‘PF_SYSTEM’ protocol
family on OS X. (Contributed by Michael Goderbauer in
bpo-13777(3).)
* New function *note sethostname(): a87. allows the hostname to be
set on unix systems if the calling process has sufficient
privileges. (Contributed by Ross Lagerwall in bpo-10866(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6560
(2) https://bugs.python.org/issue10141
(3) https://bugs.python.org/issue13777
(4) https://bugs.python.org/issue10866
File: python.info, Node: socketserver<3>, Next: sqlite3<5>, Prev: socket<7>, Up: Improved Modules<6>
1.6.20.47 socketserver
......................
*note BaseServer: a89. now has an overridable method *note
service_actions(): a8a. that is called by the *note serve_forever():
a8b. method in the service loop. *note ForkingMixIn: 3d5. now uses this
to clean up zombie child processes. (Contributed by Justin Warkentin in
bpo-11109(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11109
File: python.info, Node: sqlite3<5>, Next: ssl<8>, Prev: socketserver<3>, Up: Improved Modules<6>
1.6.20.48 sqlite3
.................
New *note sqlite3.Connection: 3d8. method *note set_trace_callback():
a8d. can be used to capture a trace of all sql commands processed by
sqlite. (Contributed by Torsten Landschoff in bpo-11688(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue11688
File: python.info, Node: ssl<8>, Next: stat<2>, Prev: sqlite3<5>, Up: Improved Modules<6>
1.6.20.49 ssl
.............
* The *note ssl: f3. module has two new random generation functions:
* *note RAND_bytes(): a8f.: generate cryptographically strong
pseudo-random bytes.
* *note RAND_pseudo_bytes(): a90.: generate pseudo-random bytes.
(Contributed by Victor Stinner in bpo-12049(1).)
* The *note ssl: f3. module now exposes a finer-grained exception
hierarchy in order to make it easier to inspect the various kinds
of errors. (Contributed by Antoine Pitrou in bpo-11183(2).)
* *note load_cert_chain(): a91. now accepts a `password' argument to
be used if the private key is encrypted. (Contributed by Adam
Simpkins in bpo-12803(3).)
* Diffie-Hellman key exchange, both regular and Elliptic Curve-based,
is now supported through the *note load_dh_params(): a92. and *note
set_ecdh_curve(): a93. methods. (Contributed by Antoine Pitrou in
bpo-13626(4) and bpo-13627(5).)
* SSL sockets have a new *note get_channel_binding(): a94. method
allowing the implementation of certain authentication mechanisms
such as SCRAM-SHA-1-PLUS. (Contributed by Jacek Konieczny in
bpo-12551(6).)
* You can query the SSL compression algorithm used by an SSL socket,
thanks to its new *note compression(): a95. method. The new
attribute *note OP_NO_COMPRESSION: a96. can be used to disable
compression. (Contributed by Antoine Pitrou in bpo-13634(7).)
* Support has been added for the Next Protocol Negotiation extension
using the *note ssl.SSLContext.set_npn_protocols(): a97. method.
(Contributed by Colin Marc in bpo-14204(8).)
* SSL errors can now be introspected more easily thanks to *note
library: a98. and *note reason: a99. attributes. (Contributed by
Antoine Pitrou in bpo-14837(9).)
* The *note get_server_certificate(): a9a. function now supports
IPv6. (Contributed by Charles-François Natali in bpo-11811(10).)
* New attribute *note OP_CIPHER_SERVER_PREFERENCE: a9b. allows
setting SSLv3 server sockets to use the server’s cipher ordering
preference rather than the client’s (bpo-13635(11)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue12049
(2) https://bugs.python.org/issue11183
(3) https://bugs.python.org/issue12803
(4) https://bugs.python.org/issue13626
(5) https://bugs.python.org/issue13627
(6) https://bugs.python.org/issue12551
(7) https://bugs.python.org/issue13634
(8) https://bugs.python.org/issue14204
(9) https://bugs.python.org/issue14837
(10) https://bugs.python.org/issue11811
(11) https://bugs.python.org/issue13635
File: python.info, Node: stat<2>, Next: struct<3>, Prev: ssl<8>, Up: Improved Modules<6>
1.6.20.50 stat
..............
The undocumented tarfile.filemode function has been moved to *note
stat.filemode(): a9d. It can be used to convert a file’s mode to a
string of the form ‘-rwxrwxrwx’.
(Contributed by Giampaolo Rodolà in bpo-14807(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14807
File: python.info, Node: struct<3>, Next: subprocess<5>, Prev: stat<2>, Up: Improved Modules<6>
1.6.20.51 struct
................
The *note struct: f8. module now supports ‘ssize_t’ and ‘size_t’ via the
new codes ‘n’ and ‘N’, respectively. (Contributed by Antoine Pitrou in
bpo-3163(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue3163
File: python.info, Node: subprocess<5>, Next: sys<7>, Prev: struct<3>, Up: Improved Modules<6>
1.6.20.52 subprocess
....................
Command strings can now be bytes objects on posix platforms.
(Contributed by Victor Stinner in bpo-8513(1).)
A new constant *note DEVNULL: aa0. allows suppressing output in a
platform-independent fashion. (Contributed by Ross Lagerwall in
bpo-5870(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8513
(2) https://bugs.python.org/issue5870
File: python.info, Node: sys<7>, Next: tarfile<4>, Prev: subprocess<5>, Up: Improved Modules<6>
1.6.20.53 sys
.............
The *note sys: fd. module has a new *note thread_info: aa2. *note named
tuple: aa3. holding information about the thread implementation
(bpo-11223(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue11223
File: python.info, Node: tarfile<4>, Next: tempfile, Prev: sys<7>, Up: Improved Modules<6>
1.6.20.54 tarfile
.................
*note tarfile: 101. now supports ‘lzma’ encoding via the *note lzma: ad.
module. (Contributed by Lars Gustäbel in bpo-5689(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5689
File: python.info, Node: tempfile, Next: textwrap<2>, Prev: tarfile<4>, Up: Improved Modules<6>
1.6.20.55 tempfile
..................
*note tempfile.SpooledTemporaryFile: aa6.’s ‘truncate()’ method now
accepts a ‘size’ parameter. (Contributed by Ryan Kelly in bpo-9957(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9957
File: python.info, Node: textwrap<2>, Next: threading<5>, Prev: tempfile, Up: Improved Modules<6>
1.6.20.56 textwrap
..................
The *note textwrap: 108. module has a new *note indent(): aa8. that
makes it straightforward to add a common prefix to selected lines in a
block of text (bpo-13857(1)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue13857
File: python.info, Node: threading<5>, Next: time<5>, Prev: textwrap<2>, Up: Improved Modules<6>
1.6.20.57 threading
...................
*note threading.Condition: aaa, *note threading.Semaphore: aab, *note
threading.BoundedSemaphore: aac, *note threading.Event: aad, and *note
threading.Timer: aae, all of which used to be factory functions
returning a class instance, are now classes and may be subclassed.
(Contributed by Éric Araujo in bpo-10968(1).)
The *note threading.Thread: 235. constructor now accepts a ‘daemon’
keyword argument to override the default behavior of inheriting the
‘daemon’ flag value from the parent thread (bpo-6064(2)).
The formerly private function ‘_thread.get_ident’ is now available as
the public function *note threading.get_ident(): aaf. This eliminates
several cases of direct access to the ‘_thread’ module in the stdlib.
Third party code that used ‘_thread.get_ident’ should likewise be
changed to use the new public interface.
---------- Footnotes ----------
(1) https://bugs.python.org/issue10968
(2) https://bugs.python.org/issue6064
File: python.info, Node: time<5>, Next: types<4>, Prev: threading<5>, Up: Improved Modules<6>
1.6.20.58 time
..............
The PEP 418(1) added new functions to the *note time: 10a. module:
* *note get_clock_info(): ab1.: Get information on a clock.
* *note monotonic(): 76f.: Monotonic clock (cannot go backward), not
affected by system clock updates.
* *note perf_counter(): 2aa.: Performance counter with the highest
available resolution to measure a short duration.
* *note process_time(): 2ab.: Sum of the system and user CPU time of
the current process.
Other new functions:
* *note clock_getres(): ab2, *note clock_gettime(): ab3. and *note
clock_settime(): ab4. functions with ‘CLOCK_xxx’ constants.
(Contributed by Victor Stinner in bpo-10278(2).)
To improve cross platform consistency, *note sleep(): 680. now raises a
*note ValueError: 1fb. when passed a negative sleep value. Previously
this was an error on posix, but produced an infinite sleep on Windows.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0418
(2) https://bugs.python.org/issue10278
File: python.info, Node: types<4>, Next: unittest<5>, Prev: time<5>, Up: Improved Modules<6>
1.6.20.59 types
...............
Add a new *note types.MappingProxyType: ab6. class: Read-only proxy of a
mapping. (bpo-14386(1))
The new functions *note types.new_class(): ab7. and *note
types.prepare_class(): ab8. provide support for PEP 3115(2) compliant
dynamic type creation. (bpo-14588(3))
---------- Footnotes ----------
(1) https://bugs.python.org/issue14386
(2) https://www.python.org/dev/peps/pep-3115
(3) https://bugs.python.org/issue14588
File: python.info, Node: unittest<5>, Next: urllib<3>, Prev: types<4>, Up: Improved Modules<6>
1.6.20.60 unittest
..................
*note assertRaises(): aba, *note assertRaisesRegex(): abb, *note
assertWarns(): abc, and *note assertWarnsRegex(): abd. now accept a
keyword argument `msg' when used as context managers. (Contributed by
Ezio Melotti and Winston Ewert in bpo-10775(1).)
*note unittest.TestCase.run(): abe. now returns the *note TestResult:
abf. object.
---------- Footnotes ----------
(1) https://bugs.python.org/issue10775
File: python.info, Node: urllib<3>, Next: webbrowser, Prev: unittest<5>, Up: Improved Modules<6>
1.6.20.61 urllib
................
The *note Request: 8f6. class, now accepts a `method' argument used by
*note get_method(): ac1. to determine what HTTP method should be used.
For example, this will send a ‘'HEAD'’ request:
>>> urlopen(Request('https://www.python.org', method='HEAD'))
(bpo-1673007(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue1673007
File: python.info, Node: webbrowser, Next: xml etree ElementTree, Prev: urllib<3>, Up: Improved Modules<6>
1.6.20.62 webbrowser
....................
The *note webbrowser: 129. module supports more “browsers”: Google
Chrome (named ‘chrome’, ‘chromium’, ‘chrome-browser’ or
‘chromium-browser’ depending on the version and operating system), and
the generic launchers ‘xdg-open’, from the FreeDesktop.org project, and
‘gvfs-open’, which is the default URI handler for GNOME 3. (The former
contributed by Arnaud Calmettes in bpo-13620(1), the latter by Matthias
Klose in bpo-14493(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue13620
(2) https://bugs.python.org/issue14493
File: python.info, Node: xml etree ElementTree, Next: zlib<2>, Prev: webbrowser, Up: Improved Modules<6>
1.6.20.63 xml.etree.ElementTree
...............................
The *note xml.etree.ElementTree: 137. module now imports its C
accelerator by default; there is no longer a need to explicitly import
‘xml.etree.cElementTree’ (this module stays for backwards compatibility,
but is now deprecated). In addition, the ‘iter’ family of methods of
*note Element: ac4. has been optimized (rewritten in C). The module’s
documentation has also been greatly improved with added examples and a
more detailed reference.
File: python.info, Node: zlib<2>, Prev: xml etree ElementTree, Up: Improved Modules<6>
1.6.20.64 zlib
..............
New attribute *note zlib.Decompress.eof: ac6. makes it possible to
distinguish between a properly-formed compressed stream and an
incomplete or truncated one. (Contributed by Nadeem Vawda in
bpo-12646(1).)
New attribute *note zlib.ZLIB_RUNTIME_VERSION: ac7. reports the version
string of the underlying ‘zlib’ library that is loaded at runtime.
(Contributed by Torsten Landschoff in bpo-12306(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue12646
(2) https://bugs.python.org/issue12306
File: python.info, Node: Optimizations<5>, Next: Build and C API Changes<4>, Prev: Improved Modules<6>, Up: What’s New In Python 3 3
1.6.21 Optimizations
--------------------
Major performance enhancements have been added:
* Thanks to PEP 393(1), some operations on Unicode strings have been
optimized:
* the memory footprint is divided by 2 to 4 depending on the
text
* encode an ASCII string to UTF-8 doesn’t need to encode
characters anymore, the UTF-8 representation is shared with
the ASCII representation
* the UTF-8 encoder has been optimized
* repeating a single ASCII letter and getting a substring of an
ASCII string is 4 times faster
* UTF-8 is now 2x to 4x faster. UTF-16 encoding is now up to 10x
faster.
(Contributed by Serhiy Storchaka, bpo-14624(2), bpo-14738(3) and
bpo-15026(4).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0393
(2) https://bugs.python.org/issue14624
(3) https://bugs.python.org/issue14738
(4) https://bugs.python.org/issue15026
File: python.info, Node: Build and C API Changes<4>, Next: Deprecated<5>, Prev: Optimizations<5>, Up: What’s New In Python 3 3
1.6.22 Build and C API Changes
------------------------------
Changes to Python’s build process and to the C API include:
* New PEP 3118(1) related function:
* *note PyMemoryView_FromMemory(): ac9.
* PEP 393(2) added new Unicode types, macros and functions:
* High-level API:
* *note PyUnicode_CopyCharacters(): aca.
* *note PyUnicode_FindChar(): 440.
* *note PyUnicode_GetLength(): acb, *note
PyUnicode_GET_LENGTH: acc.
* *note PyUnicode_New(): acd.
* *note PyUnicode_Substring(): ace.
* *note PyUnicode_ReadChar(): acf, *note
PyUnicode_WriteChar(): ad0.
* Low-level API:
* *note Py_UCS1: ad1, *note Py_UCS2: ad2, *note Py_UCS4:
ad3. types
* *note PyASCIIObject: ad4. and *note
PyCompactUnicodeObject: ad5. structures
* *note PyUnicode_READY: ad6.
* *note PyUnicode_FromKindAndData(): ad7.
* *note PyUnicode_AsUCS4(): ad8, *note
PyUnicode_AsUCS4Copy(): ad9.
* *note PyUnicode_DATA: ada, *note PyUnicode_1BYTE_DATA:
adb, *note PyUnicode_2BYTE_DATA: adc, *note
PyUnicode_4BYTE_DATA: add.
* *note PyUnicode_KIND: ade. with ‘PyUnicode_Kind’ enum:
*note PyUnicode_WCHAR_KIND: adf, *note
PyUnicode_1BYTE_KIND: ae0, *note PyUnicode_2BYTE_KIND:
ae1, *note PyUnicode_4BYTE_KIND: ae2.
* *note PyUnicode_READ: ae3, *note PyUnicode_READ_CHAR:
ae4, *note PyUnicode_WRITE: ae5.
* *note PyUnicode_MAX_CHAR_VALUE: ae6.
* *note PyArg_ParseTuple: 26a. now accepts a *note bytearray: 332.
for the ‘c’ format (bpo-12380(3)).
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3118
(2) https://www.python.org/dev/peps/pep-0393
(3) https://bugs.python.org/issue12380
File: python.info, Node: Deprecated<5>, Next: Porting to Python 3 3, Prev: Build and C API Changes<4>, Up: What’s New In Python 3 3
1.6.23 Deprecated
-----------------
* Menu:
* Unsupported Operating Systems: Unsupported Operating Systems<2>.
* Deprecated Python modules, functions and methods: Deprecated Python modules functions and methods<4>.
* Deprecated functions and types of the C API: Deprecated functions and types of the C API<3>.
* Deprecated features::
File: python.info, Node: Unsupported Operating Systems<2>, Next: Deprecated Python modules functions and methods<4>, Up: Deprecated<5>
1.6.23.1 Unsupported Operating Systems
......................................
OS/2 and VMS are no longer supported due to the lack of a maintainer.
Windows 2000 and Windows platforms which set ‘COMSPEC’ to ‘command.com’
are no longer supported due to maintenance burden.
OSF support, which was deprecated in 3.2, has been completely removed.
File: python.info, Node: Deprecated Python modules functions and methods<4>, Next: Deprecated functions and types of the C API<3>, Prev: Unsupported Operating Systems<2>, Up: Deprecated<5>
1.6.23.2 Deprecated Python modules, functions and methods
.........................................................
* Passing a non-empty string to ‘object.__format__()’ is deprecated,
and will produce a *note TypeError: 192. in Python 3.4
(bpo-9856(1)).
* The ‘unicode_internal’ codec has been deprecated because of the PEP
393(2), use UTF-8, UTF-16 (‘utf-16-le’ or ‘utf-16-be’), or UTF-32
(‘utf-32-le’ or ‘utf-32-be’)
* *note ftplib.FTP.nlst(): a06. and *note ftplib.FTP.dir(): a07.: use
*note ftplib.FTP.mlsd(): a05.
* ‘platform.popen()’: use the *note subprocess: f9. module. Check
especially the *note Replacing Older Functions with the subprocess
Module: aea. section (bpo-11377(3)).
* bpo-13374(4): The Windows bytes API has been deprecated in the
*note os: c4. module. Use Unicode filenames, instead of bytes
filenames, to not depend on the ANSI code page anymore and to
support any filename.
* bpo-13988(5): The ‘xml.etree.cElementTree’ module is deprecated.
The accelerator is used automatically whenever available.
* The behaviour of ‘time.clock()’ depends on the platform: use the
new *note time.perf_counter(): 2aa. or *note time.process_time():
2ab. function instead, depending on your requirements, to have a
well defined behaviour.
* The ‘os.stat_float_times()’ function is deprecated.
* *note abc: 4. module:
* *note abc.abstractproperty: 9cd. has been deprecated, use
*note property: 1d7. with *note abc.abstractmethod(): 9ce.
instead.
* *note abc.abstractclassmethod: 9cf. has been deprecated, use
*note classmethod: 1d8. with *note abc.abstractmethod(): 9ce.
instead.
* *note abc.abstractstaticmethod: 9d0. has been deprecated, use
*note staticmethod: 1d9. with *note abc.abstractmethod(): 9ce.
instead.
* *note importlib: 9b. package:
* *note importlib.abc.SourceLoader.path_mtime(): aeb. is now
deprecated in favour of *note
importlib.abc.SourceLoader.path_stats(): aec. as bytecode
files now store both the modification time and size of the
source file the bytecode file was compiled from.
---------- Footnotes ----------
(1) https://bugs.python.org/issue9856
(2) https://www.python.org/dev/peps/pep-0393
(3) https://bugs.python.org/issue11377
(4) https://bugs.python.org/issue13374
(5) https://bugs.python.org/issue13988
File: python.info, Node: Deprecated functions and types of the C API<3>, Next: Deprecated features, Prev: Deprecated Python modules functions and methods<4>, Up: Deprecated<5>
1.6.23.3 Deprecated functions and types of the C API
....................................................
The *note Py_UNICODE: aee. has been deprecated by PEP 393(1) and will be
removed in Python 4. All functions using this type are deprecated:
Unicode functions and methods using *note Py_UNICODE: aee. and *note
Py_UNICODE*: aee. types:
* *note PyUnicode_FromUnicode: aef.: use *note
PyUnicode_FromWideChar(): af0. or *note
PyUnicode_FromKindAndData(): ad7.
* *note PyUnicode_AS_UNICODE: af1, *note PyUnicode_AsUnicode(): af2,
*note PyUnicode_AsUnicodeAndSize(): af3.: use *note
PyUnicode_AsWideCharString(): 438.
* *note PyUnicode_AS_DATA: af4.: use *note PyUnicode_DATA: ada. with
*note PyUnicode_READ: ae3. and *note PyUnicode_WRITE: ae5.
* *note PyUnicode_GET_SIZE: af5, *note PyUnicode_GetSize(): af6.: use
*note PyUnicode_GET_LENGTH: acc. or *note PyUnicode_GetLength():
acb.
* *note PyUnicode_GET_DATA_SIZE: af7.: use ‘PyUnicode_GET_LENGTH(str)
* PyUnicode_KIND(str)’ (only work on ready strings)
* *note PyUnicode_AsUnicodeCopy(): af8.: use *note
PyUnicode_AsUCS4Copy(): ad9. or *note PyUnicode_AsWideCharString():
438.
* ‘PyUnicode_GetMax()’
Functions and macros manipulating Py_UNICODE* strings:
* ‘Py_UNICODE_strlen’: use *note PyUnicode_GetLength(): acb. or *note
PyUnicode_GET_LENGTH: acc.
* ‘Py_UNICODE_strcat’: use *note PyUnicode_CopyCharacters(): aca. or
*note PyUnicode_FromFormat(): 7cb.
* ‘Py_UNICODE_strcpy’, ‘Py_UNICODE_strncpy’, ‘Py_UNICODE_COPY’: use
*note PyUnicode_CopyCharacters(): aca. or *note
PyUnicode_Substring(): ace.
* ‘Py_UNICODE_strcmp’: use *note PyUnicode_Compare(): af9.
* ‘Py_UNICODE_strncmp’: use *note PyUnicode_Tailmatch(): afa.
* ‘Py_UNICODE_strchr’, ‘Py_UNICODE_strrchr’: use *note
PyUnicode_FindChar(): 440.
* ‘Py_UNICODE_FILL’: use *note PyUnicode_Fill(): afb.
* ‘Py_UNICODE_MATCH’
Encoders:
* *note PyUnicode_Encode(): afc.: use ‘PyUnicode_AsEncodedObject()’
* *note PyUnicode_EncodeUTF7(): afd.
* *note PyUnicode_EncodeUTF8(): afe.: use *note PyUnicode_AsUTF8():
42b. or *note PyUnicode_AsUTF8String(): aff.
* *note PyUnicode_EncodeUTF32(): b00.
* *note PyUnicode_EncodeUTF16(): b01.
* *note PyUnicode_EncodeUnicodeEscape(): b02. use *note
PyUnicode_AsUnicodeEscapeString(): b03.
* *note PyUnicode_EncodeRawUnicodeEscape(): b04. use *note
PyUnicode_AsRawUnicodeEscapeString(): b05.
* *note PyUnicode_EncodeLatin1(): b06.: use *note
PyUnicode_AsLatin1String(): b07.
* *note PyUnicode_EncodeASCII(): b08.: use *note
PyUnicode_AsASCIIString(): b09.
* *note PyUnicode_EncodeCharmap(): b0a.
* *note PyUnicode_TranslateCharmap(): b0b.
* *note PyUnicode_EncodeMBCS(): b0c.: use *note
PyUnicode_AsMBCSString(): b0d. or *note PyUnicode_EncodeCodePage():
b0e. (with ‘CP_ACP’ code_page)
* ‘PyUnicode_EncodeDecimal()’, *note
PyUnicode_TransformDecimalToASCII(): b0f.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0393
File: python.info, Node: Deprecated features, Prev: Deprecated functions and types of the C API<3>, Up: Deprecated<5>
1.6.23.4 Deprecated features
............................
The *note array: 7. module’s ‘'u'’ format code is now deprecated and
will be removed in Python 4 together with the rest of the (*note
Py_UNICODE: aee.) API.
File: python.info, Node: Porting to Python 3 3, Prev: Deprecated<5>, Up: What’s New In Python 3 3
1.6.24 Porting to Python 3.3
----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code.
* Menu:
* Porting Python code::
* Porting C code::
* Building C extensions::
* Command Line Switch Changes::
File: python.info, Node: Porting Python code, Next: Porting C code, Up: Porting to Python 3 3
1.6.24.1 Porting Python code
............................
* Hash randomization is enabled by default. Set the *note
PYTHONHASHSEED: 9c5. environment variable to ‘0’ to disable hash
randomization. See also the *note object.__hash__(): 340. method.
* bpo-12326(1): On Linux, sys.platform doesn’t contain the major
version anymore. It is now always ‘linux’, instead of ‘linux2’ or
‘linux3’ depending on the Linux version used to build Python.
Replace sys.platform == ‘linux2’ with
sys.platform.startswith(‘linux’), or directly sys.platform ==
‘linux’ if you don’t need to support older Python versions.
* bpo-13847(2), bpo-14180(3): *note time: 10a. and *note datetime:
31.: *note OverflowError: 960. is now raised instead of *note
ValueError: 1fb. if a timestamp is out of range. *note OSError:
1d3. is now raised if C functions ‘gmtime()’ or ‘localtime()’
failed.
* The default finders used by import now utilize a cache of what is
contained within a specific directory. If you create a Python
source file or sourceless bytecode file, make sure to call *note
importlib.invalidate_caches(): 49c. to clear out the cache for the
finders to notice the new file.
* *note ImportError: 334. now uses the full name of the module that
was attempted to be imported. Doctests that check ImportErrors’
message will need to be updated to use the full name of the module
instead of just the tail of the name.
* The `index' argument to *note __import__(): 610. now defaults to 0
instead of -1 and no longer support negative values. It was an
oversight when PEP 328(4) was implemented that the default value
remained -1. If you need to continue to perform a relative import
followed by an absolute import, then perform the relative import
using an index of 1, followed by another import using an index of
0. It is preferred, though, that you use *note
importlib.import_module(): b13. rather than call *note
__import__(): 610. directly.
* *note __import__(): 610. no longer allows one to use an index value
other than 0 for top-level modules. E.g. ‘__import__('sys',
level=1)’ is now an error.
* Because *note sys.meta_path: 5e6. and *note sys.path_hooks: 95c.
now have finders on them by default, you will most likely want to
use ‘list.insert()’ instead of ‘list.append()’ to add to those
lists.
* Because ‘None’ is now inserted into *note sys.path_importer_cache:
49d, if you are clearing out entries in the dictionary of paths
that do not have a finder, you will need to remove keys paired with
values of ‘None’ `and' *note imp.NullImporter: 9bb. to be
backwards-compatible. This will lead to extra overhead on older
versions of Python that re-insert ‘None’ into *note
sys.path_importer_cache: 49d. where it represents the use of
implicit finders, but semantically it should not change anything.
* *note importlib.abc.Finder: 9b5. no longer specifies a
‘find_module()’ abstract method that must be implemented. If you
were relying on subclasses to implement that method, make sure to
check for the method’s existence first. You will probably want to
check for ‘find_loader()’ first, though, in the case of working
with *note path entry finders: 9b2.
* *note pkgutil: cd. has been converted to use *note importlib: 9b.
internally. This eliminates many edge cases where the old
behaviour of the PEP 302(5) import emulation failed to match the
behaviour of the real import system. The import emulation itself
is still present, but is now deprecated. The *note
pkgutil.iter_importers(): b14. and *note pkgutil.walk_packages():
48b. functions special case the standard import hooks so they are
still supported even though they do not provide the non-standard
‘iter_modules()’ method.
* A longstanding RFC-compliance bug (bpo-1079(6)) in the parsing done
by *note email.header.decode_header(): 9f9. has been fixed. Code
that uses the standard idiom to convert encoded headers into
unicode (‘str(make_header(decode_header(h))’) will see no change,
but code that looks at the individual tuples returned by
decode_header will see that whitespace that precedes or follows
‘ASCII’ sections is now included in the ‘ASCII’ section. Code that
builds headers using ‘make_header’ should also continue to work
without change, since ‘make_header’ continues to add whitespace
between ‘ASCII’ and non-‘ASCII’ sections if it is not already
present in the input strings.
* *note email.utils.formataddr(): b15. now does the correct content
transfer encoding when passed non-‘ASCII’ display names. Any code
that depended on the previous buggy behavior that preserved the
non-‘ASCII’ unicode in the formatted output string will need to be
changed (bpo-1690608(7)).
* *note poplib.POP3.quit(): b16. may now raise protocol errors like
all other ‘poplib’ methods. Code that assumes ‘quit’ does not
raise *note poplib.error_proto: b17. errors may need to be changed
if errors on ‘quit’ are encountered by a particular application
(bpo-11291(8)).
* The ‘strict’ argument to *note email.parser.Parser: b18, deprecated
since Python 2.4, has finally been removed.
* The deprecated method ‘unittest.TestCase.assertSameElements’ has
been removed.
* The deprecated variable ‘time.accept2dyear’ has been removed.
* The deprecated ‘Context._clamp’ attribute has been removed from the
*note decimal: 36. module. It was previously replaced by the
public attribute ‘clamp’. (See bpo-8540(9).)
* The undocumented internal helper class ‘SSLFakeFile’ has been
removed from *note smtplib: ed, since its functionality has long
been provided directly by *note socket.socket.makefile(): b19.
* Passing a negative value to *note time.sleep(): 680. on Windows now
raises an error instead of sleeping forever. It has always raised
an error on posix.
* The ‘ast.__version__’ constant has been removed. If you need to
make decisions affected by the AST version, use *note
sys.version_info: b1a. to make the decision.
* Code that used to work around the fact that the *note threading:
109. module used factory functions by subclassing the private
classes will need to change to subclass the now-public classes.
* The undocumented debugging machinery in the threading module has
been removed, simplifying the code. This should have no effect on
production code, but is mentioned here in case any application
debug frameworks were interacting with it (bpo-13550(10)).
---------- Footnotes ----------
(1) https://bugs.python.org/issue12326
(2) https://bugs.python.org/issue13847
(3) https://bugs.python.org/issue14180
(4) https://www.python.org/dev/peps/pep-0328
(5) https://www.python.org/dev/peps/pep-0302
(6) https://bugs.python.org/issue1079
(7) https://bugs.python.org/issue1690608
(8) https://bugs.python.org/issue11291
(9) https://bugs.python.org/issue8540
(10) https://bugs.python.org/issue13550
File: python.info, Node: Porting C code, Next: Building C extensions, Prev: Porting Python code, Up: Porting to Python 3 3
1.6.24.2 Porting C code
.......................
* In the course of changes to the buffer API the undocumented
‘smalltable’ member of the *note Py_buffer: b1b. structure has been
removed and the layout of the ‘PyMemoryViewObject’ has changed.
All extensions relying on the relevant parts in ‘memoryobject.h’ or
‘object.h’ must be rebuilt.
* Due to *note PEP 393: 97c, the *note Py_UNICODE: aee. type and all
functions using this type are deprecated (but will stay available
for at least five years). If you were using low-level Unicode APIs
to construct and access unicode objects and you want to benefit of
the memory footprint reduction provided by PEP 393(1), you have to
convert your code to the new *note Unicode API: b1c.
However, if you only have been using high-level functions such as
*note PyUnicode_Concat(): b1d, *note PyUnicode_Join(): b1e. or
*note PyUnicode_FromFormat(): 7cb, your code will automatically
take advantage of the new unicode representations.
* *note PyImport_GetMagicNumber(): b1f. now returns ‘-1’ upon
failure.
* As a negative value for the `level' argument to *note __import__():
610. is no longer valid, the same now holds for *note
PyImport_ImportModuleLevel(): b20. This also means that the value
of `level' used by *note PyImport_ImportModuleEx(): b21. is now ‘0’
instead of ‘-1’.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0393
File: python.info, Node: Building C extensions, Next: Command Line Switch Changes, Prev: Porting C code, Up: Porting to Python 3 3
1.6.24.3 Building C extensions
..............................
* The range of possible file names for C extensions has been
narrowed. Very rarely used spellings have been suppressed: under
POSIX, files named ‘xxxmodule.so’, ‘xxxmodule.abi3.so’ and
‘xxxmodule.cpython-*.so’ are no longer recognized as implementing
the ‘xxx’ module. If you had been generating such files, you have
to switch to the other spellings (i.e., remove the ‘module’ string
from the file names).
(implemented in bpo-14040(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue14040
File: python.info, Node: Command Line Switch Changes, Prev: Building C extensions, Up: Porting to Python 3 3
1.6.24.4 Command Line Switch Changes
....................................
* The -Q command-line flag and related artifacts have been removed.
Code checking sys.flags.division_warning will need updating.
(bpo-10998(1), contributed by Éric Araujo.)
* When ‘python’ is started with *note -S: b24, ‘import site’ will no
longer add site-specific paths to the module search paths. In
previous versions, it did.
(bpo-11591(2), contributed by Carl Meyer with editions by Éric
Araujo.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10998
(2) https://bugs.python.org/issue11591
File: python.info, Node: What’s New In Python 3 2, Next: What’s New In Python 3 1, Prev: What’s New In Python 3 3, Up: What’s New in Python
1.7 What’s New In Python 3.2
============================
Author: Raymond Hettinger
This article explains the new features in Python 3.2 as compared to 3.1.
It focuses on a few highlights and gives a few examples. For full
details, see the Misc/NEWS(1) file.
See also
........
PEP 392(2) - Python 3.2 Release Schedule
* Menu:
* PEP 384; Defining a Stable ABI: PEP 384 Defining a Stable ABI.
* PEP 389; Argparse Command Line Parsing Module: PEP 389 Argparse Command Line Parsing Module.
* PEP 391; Dictionary Based Configuration for Logging: PEP 391 Dictionary Based Configuration for Logging.
* PEP 3148; The concurrent.futures module: PEP 3148 The concurrent futures module.
* PEP 3147; PYC Repository Directories: PEP 3147 PYC Repository Directories.
* PEP 3149; ABI Version Tagged .so Files: PEP 3149 ABI Version Tagged so Files.
* PEP 3333; Python Web Server Gateway Interface v1.0.1: PEP 3333 Python Web Server Gateway Interface v1 0 1.
* Other Language Changes: Other Language Changes<7>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules.
* Multi-threading::
* Optimizations: Optimizations<6>.
* Unicode::
* Codecs::
* Documentation::
* IDLE::
* Code Repository::
* Build and C API Changes: Build and C API Changes<5>.
* Porting to Python 3.2: Porting to Python 3 2.
---------- Footnotes ----------
(1)
https://github.com/python/cpython/blob/076ca6c3c8df3030307e548d9be792ce3c1c6eea/Misc/NEWS
(2) https://www.python.org/dev/peps/pep-0392
File: python.info, Node: PEP 384 Defining a Stable ABI, Next: PEP 389 Argparse Command Line Parsing Module, Up: What’s New In Python 3 2
1.7.1 PEP 384: Defining a Stable ABI
------------------------------------
In the past, extension modules built for one Python version were often
not usable with other Python versions. Particularly on Windows, every
feature release of Python required rebuilding all extension modules that
one wanted to use. This requirement was the result of the free access
to Python interpreter internals that extension modules could use.
With Python 3.2, an alternative approach becomes available: extension
modules which restrict themselves to a limited API (by defining
Py_LIMITED_API) cannot use many of the internals, but are constrained to
a set of API functions that are promised to be stable for several
releases. As a consequence, extension modules built for 3.2 in that
mode will also work with 3.3, 3.4, and so on. Extension modules that
make use of details of memory structures can still be built, but will
need to be recompiled for every feature release.
See also
........
PEP 384(1) - Defining a Stable ABI
PEP written by Martin von Löwis.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0384
File: python.info, Node: PEP 389 Argparse Command Line Parsing Module, Next: PEP 391 Dictionary Based Configuration for Logging, Prev: PEP 384 Defining a Stable ABI, Up: What’s New In Python 3 2
1.7.2 PEP 389: Argparse Command Line Parsing Module
---------------------------------------------------
A new module for command line parsing, *note argparse: 6, was introduced
to overcome the limitations of *note optparse: c3. which did not provide
support for positional arguments (not just options), subcommands,
required options and other common patterns of specifying and validating
options.
This module has already had widespread success in the community as a
third-party module. Being more fully featured than its predecessor, the
*note argparse: 6. module is now the preferred module for command-line
processing. The older module is still being kept available because of
the substantial amount of legacy code that depends on it.
Here’s an annotated example parser showing features like limiting
results to a set of choices, specifying a `metavar' in the help screen,
validating that one or more positional arguments is present, and making
a required option:
import argparse
parser = argparse.ArgumentParser(
description = 'Manage servers', # main description for help
epilog = 'Tested on Solaris and Linux') # displayed after help
parser.add_argument('action', # argument name
choices = ['deploy', 'start', 'stop'], # three allowed values
help = 'action on each target') # help msg
parser.add_argument('targets',
metavar = 'HOSTNAME', # var name used in help msg
nargs = '+', # require one or more targets
help = 'url for target machines') # help msg explanation
parser.add_argument('-u', '--user', # -u or --user option
required = True, # make it a required argument
help = 'login as user')
Example of calling the parser on a command string:
>>> cmd = 'deploy sneezy.example.com sleepy.example.com -u skycaptain'
>>> result = parser.parse_args(cmd.split())
>>> result.action
'deploy'
>>> result.targets
['sneezy.example.com', 'sleepy.example.com']
>>> result.user
'skycaptain'
Example of the parser’s automatically generated help:
>>> parser.parse_args('-h'.split())
usage: manage_cloud.py [-h] -u USER
{deploy,start,stop} HOSTNAME [HOSTNAME ...]
Manage servers
positional arguments:
{deploy,start,stop} action on each target
HOSTNAME url for target machines
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER login as user
Tested on Solaris and Linux
An especially nice *note argparse: 6. feature is the ability to define
subparsers, each with their own argument patterns and help displays:
import argparse
parser = argparse.ArgumentParser(prog='HELM')
subparsers = parser.add_subparsers()
parser_l = subparsers.add_parser('launch', help='Launch Control') # first subgroup
parser_l.add_argument('-m', '--missiles', action='store_true')
parser_l.add_argument('-t', '--torpedos', action='store_true')
parser_m = subparsers.add_parser('move', help='Move Vessel', # second subgroup
aliases=('steer', 'turn')) # equivalent names
parser_m.add_argument('-c', '--course', type=int, required=True)
parser_m.add_argument('-s', '--speed', type=int, default=0)
$ ./helm.py --help # top level help (launch and move)
$ ./helm.py launch --help # help for launch options
$ ./helm.py launch --missiles # set missiles=True and torpedos=False
$ ./helm.py steer --course 180 --speed 5 # set movement parameters
See also
........
PEP 389(1) - New Command Line Parsing Module
PEP written by Steven Bethard.
*note Upgrading optparse code: b29. for details on the differences from
*note optparse: c3.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0389
File: python.info, Node: PEP 391 Dictionary Based Configuration for Logging, Next: PEP 3148 The concurrent futures module, Prev: PEP 389 Argparse Command Line Parsing Module, Up: What’s New In Python 3 2
1.7.3 PEP 391: Dictionary Based Configuration for Logging
---------------------------------------------------------
The *note logging: aa. module provided two kinds of configuration, one
style with function calls for each option or another style driven by an
external file saved in a ‘ConfigParser’ format. Those options did not
provide the flexibility to create configurations from JSON or YAML
files, nor did they support incremental configuration, which is needed
for specifying logger options from a command line.
To support a more flexible style, the module now offers *note
logging.config.dictConfig(): b2b. for specifying logging configuration
with plain Python dictionaries. The configuration options include
formatters, handlers, filters, and loggers. Here’s a working example of
a configuration dictionary:
{"version": 1,
"formatters": {"brief": {"format": "%(levelname)-8s: %(name)-15s: %(message)s"},
"full": {"format": "%(asctime)s %(name)-15s %(levelname)-8s %(message)s"}
},
"handlers": {"console": {
"class": "logging.StreamHandler",
"formatter": "brief",
"level": "INFO",
"stream": "ext://sys.stdout"},
"console_priority": {
"class": "logging.StreamHandler",
"formatter": "full",
"level": "ERROR",
"stream": "ext://sys.stderr"}
},
"root": {"level": "DEBUG", "handlers": ["console", "console_priority"]}}
If that dictionary is stored in a file called ‘conf.json’, it can be
loaded and called with code like this:
>>> import json, logging.config
>>> with open('conf.json') as f:
... conf = json.load(f)
...
>>> logging.config.dictConfig(conf)
>>> logging.info("Transaction completed normally")
INFO : root : Transaction completed normally
>>> logging.critical("Abnormal termination")
2011-02-17 11:14:36,694 root CRITICAL Abnormal termination
See also
........
PEP 391(1) - Dictionary Based Configuration for Logging
PEP written by Vinay Sajip.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0391
File: python.info, Node: PEP 3148 The concurrent futures module, Next: PEP 3147 PYC Repository Directories, Prev: PEP 391 Dictionary Based Configuration for Logging, Up: What’s New In Python 3 2
1.7.4 PEP 3148: The ‘concurrent.futures’ module
-----------------------------------------------
Code for creating and managing concurrency is being collected in a new
top-level namespace, `concurrent'. Its first member is a `futures'
package which provides a uniform high-level interface for managing
threads and processes.
The design for *note concurrent.futures: 22. was inspired by the
`java.util.concurrent' package. In that model, a running call and its
result are represented by a *note Future: b2d. object that abstracts
features common to threads, processes, and remote procedure calls. That
object supports status checks (running or done), timeouts,
cancellations, adding callbacks, and access to results or exceptions.
The primary offering of the new module is a pair of executor classes for
launching and managing calls. The goal of the executors is to make it
easier to use existing tools for making parallel calls. They save the
effort needed to setup a pool of resources, launch the calls, create a
results queue, add time-out handling, and limit the total number of
threads, processes, or remote procedure calls.
Ideally, each application should share a single executor across multiple
components so that process and thread limits can be centrally managed.
This solves the design challenge that arises when each component has its
own competing strategy for resource management.
Both classes share a common interface with three methods: *note
submit(): 2a3. for scheduling a callable and returning a *note Future:
b2d. object; *note map(): 6be. for scheduling many asynchronous calls at
a time, and *note shutdown(): b2e. for freeing resources. The class is
a *note context manager: 568. and can be used in a *note with: 6e9.
statement to assure that resources are automatically released when
currently pending futures are done executing.
A simple of example of *note ThreadPoolExecutor: 27a. is a launch of
four parallel threads for copying files:
import concurrent.futures, shutil
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as e:
e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
e.submit(shutil.copy, 'src3.txt', 'dest4.txt')
See also
........
PEP 3148(1) - Futures – Execute Computations Asynchronously
PEP written by Brian Quinlan.
*note Code for Threaded Parallel URL reads: b2f, an example using
threads to fetch multiple web pages in parallel.
*note Code for computing prime numbers in parallel: b30, an example
demonstrating *note ProcessPoolExecutor: 2a4.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3148
File: python.info, Node: PEP 3147 PYC Repository Directories, Next: PEP 3149 ABI Version Tagged so Files, Prev: PEP 3148 The concurrent futures module, Up: What’s New In Python 3 2
1.7.5 PEP 3147: PYC Repository Directories
------------------------------------------
Python’s scheme for caching bytecode in `.pyc' files did not work well
in environments with multiple Python interpreters. If one interpreter
encountered a cached file created by another interpreter, it would
recompile the source and overwrite the cached file, thus losing the
benefits of caching.
The issue of “pyc fights” has become more pronounced as it has become
commonplace for Linux distributions to ship with multiple versions of
Python. These conflicts also arise with CPython alternatives such as
Unladen Swallow.
To solve this problem, Python’s import machinery has been extended to
use distinct filenames for each interpreter. Instead of Python 3.2 and
Python 3.3 and Unladen Swallow each competing for a file called
“mymodule.pyc”, they will now look for “mymodule.cpython-32.pyc”,
“mymodule.cpython-33.pyc”, and “mymodule.unladen10.pyc”. And to prevent
all of these new files from cluttering source directories, the `pyc'
files are now collected in a “__pycache__” directory stored under the
package directory.
Aside from the filenames and target directories, the new scheme has a
few aspects that are visible to the programmer:
* Imported modules now have a *note __cached__: b32. attribute which
stores the name of the actual file that was imported:
>>> import collections
>>> collections.__cached__
'c:/py32/lib/__pycache__/collections.cpython-32.pyc'
* The tag that is unique to each interpreter is accessible from the
*note imp: 9a. module:
>>> import imp
>>> imp.get_tag()
'cpython-32'
* Scripts that try to deduce source filename from the imported file
now need to be smarter. It is no longer sufficient to simply strip
the “c” from a “.pyc” filename. Instead, use the new functions in
the *note imp: 9a. module:
>>> imp.source_from_cache('c:/py32/lib/__pycache__/collections.cpython-32.pyc')
'c:/py32/lib/collections.py'
>>> imp.cache_from_source('c:/py32/lib/collections.py')
'c:/py32/lib/__pycache__/collections.cpython-32.pyc'
* The *note py_compile: d7. and *note compileall: 21. modules have
been updated to reflect the new naming convention and target
directory. The command-line invocation of `compileall' has new
options: ‘-i’ for specifying a list of files and directories to
compile and ‘-b’ which causes bytecode files to be written to their
legacy location rather than `__pycache__'.
* The *note importlib.abc: 9c. module has been updated with new *note
abstract base classes: b33. for loading bytecode files. The
obsolete ABCs, ‘PyLoader’ and ‘PyPycLoader’, have been deprecated
(instructions on how to stay Python 3.1 compatible are included
with the documentation).
See also
........
PEP 3147(1) - PYC Repository Directories
PEP written by Barry Warsaw.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3147
File: python.info, Node: PEP 3149 ABI Version Tagged so Files, Next: PEP 3333 Python Web Server Gateway Interface v1 0 1, Prev: PEP 3147 PYC Repository Directories, Up: What’s New In Python 3 2
1.7.6 PEP 3149: ABI Version Tagged .so Files
--------------------------------------------
The PYC repository directory allows multiple bytecode cache files to be
co-located. This PEP implements a similar mechanism for shared object
files by giving them a common directory and distinct names for each
version.
The common directory is “pyshared” and the file names are made distinct
by identifying the Python implementation (such as CPython, PyPy, Jython,
etc.), the major and minor version numbers, and optional build flags
(such as “d” for debug, “m” for pymalloc, “u” for wide-unicode). For an
arbitrary package “foo”, you may see these files when the distribution
package is installed:
/usr/share/pyshared/foo.cpython-32m.so
/usr/share/pyshared/foo.cpython-33md.so
In Python itself, the tags are accessible from functions in the *note
sysconfig: fe. module:
>>> import sysconfig
>>> sysconfig.get_config_var('SOABI') # find the version tag
'cpython-32mu'
>>> sysconfig.get_config_var('EXT_SUFFIX') # find the full filename extension
'.cpython-32mu.so'
See also
........
PEP 3149(1) - ABI Version Tagged .so Files
PEP written by Barry Warsaw.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3149
File: python.info, Node: PEP 3333 Python Web Server Gateway Interface v1 0 1, Next: Other Language Changes<7>, Prev: PEP 3149 ABI Version Tagged so Files, Up: What’s New In Python 3 2
1.7.7 PEP 3333: Python Web Server Gateway Interface v1.0.1
----------------------------------------------------------
This informational PEP clarifies how bytes/text issues are to be handled
by the WSGI protocol. The challenge is that string handling in Python 3
is most conveniently handled with the *note str: 330. type even though
the HTTP protocol is itself bytes oriented.
The PEP differentiates so-called `native strings' that are used for
request/response headers and metadata versus `byte strings' which are
used for the bodies of requests and responses.
The `native strings' are always of type *note str: 330. but are
restricted to code points between `U+0000' through `U+00FF' which are
translatable to bytes using `Latin-1' encoding. These strings are used
for the keys and values in the environment dictionary and for response
headers and statuses in the ‘start_response()’ function. They must
follow RFC 2616(1) with respect to encoding. That is, they must either
be `ISO-8859-1' characters or use RFC 2047(2) MIME encoding.
For developers porting WSGI applications from Python 2, here are the
salient points:
* If the app already used strings for headers in Python 2, no change
is needed.
* If instead, the app encoded output headers or decoded input
headers, then the headers will need to be re-encoded to Latin-1.
For example, an output header encoded in utf-8 was using
‘h.encode('utf-8')’ now needs to convert from bytes to native
strings using ‘h.encode('utf-8').decode('latin-1')’.
* Values yielded by an application or sent using the ‘write()’ method
must be byte strings. The ‘start_response()’ function and environ
must use native strings. The two cannot be mixed.
For server implementers writing CGI-to-WSGI pathways or other CGI-style
protocols, the users must to be able access the environment using native
strings even though the underlying platform may have a different
convention. To bridge this gap, the *note wsgiref: 12c. module has a
new function, *note wsgiref.handlers.read_environ(): b36. for
transcoding CGI variables from *note os.environ: b37. into native
strings and returning a new dictionary.
See also
........
PEP 3333(3) - Python Web Server Gateway Interface v1.0.1
PEP written by Phillip Eby.
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc2616.html
(2) https://tools.ietf.org/html/rfc2047.html
(3) https://www.python.org/dev/peps/pep-3333
File: python.info, Node: Other Language Changes<7>, Next: New Improved and Deprecated Modules, Prev: PEP 3333 Python Web Server Gateway Interface v1 0 1, Up: What’s New In Python 3 2
1.7.8 Other Language Changes
----------------------------
Some smaller changes made to the core Python language are:
* String formatting for *note format(): 4db. and *note str.format():
4da. gained new capabilities for the format character `#'.
Previously, for integers in binary, octal, or hexadecimal, it
caused the output to be prefixed with ‘0b’, ‘0o’, or ‘0x’
respectively. Now it can also handle floats, complex, and Decimal,
causing the output to always have a decimal point even when no
digits follow it.
>>> format(20, '#o')
'0o24'
>>> format(12.34, '#5.0f')
' 12.'
(Suggested by Mark Dickinson and implemented by Eric Smith in
bpo-7094(1).)
* There is also a new *note str.format_map(): 6b1. method that
extends the capabilities of the existing *note str.format(): 4da.
method by accepting arbitrary *note mapping: b39. objects. This
new method makes it possible to use string formatting with any of
Python’s many dictionary-like objects such as *note defaultdict:
b3a, *note Shelf: 8b0, *note ConfigParser: 4aa, or *note dbm: 32.
It is also useful with custom *note dict: 1b8. subclasses that
normalize keys before look-up or that supply a *note __missing__():
b3b. method for unknown keys:
>>> import shelve
>>> d = shelve.open('tmp.shl')
>>> 'The {project_name} status is {status} as of {date}'.format_map(d)
'The testing project status is green as of February 15, 2011'
>>> class LowerCasedDict(dict):
... def __getitem__(self, key):
... return dict.__getitem__(self, key.lower())
>>> lcd = LowerCasedDict(part='widgets', quantity=10)
>>> 'There are {QUANTITY} {Part} in stock'.format_map(lcd)
'There are 10 widgets in stock'
>>> class PlaceholderDict(dict):
... def __missing__(self, key):
... return '<{}>'.format(key)
>>> 'Hello {name}, welcome to {location}'.format_map(PlaceholderDict())
'Hello , welcome to '
(Suggested by Raymond Hettinger and implemented by Eric Smith in
bpo-6081(2).)
* The interpreter can now be started with a quiet option, ‘-q’, to
prevent the copyright and version information from being displayed
in the interactive mode. The option can be introspected using the
*note sys.flags: b3c. attribute:
$ python -q
>>> sys.flags
sys.flags(debug=0, division_warning=0, inspect=0, interactive=0,
optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0,
ignore_environment=0, verbose=0, bytes_warning=0, quiet=1)
(Contributed by Marcin Wojdyr in bpo-1772833(3)).
* The *note hasattr(): 447. function works by calling *note
getattr(): 448. and detecting whether an exception is raised. This
technique allows it to detect methods created dynamically by *note
__getattr__(): 31a. or *note __getattribute__(): 449. which would
otherwise be absent from the class dictionary. Formerly, `hasattr'
would catch any exception, possibly masking genuine errors. Now,
`hasattr' has been tightened to only catch *note AttributeError:
39b. and let other exceptions pass through:
>>> class A:
... @property
... def f(self):
... return 1 // 0
...
>>> a = A()
>>> hasattr(a, 'f')
Traceback (most recent call last):
...
ZeroDivisionError: integer division or modulo by zero
(Discovered by Yury Selivanov and fixed by Benjamin Peterson;
bpo-9666(4).)
* The *note str(): 330. of a float or complex number is now the same
as its *note repr(): 7cc. Previously, the *note str(): 330. form
was shorter but that just caused confusion and is no longer needed
now that the shortest possible *note repr(): 7cc. is displayed by
default:
>>> import math
>>> repr(math.pi)
'3.141592653589793'
>>> str(math.pi)
'3.141592653589793'
(Proposed and implemented by Mark Dickinson; bpo-9337(5).)
* *note memoryview: 25c. objects now have a *note release(): b3d.
method and they also now support the context management protocol.
This allows timely release of any resources that were acquired when
requesting a buffer from the original object.
>>> with memoryview(b'abcdefgh') as v:
... print(v.tolist())
[97, 98, 99, 100, 101, 102, 103, 104]
(Added by Antoine Pitrou; bpo-9757(6).)
* Previously it was illegal to delete a name from the local namespace
if it occurs as a free variable in a nested block:
def outer(x):
def inner():
return x
inner()
del x
This is now allowed. Remember that the target of an *note except:
b3e. clause is cleared, so this code which used to work with Python
2.6, raised a *note SyntaxError: 458. with Python 3.1 and now works
again:
def f():
def print_error():
print(e)
try:
something
except Exception as e:
print_error()
# implicit "del e" here
(See bpo-4617(7).)
* The internal ‘structsequence’ tool now creates subclasses of tuple.
This means that C structures like those returned by *note
os.stat(): 1f1, *note time.gmtime(): b3f, and *note
sys.version_info: b1a. now work like a *note named tuple: aa3. and
now work with functions and methods that expect a tuple as an
argument. This is a big step forward in making the C structures as
flexible as their pure Python counterparts:
>>> import sys
>>> isinstance(sys.version_info, tuple)
True
>>> 'Version %d.%d.%d %s(%d)' % sys.version_info
'Version 3.2.0 final(0)'
(Suggested by Arfrever Frehtes Taifersar Arahesis and implemented
by Benjamin Peterson in bpo-8413(8).)
* Warnings are now easier to control using the *note PYTHONWARNINGS:
418. environment variable as an alternative to using ‘-W’ at the
command line:
$ export PYTHONWARNINGS='ignore::RuntimeWarning::,once::UnicodeWarning::'
(Suggested by Barry Warsaw and implemented by Philip Jenvey in
bpo-7301(9).)
* A new warning category, *note ResourceWarning: 4d0, has been added.
It is emitted when potential issues with resource consumption or
cleanup are detected. It is silenced by default in normal release
builds but can be enabled through the means provided by the *note
warnings: 126. module, or on the command line.
A *note ResourceWarning: 4d0. is issued at interpreter shutdown if
the *note gc.garbage: b40. list isn’t empty, and if *note
gc.DEBUG_UNCOLLECTABLE: b41. is set, all uncollectable objects are
printed. This is meant to make the programmer aware that their
code contains object finalization issues.
A *note ResourceWarning: 4d0. is also issued when a *note file
object: b42. is destroyed without having been explicitly closed.
While the deallocator for such object ensures it closes the
underlying operating system resource (usually, a file descriptor),
the delay in deallocating the object could produce various issues,
especially under Windows. Here is an example of enabling the
warning from the command line:
$ python -q -Wdefault
>>> f = open("foo", "wb")
>>> del f
__main__:1: ResourceWarning: unclosed file <_io.BufferedWriter name='foo'>
(Added by Antoine Pitrou and Georg Brandl in bpo-10093(10) and
bpo-477863(11).)
* *note range: 9be. objects now support `index' and `count' methods.
This is part of an effort to make more objects fully implement the
‘collections.Sequence’ *note abstract base class: b33. As a
result, the language will have a more uniform API. In addition,
*note range: 9be. objects now support slicing and negative indices,
even with values larger than *note sys.maxsize: b43. This makes
`range' more interoperable with lists:
>>> range(0, 100, 2).count(10)
1
>>> range(0, 100, 2).index(10)
5
>>> range(0, 100, 2)[5]
10
>>> range(0, 100, 2)[0:5]
range(0, 10, 2)
(Contributed by Daniel Stutzbach in bpo-9213(12), by Alexander
Belopolsky in bpo-2690(13), and by Nick Coghlan in bpo-10889(14).)
* The *note callable(): b44. builtin function from Py2.x was
resurrected. It provides a concise, readable alternative to using
an *note abstract base class: b33. in an expression like
‘isinstance(x, collections.Callable)’:
>>> callable(max)
True
>>> callable(20)
False
(See bpo-10518(15).)
* Python’s import mechanism can now load modules installed in
directories with non-ASCII characters in the path name. This
solved an aggravating problem with home directories for users with
non-ASCII characters in their usernames.
(Required extensive work by Victor Stinner in bpo-9425(16).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue7094
(2) https://bugs.python.org/issue6081
(3) https://bugs.python.org/issue1772833
(4) https://bugs.python.org/issue9666
(5) https://bugs.python.org/issue9337
(6) https://bugs.python.org/issue9757
(7) https://bugs.python.org/issue4617
(8) https://bugs.python.org/issue8413
(9) https://bugs.python.org/issue7301
(10) https://bugs.python.org/issue10093
(11) https://bugs.python.org/issue477863
(12) https://bugs.python.org/issue9213
(13) https://bugs.python.org/issue2690
(14) https://bugs.python.org/issue10889
(15) https://bugs.python.org/issue10518
(16) https://bugs.python.org/issue9425
File: python.info, Node: New Improved and Deprecated Modules, Next: Multi-threading, Prev: Other Language Changes<7>, Up: What’s New In Python 3 2
1.7.9 New, Improved, and Deprecated Modules
-------------------------------------------
Python’s standard library has undergone significant maintenance efforts
and quality improvements.
The biggest news for Python 3.2 is that the *note email: 69. package,
*note mailbox: ae. module, and *note nntplib: c0. modules now work
correctly with the bytes/text model in Python 3. For the first time,
there is correct handling of messages with mixed encodings.
Throughout the standard library, there has been more careful attention
to encodings and text versus bytes issues. In particular, interactions
with the operating system are now better able to exchange non-ASCII data
using the Windows MBCS encoding, locale-aware encodings, or UTF-8.
Another significant win is the addition of substantially better support
for `SSL' connections and security certificates.
In addition, more classes now implement a *note context manager: 568. to
support convenient and reliable resource clean-up using a *note with:
6e9. statement.
* Menu:
* email: email<5>.
* elementtree::
* functools: functools<6>.
* itertools: itertools<4>.
* collections: collections<8>.
* threading: threading<6>.
* datetime and time::
* math: math<6>.
* abc: abc<3>.
* io: io<5>.
* reprlib::
* logging: logging<7>.
* csv: csv<3>.
* contextlib: contextlib<6>.
* decimal and fractions::
* ftp::
* popen::
* select: select<3>.
* gzip and zipfile::
* tarfile: tarfile<5>.
* hashlib: hashlib<3>.
* ast: ast<3>.
* os: os<8>.
* shutil: shutil<5>.
* sqlite3: sqlite3<6>.
* html: html<3>.
* socket: socket<8>.
* ssl: ssl<9>.
* nntp::
* certificates::
* imaplib: imaplib<3>.
* http.client: http client<4>.
* unittest: unittest<6>.
* random: random<2>.
* poplib: poplib<3>.
* asyncore: asyncore<2>.
* tempfile: tempfile<2>.
* inspect: inspect<6>.
* pydoc: pydoc<5>.
* dis: dis<3>.
* dbm: dbm<6>.
* ctypes: ctypes<2>.
* site: site<2>.
* sysconfig: sysconfig<2>.
* pdb: pdb<5>.
* configparser: configparser<2>.
* urllib.parse: urllib parse<2>.
* mailbox::
* turtledemo::
File: python.info, Node: email<5>, Next: elementtree, Up: New Improved and Deprecated Modules
1.7.9.1 email
.............
The usability of the *note email: 69. package in Python 3 has been
mostly fixed by the extensive efforts of R. David Murray. The problem
was that emails are typically read and stored in the form of *note
bytes: 331. rather than *note str: 330. text, and they may contain
multiple encodings within a single email. So, the email package had to
be extended to parse and generate email messages in bytes format.
* New functions *note message_from_bytes(): b47. and *note
message_from_binary_file(): b48, and new classes *note
BytesFeedParser: b49. and *note BytesParser: b4a. allow binary
message data to be parsed into model objects.
* Given bytes input to the model, *note get_payload(): b4b. will by
default decode a message body that has a
‘Content-Transfer-Encoding’ of `8bit' using the charset specified
in the MIME headers and return the resulting string.
* Given bytes input to the model, *note Generator: b4c. will convert
message bodies that have a ‘Content-Transfer-Encoding’ of `8bit' to
instead have a `7bit' ‘Content-Transfer-Encoding’.
Headers with unencoded non-ASCII bytes are deemed to be RFC
2047(1)-encoded using the `unknown-8bit' character set.
* A new class *note BytesGenerator: b4d. produces bytes as output,
preserving any unchanged non-ASCII data that was present in the
input used to build the model, including message bodies with a
‘Content-Transfer-Encoding’ of `8bit'.
* The *note smtplib: ed. *note SMTP: a81. class now accepts a byte
string for the `msg' argument to the *note sendmail(): 744. method,
and a new method, *note send_message(): 745. accepts a *note
Message: 541. object and can optionally obtain the `from_addr' and
`to_addrs' addresses directly from the object.
(Proposed and implemented by R. David Murray, bpo-4661(2) and
bpo-10321(3).)
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc2047.html
(2) https://bugs.python.org/issue4661
(3) https://bugs.python.org/issue10321
File: python.info, Node: elementtree, Next: functools<6>, Prev: email<5>, Up: New Improved and Deprecated Modules
1.7.9.2 elementtree
...................
The *note xml.etree.ElementTree: 137. package and its
‘xml.etree.cElementTree’ counterpart have been updated to version 1.3.
Several new and useful functions and methods have been added:
* *note xml.etree.ElementTree.fromstringlist(): b4f. which builds an
XML document from a sequence of fragments
* *note xml.etree.ElementTree.register_namespace(): b50. for
registering a global namespace prefix
* *note xml.etree.ElementTree.tostringlist(): 916. for string
representation including all sublists
* *note xml.etree.ElementTree.Element.extend(): b51. for appending a
sequence of zero or more elements
* *note xml.etree.ElementTree.Element.iterfind(): b52. searches an
element and subelements
* *note xml.etree.ElementTree.Element.itertext(): b53. creates a text
iterator over an element and its subelements
* *note xml.etree.ElementTree.TreeBuilder.end(): b54. closes the
current element
* *note xml.etree.ElementTree.TreeBuilder.doctype(): 2c1. handles a
doctype declaration
Two methods have been deprecated:
* ‘xml.etree.ElementTree.getchildren()’ use ‘list(elem)’ instead.
* ‘xml.etree.ElementTree.getiterator()’ use ‘Element.iter’ instead.
For details of the update, see Introducing ElementTree(1) on Fredrik
Lundh’s website.
(Contributed by Florent Xicluna and Fredrik Lundh, bpo-6472(2).)
---------- Footnotes ----------
(1) http://effbot.org/zone/elementtree-13-intro.htm
(2) https://bugs.python.org/issue6472
File: python.info, Node: functools<6>, Next: itertools<4>, Prev: elementtree, Up: New Improved and Deprecated Modules
1.7.9.3 functools
.................
* The *note functools: 85. module includes a new decorator for
caching function calls. *note functools.lru_cache(): 1c7. can save
repeated queries to an external resource whenever the results are
expected to be the same.
For example, adding a caching decorator to a database query
function can save database accesses for popular searches:
>>> import functools
>>> @functools.lru_cache(maxsize=300)
... def get_phone_number(name):
... c = conn.cursor()
... c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
... return c.fetchone()[0]
>>> for name in user_requests:
... get_phone_number(name) # cached lookup
To help with choosing an effective cache size, the wrapped function
is instrumented for tracking cache statistics:
>>> get_phone_number.cache_info()
CacheInfo(hits=4805, misses=980, maxsize=300, currsize=300)
If the phonelist table gets updated, the outdated contents of the
cache can be cleared with:
>>> get_phone_number.cache_clear()
(Contributed by Raymond Hettinger and incorporating design ideas
from Jim Baker, Miki Tebeka, and Nick Coghlan; see recipe
498245(1), recipe 577479(2), bpo-10586(3), and bpo-10593(4).)
* The *note functools.wraps(): 86a. decorator now adds a
‘__wrapped__’ attribute pointing to the original callable function.
This allows wrapped functions to be introspected. It also copies
‘__annotations__’ if defined. And now it also gracefully skips
over missing attributes such as ‘__doc__’ which might not be
defined for the wrapped callable.
In the above example, the cache can be removed by recovering the
original function:
>>> get_phone_number = get_phone_number.__wrapped__ # uncached function
(By Nick Coghlan and Terrence Cole; bpo-9567(5), bpo-3445(6), and
bpo-8814(7).)
* To help write classes with rich comparison methods, a new decorator
*note functools.total_ordering(): 84b. will use existing equality
and inequality methods to fill in the remaining methods.
For example, supplying `__eq__' and `__lt__' will enable *note
total_ordering(): 84b. to fill-in `__le__', `__gt__' and `__ge__':
@total_ordering
class Student:
def __eq__(self, other):
return ((self.lastname.lower(), self.firstname.lower()) ==
(other.lastname.lower(), other.firstname.lower()))
def __lt__(self, other):
return ((self.lastname.lower(), self.firstname.lower()) <
(other.lastname.lower(), other.firstname.lower()))
With the `total_ordering' decorator, the remaining comparison
methods are filled in automatically.
(Contributed by Raymond Hettinger.)
* To aid in porting programs from Python 2, the *note
functools.cmp_to_key(): b56. function converts an old-style
comparison function to modern *note key function: 6e0.:
>>> # locale-aware sort order
>>> sorted(iterable, key=cmp_to_key(locale.strcoll))
For sorting examples and a brief sorting tutorial, see the Sorting
HowTo(8) tutorial.
(Contributed by Raymond Hettinger.)
---------- Footnotes ----------
(1) https://code.activestate.com/recipes/498245
(2) https://code.activestate.com/recipes/577479
(3) https://bugs.python.org/issue10586
(4) https://bugs.python.org/issue10593
(5) https://bugs.python.org/issue9567
(6) https://bugs.python.org/issue3445
(7) https://bugs.python.org/issue8814
(8) https://wiki.python.org/moin/HowTo/Sorting/
File: python.info, Node: itertools<4>, Next: collections<8>, Prev: functools<6>, Up: New Improved and Deprecated Modules
1.7.9.4 itertools
.................
* The *note itertools: a3. module has a new *note accumulate(): 1dd.
function modeled on APL’s `scan' operator and Numpy’s `accumulate'
function:
>>> from itertools import accumulate
>>> list(accumulate([8, 2, 50]))
[8, 10, 60]
>>> prob_dist = [0.1, 0.4, 0.2, 0.3]
>>> list(accumulate(prob_dist)) # cumulative probability distribution
[0.1, 0.5, 0.7, 1.0]
For an example using *note accumulate(): 1dd, see the *note
examples for the random module: b58.
(Contributed by Raymond Hettinger and incorporating design
suggestions from Mark Dickinson.)
File: python.info, Node: collections<8>, Next: threading<6>, Prev: itertools<4>, Up: New Improved and Deprecated Modules
1.7.9.5 collections
...................
* The *note collections.Counter: 9da. class now has two forms of
in-place subtraction, the existing `-=' operator for saturating
subtraction(1) and the new *note subtract(): b5a. method for
regular subtraction. The former is suitable for multisets(2) which
only have positive counts, and the latter is more suitable for use
cases that allow negative counts:
>>> from collections import Counter
>>> tally = Counter(dogs=5, cats=3)
>>> tally -= Counter(dogs=2, cats=8) # saturating subtraction
>>> tally
Counter({'dogs': 3})
>>> tally = Counter(dogs=5, cats=3)
>>> tally.subtract(dogs=2, cats=8) # regular subtraction
>>> tally
Counter({'dogs': 3, 'cats': -5})
(Contributed by Raymond Hettinger.)
* The *note collections.OrderedDict: 1b9. class has a new method
*note move_to_end(): b5b. which takes an existing key and moves it
to either the first or last position in the ordered sequence.
The default is to move an item to the last position. This is
equivalent of renewing an entry with ‘od[k] = od.pop(k)’.
A fast move-to-end operation is useful for resequencing entries.
For example, an ordered dictionary can be used to track order of
access by aging entries from the oldest to the most recently
accessed.
>>> from collections import OrderedDict
>>> d = OrderedDict.fromkeys(['a', 'b', 'X', 'd', 'e'])
>>> list(d)
['a', 'b', 'X', 'd', 'e']
>>> d.move_to_end('X')
>>> list(d)
['a', 'b', 'd', 'e', 'X']
(Contributed by Raymond Hettinger.)
* The *note collections.deque: 531. class grew two new methods *note
count(): b5c. and *note reverse(): b5d. that make them more
substitutable for *note list: 262. objects:
>>> from collections import deque
>>> d = deque('simsalabim')
>>> d.count('s')
2
>>> d.reverse()
>>> d
deque(['m', 'i', 'b', 'a', 'l', 'a', 's', 'm', 'i', 's'])
(Contributed by Raymond Hettinger.)
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/Saturation_arithmetic
(2) https://en.wikipedia.org/wiki/Multiset
File: python.info, Node: threading<6>, Next: datetime and time, Prev: collections<8>, Up: New Improved and Deprecated Modules
1.7.9.6 threading
.................
The *note threading: 109. module has a new *note Barrier: b5f.
synchronization class for making multiple threads wait until all of them
have reached a common barrier point. Barriers are useful for making
sure that a task with multiple preconditions does not run until all of
the predecessor tasks are complete.
Barriers can work with an arbitrary number of threads. This is a
generalization of a Rendezvous(1) which is defined for only two threads.
Implemented as a two-phase cyclic barrier, *note Barrier: b5f. objects
are suitable for use in loops. The separate `filling' and `draining'
phases assure that all threads get released (drained) before any one of
them can loop back and re-enter the barrier. The barrier fully resets
after each cycle.
Example of using barriers:
from threading import Barrier, Thread
def get_votes(site):
ballots = conduct_election(site)
all_polls_closed.wait() # do not count until all polls are closed
totals = summarize(ballots)
publish(site, totals)
all_polls_closed = Barrier(len(sites))
for site in sites:
Thread(target=get_votes, args=(site,)).start()
In this example, the barrier enforces a rule that votes cannot be
counted at any polling site until all polls are closed. Notice how a
solution with a barrier is similar to one with *note
threading.Thread.join(): b60, but the threads stay alive and continue to
do work (summarizing ballots) after the barrier point is crossed.
If any of the predecessor tasks can hang or be delayed, a barrier can be
created with an optional `timeout' parameter. Then if the timeout
period elapses before all the predecessor tasks reach the barrier point,
all waiting threads are released and a *note BrokenBarrierError: b61.
exception is raised:
def get_votes(site):
ballots = conduct_election(site)
try:
all_polls_closed.wait(timeout=midnight - time.now())
except BrokenBarrierError:
lockbox = seal_ballots(ballots)
queue.put(lockbox)
else:
totals = summarize(ballots)
publish(site, totals)
In this example, the barrier enforces a more robust rule. If some
election sites do not finish before midnight, the barrier times-out and
the ballots are sealed and deposited in a queue for later handling.
See Barrier Synchronization Patterns(2) for more examples of how
barriers can be used in parallel computing. Also, there is a simple but
thorough explanation of barriers in The Little Book of Semaphores(3),
`section 3.6'.
(Contributed by Kristján Valur Jónsson with an API review by Jeffrey
Yasskin in bpo-8777(4).)
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/Synchronous_rendezvous
(2)
http://osl.cs.illinois.edu/media/papers/karmani-2009-barrier_synchronization_pattern.pdf
(3) https://greenteapress.com/semaphores/LittleBookOfSemaphores.pdf
(4) https://bugs.python.org/issue8777
File: python.info, Node: datetime and time, Next: math<6>, Prev: threading<6>, Up: New Improved and Deprecated Modules
1.7.9.7 datetime and time
.........................
* The *note datetime: 31. module has a new type *note timezone: a01.
that implements the *note tzinfo: 380. interface by returning a
fixed UTC offset and timezone name. This makes it easier to create
timezone-aware datetime objects:
>>> from datetime import datetime, timezone
>>> datetime.now(timezone.utc)
datetime.datetime(2010, 12, 8, 21, 4, 2, 923754, tzinfo=datetime.timezone.utc)
>>> datetime.strptime("01/01/2000 12:00 +0000", "%m/%d/%Y %H:%M %z")
datetime.datetime(2000, 1, 1, 12, 0, tzinfo=datetime.timezone.utc)
* Also, *note timedelta: 195. objects can now be multiplied by *note
float: 187. and divided by *note float: 187. and *note int: 184.
objects. And *note timedelta: 195. objects can now divide one
another.
* The *note datetime.date.strftime(): 538. method is no longer
restricted to years after 1900. The new supported year range is
from 1000 to 9999 inclusive.
* Whenever a two-digit year is used in a time tuple, the
interpretation has been governed by ‘time.accept2dyear’. The
default is ‘True’ which means that for a two-digit year, the
century is guessed according to the POSIX rules governing the ‘%y’
strptime format.
Starting with Py3.2, use of the century guessing heuristic will
emit a *note DeprecationWarning: 278. Instead, it is recommended
that ‘time.accept2dyear’ be set to ‘False’ so that large date
ranges can be used without guesswork:
>>> import time, warnings
>>> warnings.resetwarnings() # remove the default warning filters
>>> time.accept2dyear = True # guess whether 11 means 11 or 2011
>>> time.asctime((11, 1, 1, 12, 34, 56, 4, 1, 0))
Warning (from warnings module):
...
DeprecationWarning: Century info guessed for a 2-digit year.
'Fri Jan 1 12:34:56 2011'
>>> time.accept2dyear = False # use the full range of allowable dates
>>> time.asctime((11, 1, 1, 12, 34, 56, 4, 1, 0))
'Fri Jan 1 12:34:56 11'
Several functions now have significantly expanded date ranges.
When ‘time.accept2dyear’ is false, the *note time.asctime(): b63.
function will accept any year that fits in a C int, while the *note
time.mktime(): b64. and *note time.strftime(): b65. functions will
accept the full range supported by the corresponding operating
system functions.
(Contributed by Alexander Belopolsky and Victor Stinner in
bpo-1289118(1), bpo-5094(2), bpo-6641(3), bpo-2706(4), bpo-1777412(5),
bpo-8013(6), and bpo-10827(7).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1289118
(2) https://bugs.python.org/issue5094
(3) https://bugs.python.org/issue6641
(4) https://bugs.python.org/issue2706
(5) https://bugs.python.org/issue1777412
(6) https://bugs.python.org/issue8013
(7) https://bugs.python.org/issue10827
File: python.info, Node: math<6>, Next: abc<3>, Prev: datetime and time, Up: New Improved and Deprecated Modules
1.7.9.8 math
............
The *note math: b1. module has been updated with six new functions
inspired by the C99 standard.
The *note isfinite(): b67. function provides a reliable and fast way to
detect special values. It returns ‘True’ for regular numbers and
‘False’ for `Nan' or `Infinity':
>>> from math import isfinite
>>> [isfinite(x) for x in (123, 4.56, float('Nan'), float('Inf'))]
[True, True, False, False]
The *note expm1(): b68. function computes ‘e**x-1’ for small values of
`x' without incurring the loss of precision that usually accompanies the
subtraction of nearly equal quantities:
>>> from math import expm1
>>> expm1(0.013671875) # more accurate way to compute e**x-1 for a small x
0.013765762467652909
The *note erf(): 452. function computes a probability integral or
Gaussian error function(1). The complementary error function, *note
erfc(): 453, is ‘1 - erf(x)’:
>>> from math import erf, erfc, sqrt
>>> erf(1.0/sqrt(2.0)) # portion of normal distribution within 1 standard deviation
0.682689492137086
>>> erfc(1.0/sqrt(2.0)) # portion of normal distribution outside 1 standard deviation
0.31731050786291404
>>> erf(1.0/sqrt(2.0)) + erfc(1.0/sqrt(2.0))
1.0
The *note gamma(): b69. function is a continuous extension of the
factorial function. See ‘https://en.wikipedia.org/wiki/Gamma_function’
for details. Because the function is related to factorials, it grows
large even for small values of `x', so there is also a *note lgamma():
b6a. function for computing the natural logarithm of the gamma function:
>>> from math import gamma, lgamma
>>> gamma(7.0) # six factorial
720.0
>>> lgamma(801.0) # log(800 factorial)
4551.950730698041
(Contributed by Mark Dickinson.)
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/Error_function
File: python.info, Node: abc<3>, Next: io<5>, Prev: math<6>, Up: New Improved and Deprecated Modules
1.7.9.9 abc
...........
The *note abc: 4. module now supports *note abstractclassmethod(): 9cf.
and *note abstractstaticmethod(): 9d0.
These tools make it possible to define an *note abstract base class:
b33. that requires a particular *note classmethod(): 1d8. or *note
staticmethod(): 1d9. to be implemented:
class Temperature(metaclass=abc.ABCMeta):
@abc.abstractclassmethod
def from_fahrenheit(cls, t):
...
@abc.abstractclassmethod
def from_celsius(cls, t):
...
(Patch submitted by Daniel Urban; bpo-5867(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5867
File: python.info, Node: io<5>, Next: reprlib, Prev: abc<3>, Up: New Improved and Deprecated Modules
1.7.9.10 io
...........
The *note io.BytesIO: 79b. has a new method, *note getbuffer(): b6d,
which provides functionality similar to *note memoryview(): 25c. It
creates an editable view of the data without making a copy. The
buffer’s random access and support for slice notation are well-suited to
in-place editing:
>>> REC_LEN, LOC_START, LOC_LEN = 34, 7, 11
>>> def change_location(buffer, record_number, location):
... start = record_number * REC_LEN + LOC_START
... buffer[start: start+LOC_LEN] = location
>>> import io
>>> byte_stream = io.BytesIO(
... b'G3805 storeroom Main chassis '
... b'X7899 shipping Reserve cog '
... b'L6988 receiving Primary sprocket'
... )
>>> buffer = byte_stream.getbuffer()
>>> change_location(buffer, 1, b'warehouse ')
>>> change_location(buffer, 0, b'showroom ')
>>> print(byte_stream.getvalue())
b'G3805 showroom Main chassis '
b'X7899 warehouse Reserve cog '
b'L6988 receiving Primary sprocket'
(Contributed by Antoine Pitrou in bpo-5506(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5506
File: python.info, Node: reprlib, Next: logging<7>, Prev: io<5>, Up: New Improved and Deprecated Modules
1.7.9.11 reprlib
................
When writing a *note __repr__(): 33e. method for a custom container, it
is easy to forget to handle the case where a member refers back to the
container itself. Python’s builtin objects such as *note list: 262. and
*note set: b6f. handle self-reference by displaying “…” in the recursive
part of the representation string.
To help write such *note __repr__(): 33e. methods, the *note reprlib:
df. module has a new decorator, *note recursive_repr(): b70, for
detecting recursive calls to *note __repr__(): 33e. and substituting a
placeholder string instead:
>>> class MyList(list):
... @recursive_repr()
... def __repr__(self):
... return '<' + '|'.join(map(repr, self)) + '>'
...
>>> m = MyList('abc')
>>> m.append(m)
>>> m.append('x')
>>> print(m)
<'a'|'b'|'c'|...|'x'>
(Contributed by Raymond Hettinger in bpo-9826(1) and bpo-9840(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9826
(2) https://bugs.python.org/issue9840
File: python.info, Node: logging<7>, Next: csv<3>, Prev: reprlib, Up: New Improved and Deprecated Modules
1.7.9.12 logging
................
In addition to dictionary-based configuration described above, the *note
logging: aa. package has many other improvements.
The logging documentation has been augmented by a *note basic tutorial:
b72, an *note advanced tutorial: b73, and a *note cookbook: b74. of
logging recipes. These documents are the fastest way to learn about
logging.
The *note logging.basicConfig(): 1e0. set-up function gained a `style'
argument to support three different types of string formatting. It
defaults to “%” for traditional %-formatting, can be set to “{” for the
new *note str.format(): 4da. style, or can be set to “$” for the
shell-style formatting provided by *note string.Template: 3eb. The
following three configurations are equivalent:
>>> from logging import basicConfig
>>> basicConfig(style='%', format="%(name)s -> %(levelname)s: %(message)s")
>>> basicConfig(style='{', format="{name} -> {levelname} {message}")
>>> basicConfig(style='$', format="$name -> $levelname: $message")
If no configuration is set-up before a logging event occurs, there is
now a default configuration using a *note StreamHandler: b75. directed
to *note sys.stderr: 30f. for events of ‘WARNING’ level or higher.
Formerly, an event occurring before a configuration was set-up would
either raise an exception or silently drop the event depending on the
value of ‘logging.raiseExceptions’. The new default handler is stored
in *note logging.lastResort: b76.
The use of filters has been simplified. Instead of creating a *note
Filter: b77. object, the predicate can be any Python callable that
returns ‘True’ or ‘False’.
There were a number of other improvements that add flexibility and
simplify configuration. See the module documentation for a full listing
of changes in Python 3.2.
File: python.info, Node: csv<3>, Next: contextlib<6>, Prev: logging<7>, Up: New Improved and Deprecated Modules
1.7.9.13 csv
............
The *note csv: 2a. module now supports a new dialect, *note
unix_dialect: b79, which applies quoting for all fields and a
traditional Unix style with ‘'\n'’ as the line terminator. The
registered dialect name is ‘unix’.
The *note csv.DictWriter: b7a. has a new method, *note writeheader():
b7b. for writing-out an initial row to document the field names:
>>> import csv, sys
>>> w = csv.DictWriter(sys.stdout, ['name', 'dept'], dialect='unix')
>>> w.writeheader()
"name","dept"
>>> w.writerows([
... {'name': 'tom', 'dept': 'accounting'},
... {'name': 'susan', 'dept': 'Salesl'}])
"tom","accounting"
"susan","sales"
(New dialect suggested by Jay Talbot in bpo-5975(1), and the new method
suggested by Ed Abraham in bpo-1537721(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5975
(2) https://bugs.python.org/issue1537721
File: python.info, Node: contextlib<6>, Next: decimal and fractions, Prev: csv<3>, Up: New Improved and Deprecated Modules
1.7.9.14 contextlib
...................
There is a new and slightly mind-blowing tool *note ContextDecorator:
b7d. that is helpful for creating a *note context manager: 568. that
does double duty as a function decorator.
As a convenience, this new functionality is used by *note
contextmanager(): b7e. so that no extra effort is needed to support both
roles.
The basic idea is that both context managers and function decorators can
be used for pre-action and post-action wrappers. Context managers wrap
a group of statements using a *note with: 6e9. statement, and function
decorators wrap a group of statements enclosed in a function. So,
occasionally there is a need to write a pre-action or post-action
wrapper that can be used in either role.
For example, it is sometimes useful to wrap functions or groups of
statements with a logger that can track the time of entry and time of
exit. Rather than writing both a function decorator and a context
manager for the task, the *note contextmanager(): b7e. provides both
capabilities in a single definition:
from contextlib import contextmanager
import logging
logging.basicConfig(level=logging.INFO)
@contextmanager
def track_entry_and_exit(name):
logging.info('Entering: %s', name)
yield
logging.info('Exiting: %s', name)
Formerly, this would have only been usable as a context manager:
with track_entry_and_exit('widget loader'):
print('Some time consuming activity goes here')
load_widget()
Now, it can be used as a decorator as well:
@track_entry_and_exit('widget loader')
def activity():
print('Some time consuming activity goes here')
load_widget()
Trying to fulfill two roles at once places some limitations on the
technique. Context managers normally have the flexibility to return an
argument usable by a *note with: 6e9. statement, but there is no
parallel for function decorators.
In the above example, there is not a clean way for the
`track_entry_and_exit' context manager to return a logging instance for
use in the body of enclosed statements.
(Contributed by Michael Foord in bpo-9110(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9110
File: python.info, Node: decimal and fractions, Next: ftp, Prev: contextlib<6>, Up: New Improved and Deprecated Modules
1.7.9.15 decimal and fractions
..............................
Mark Dickinson crafted an elegant and efficient scheme for assuring that
different numeric datatypes will have the same hash value whenever their
actual values are equal (bpo-8188(1)):
assert hash(Fraction(3, 2)) == hash(1.5) == \
hash(Decimal("1.5")) == hash(complex(1.5, 0))
Some of the hashing details are exposed through a new attribute, *note
sys.hash_info: 921, which describes the bit width of the hash value, the
prime modulus, the hash values for `infinity' and `nan', and the
multiplier used for the imaginary part of a number:
>>> sys.hash_info
sys.hash_info(width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003)
An early decision to limit the inter-operability of various numeric
types has been relaxed. It is still unsupported (and ill-advised) to
have implicit mixing in arithmetic expressions such as ‘Decimal('1.1') +
float('1.1')’ because the latter loses information in the process of
constructing the binary float. However, since existing floating point
value can be converted losslessly to either a decimal or rational
representation, it makes sense to add them to the constructor and to
support mixed-type comparisons.
* The *note decimal.Decimal: 188. constructor now accepts *note
float: 187. objects directly so there in no longer a need to use
the *note from_float(): b80. method (bpo-8257(2)).
* Mixed type comparisons are now fully supported so that *note
Decimal: 188. objects can be directly compared with *note float:
187. and *note fractions.Fraction: 185. (bpo-2531(3) and
bpo-8188(4)).
Similar changes were made to *note fractions.Fraction: 185. so that the
*note from_float(): b81. and *note from_decimal(): b82. methods are no
longer needed (bpo-8294(5)):
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> Decimal(1.1)
Decimal('1.100000000000000088817841970012523233890533447265625')
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)
Another useful change for the *note decimal: 36. module is that the
‘Context.clamp’ attribute is now public. This is useful in creating
contexts that correspond to the decimal interchange formats specified in
IEEE 754 (see bpo-8540(6)).
(Contributed by Mark Dickinson and Raymond Hettinger.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8188
(2) https://bugs.python.org/issue8257
(3) https://bugs.python.org/issue2531
(4) https://bugs.python.org/issue8188
(5) https://bugs.python.org/issue8294
(6) https://bugs.python.org/issue8540
File: python.info, Node: ftp, Next: popen, Prev: decimal and fractions, Up: New Improved and Deprecated Modules
1.7.9.16 ftp
............
The *note ftplib.FTP: 2f0. class now supports the context management
protocol to unconditionally consume *note socket.error: 995. exceptions
and to close the FTP connection when done:
>>> from ftplib import FTP
>>> with FTP("ftp1.at.proftpd.org") as ftp:
ftp.login()
ftp.dir()
'230 Anonymous login ok, restrictions apply.'
dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 .
dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 ..
dr-xr-xr-x 5 ftp ftp 4096 May 6 10:43 CentOS
dr-xr-xr-x 3 ftp ftp 18 Jul 10 2008 Fedora
Other file-like objects such as *note mmap.mmap: 1ec. and *note
fileinput.input(): 2ad. also grew auto-closing context managers:
with fileinput.input(files=('log1.txt', 'log2.txt')) as f:
for line in f:
process(line)
(Contributed by Tarek Ziadé and Giampaolo Rodolà in bpo-4972(1), and by
Georg Brandl in bpo-8046(2) and bpo-1286(3).)
The *note FTP_TLS: a03. class now accepts a `context' parameter, which
is a *note ssl.SSLContext: 3e4. object allowing bundling SSL
configuration options, certificates and private keys into a single
(potentially long-lived) structure.
(Contributed by Giampaolo Rodolà; bpo-8806(4).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4972
(2) https://bugs.python.org/issue8046
(3) https://bugs.python.org/issue1286
(4) https://bugs.python.org/issue8806
File: python.info, Node: popen, Next: select<3>, Prev: ftp, Up: New Improved and Deprecated Modules
1.7.9.17 popen
..............
The *note os.popen(): 2a9. and *note subprocess.Popen(): 2b9. functions
now support *note with: 6e9. statements for auto-closing of the file
descriptors.
(Contributed by Antoine Pitrou and Brian Curtin in bpo-7461(1) and
bpo-10554(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue7461
(2) https://bugs.python.org/issue10554
File: python.info, Node: select<3>, Next: gzip and zipfile, Prev: popen, Up: New Improved and Deprecated Modules
1.7.9.18 select
...............
The *note select: e5. module now exposes a new, constant attribute,
*note PIPE_BUF: b86, which gives the minimum number of bytes which are
guaranteed not to block when *note select.select(): 673. says a pipe is
ready for writing.
>>> import select
>>> select.PIPE_BUF
512
(Available on Unix systems. Patch by Sébastien Sablé in bpo-9862(1))
---------- Footnotes ----------
(1) https://bugs.python.org/issue9862
File: python.info, Node: gzip and zipfile, Next: tarfile<5>, Prev: select<3>, Up: New Improved and Deprecated Modules
1.7.9.19 gzip and zipfile
.........................
*note gzip.GzipFile: 6dd. now implements the *note io.BufferedIOBase:
588. *note abstract base class: b33. (except for ‘truncate()’). It also
has a *note peek(): b88. method and supports unseekable as well as
zero-padded file objects.
The *note gzip: 8c. module also gains the *note compress(): 1d1. and
*note decompress(): b89. functions for easier in-memory compression and
decompression. Keep in mind that text needs to be encoded as *note
bytes: 331. before compressing and decompressing:
>>> import gzip
>>> s = 'Three shall be the number thou shalt count, '
>>> s += 'and the number of the counting shall be three'
>>> b = s.encode() # convert to utf-8
>>> len(b)
89
>>> c = gzip.compress(b)
>>> len(c)
77
>>> gzip.decompress(c).decode()[:42] # decompress and convert to text
'Three shall be the number thou shalt count'
(Contributed by Anand B. Pillai in bpo-3488(1); and by Antoine Pitrou,
Nir Aides and Brian Curtin in bpo-9962(2), bpo-1675951(3), bpo-7471(4)
and bpo-2846(5).)
Also, the ‘zipfile.ZipExtFile’ class was reworked internally to
represent files stored inside an archive. The new implementation is
significantly faster and can be wrapped in an *note io.BufferedReader:
b8a. object for more speedups. It also solves an issue where
interleaved calls to `read' and `readline' gave the wrong results.
(Patch submitted by Nir Aides in bpo-7610(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue3488
(2) https://bugs.python.org/issue9962
(3) https://bugs.python.org/issue1675951
(4) https://bugs.python.org/issue7471
(5) https://bugs.python.org/issue2846
(6) https://bugs.python.org/issue7610
File: python.info, Node: tarfile<5>, Next: hashlib<3>, Prev: gzip and zipfile, Up: New Improved and Deprecated Modules
1.7.9.20 tarfile
................
The *note TarFile: b8c. class can now be used as a context manager. In
addition, its *note add(): 47c. method has a new option, `filter', that
controls which files are added to the archive and allows the file
metadata to be edited.
The new `filter' option replaces the older, less flexible `exclude'
parameter which is now deprecated. If specified, the optional `filter'
parameter needs to be a *note keyword argument: 5c4. The user-supplied
filter function accepts a *note TarInfo: b8d. object and returns an
updated *note TarInfo: b8d. object, or if it wants the file to be
excluded, the function can return ‘None’:
>>> import tarfile, glob
>>> def myfilter(tarinfo):
... if tarinfo.isfile(): # only save real files
... tarinfo.uname = 'monty' # redact the user name
... return tarinfo
>>> with tarfile.open(name='myarchive.tar.gz', mode='w:gz') as tf:
... for filename in glob.glob('*.txt'):
... tf.add(filename, filter=myfilter)
... tf.list()
-rw-r--r-- monty/501 902 2011-01-26 17:59:11 annotations.txt
-rw-r--r-- monty/501 123 2011-01-26 17:59:11 general_questions.txt
-rw-r--r-- monty/501 3514 2011-01-26 17:59:11 prion.txt
-rw-r--r-- monty/501 124 2011-01-26 17:59:11 py_todo.txt
-rw-r--r-- monty/501 1399 2011-01-26 17:59:11 semaphore_notes.txt
(Proposed by Tarek Ziadé and implemented by Lars Gustäbel in
bpo-6856(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6856
File: python.info, Node: hashlib<3>, Next: ast<3>, Prev: tarfile<5>, Up: New Improved and Deprecated Modules
1.7.9.21 hashlib
................
The *note hashlib: 8d. module has two new constant attributes listing
the hashing algorithms guaranteed to be present in all implementations
and those available on the current implementation:
>>> import hashlib
>>> hashlib.algorithms_guaranteed
{'sha1', 'sha224', 'sha384', 'sha256', 'sha512', 'md5'}
>>> hashlib.algorithms_available
{'md2', 'SHA256', 'SHA512', 'dsaWithSHA', 'mdc2', 'SHA224', 'MD4', 'sha256',
'sha512', 'ripemd160', 'SHA1', 'MDC2', 'SHA', 'SHA384', 'MD2',
'ecdsa-with-SHA1','md4', 'md5', 'sha1', 'DSA-SHA', 'sha224',
'dsaEncryption', 'DSA', 'RIPEMD160', 'sha', 'MD5', 'sha384'}
(Suggested by Carl Chenet in bpo-7418(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue7418
File: python.info, Node: ast<3>, Next: os<8>, Prev: hashlib<3>, Up: New Improved and Deprecated Modules
1.7.9.22 ast
............
The *note ast: 8. module has a wonderful a general-purpose tool for
safely evaluating expression strings using the Python literal syntax.
The *note ast.literal_eval(): 4a5. function serves as a secure
alternative to the builtin *note eval(): b90. function which is easily
abused. Python 3.2 adds *note bytes: 331. and *note set: b6f. literals
to the list of supported types: strings, bytes, numbers, tuples, lists,
dicts, sets, booleans, and ‘None’.
>>> from ast import literal_eval
>>> request = "{'req': 3, 'func': 'pow', 'args': (2, 0.5)}"
>>> literal_eval(request)
{'args': (2, 0.5), 'req': 3, 'func': 'pow'}
>>> request = "os.system('do something harmful')"
>>> literal_eval(request)
Traceback (most recent call last):
...
ValueError: malformed node or string: <_ast.Call object at 0x101739a10>
(Implemented by Benjamin Peterson and Georg Brandl.)
File: python.info, Node: os<8>, Next: shutil<5>, Prev: ast<3>, Up: New Improved and Deprecated Modules
1.7.9.23 os
...........
Different operating systems use various encodings for filenames and
environment variables. The *note os: c4. module provides two new
functions, *note fsencode(): 4ef. and *note fsdecode(): 4ee, for
encoding and decoding filenames:
>>> import os
>>> filename = 'Sehenswürdigkeiten'
>>> os.fsencode(filename)
b'Sehensw\xc3\xbcrdigkeiten'
Some operating systems allow direct access to encoded bytes in the
environment. If so, the *note os.supports_bytes_environ: b92. constant
will be true.
For direct access to encoded environment variables (if available), use
the new *note os.getenvb(): b93. function or use *note os.environb: b94.
which is a bytes version of *note os.environ: b37.
(Contributed by Victor Stinner.)
File: python.info, Node: shutil<5>, Next: sqlite3<6>, Prev: os<8>, Up: New Improved and Deprecated Modules
1.7.9.24 shutil
...............
The *note shutil.copytree(): 21a. function has two new options:
* `ignore_dangling_symlinks': when ‘symlinks=False’ so that the
function copies a file pointed to by a symlink, not the symlink
itself. This option will silence the error raised if the file
doesn’t exist.
* `copy_function': is a callable that will be used to copy files.
*note shutil.copy2(): 25a. is used by default.
(Contributed by Tarek Ziadé.)
In addition, the *note shutil: e9. module now supports *note archiving
operations: b96. for zipfiles, uncompressed tarfiles, gzipped tarfiles,
and bzipped tarfiles. And there are functions for registering
additional archiving file formats (such as xz compressed tarfiles or
custom formats).
The principal functions are *note make_archive(): 21b. and *note
unpack_archive(): b97. By default, both operate on the current
directory (which can be set by *note os.chdir(): a3f.) and on any
sub-directories. The archive filename needs to be specified with a full
pathname. The archiving step is non-destructive (the original files are
left unchanged).
>>> import shutil, pprint
>>> os.chdir('mydata') # change to the source directory
>>> f = shutil.make_archive('/var/backup/mydata',
... 'zip') # archive the current directory
>>> f # show the name of archive
'/var/backup/mydata.zip'
>>> os.chdir('tmp') # change to an unpacking
>>> shutil.unpack_archive('/var/backup/mydata.zip') # recover the data
>>> pprint.pprint(shutil.get_archive_formats()) # display known formats
[('bztar', "bzip2'ed tar-file"),
('gztar', "gzip'ed tar-file"),
('tar', 'uncompressed tar file'),
('zip', 'ZIP file')]
>>> shutil.register_archive_format( # register a new archive format
... name='xz',
... function=xz.compress, # callable archiving function
... extra_args=[('level', 8)], # arguments to the function
... description='xz compression'
... )
(Contributed by Tarek Ziadé.)
File: python.info, Node: sqlite3<6>, Next: html<3>, Prev: shutil<5>, Up: New Improved and Deprecated Modules
1.7.9.25 sqlite3
................
The *note sqlite3: f2. module was updated to pysqlite version 2.6.0. It
has two new capabilities.
* The ‘sqlite3.Connection.in_transit’ attribute is true if there is
an active transaction for uncommitted changes.
* The *note sqlite3.Connection.enable_load_extension(): b99. and
*note sqlite3.Connection.load_extension(): b9a. methods allows you
to load SQLite extensions from “.so” files. One well-known
extension is the fulltext-search extension distributed with SQLite.
(Contributed by R. David Murray and Shashwat Anand; bpo-8845(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8845
File: python.info, Node: html<3>, Next: socket<8>, Prev: sqlite3<6>, Up: New Improved and Deprecated Modules
1.7.9.26 html
.............
A new *note html: 90. module was introduced with only a single function,
*note escape(): 934, which is used for escaping reserved characters from
HTML markup:
>>> import html
>>> html.escape('x > 2 && x < 7')
'x > 2 && x < 7'
File: python.info, Node: socket<8>, Next: ssl<9>, Prev: html<3>, Up: New Improved and Deprecated Modules
1.7.9.27 socket
...............
The *note socket: ef. module has two new improvements.
* Socket objects now have a *note detach(): b9d. method which puts
the socket into closed state without actually closing the
underlying file descriptor. The latter can then be reused for
other purposes. (Added by Antoine Pitrou; bpo-8524(1).)
* *note socket.create_connection(): b9e. now supports the context
management protocol to unconditionally consume *note socket.error:
995. exceptions and to close the socket when done. (Contributed by
Giampaolo Rodolà; bpo-9794(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8524
(2) https://bugs.python.org/issue9794
File: python.info, Node: ssl<9>, Next: nntp, Prev: socket<8>, Up: New Improved and Deprecated Modules
1.7.9.28 ssl
............
The *note ssl: f3. module added a number of features to satisfy common
requirements for secure (encrypted, authenticated) internet connections:
* A new class, *note SSLContext: 3e4, serves as a container for
persistent SSL data, such as protocol settings, certificates,
private keys, and various other options. It includes a *note
wrap_socket(): 3e5. for creating an SSL socket from an SSL context.
* A new function, *note ssl.match_hostname(): 3dc, supports server
identity verification for higher-level protocols by implementing
the rules of HTTPS (from RFC 2818(1)) which are also suitable for
other protocols.
* The *note ssl.wrap_socket(): 46f. constructor function now takes a
`ciphers' argument. The `ciphers' string lists the allowed
encryption algorithms using the format described in the OpenSSL
documentation(2).
* When linked against recent versions of OpenSSL, the *note ssl: f3.
module now supports the Server Name Indication extension to the TLS
protocol, allowing multiple “virtual hosts” using different
certificates on a single IP port. This extension is only supported
in client mode, and is activated by passing the `server_hostname'
argument to *note ssl.SSLContext.wrap_socket(): 3e5.
* Various options have been added to the *note ssl: f3. module, such
as *note OP_NO_SSLv2: ba0. which disables the insecure and obsolete
SSLv2 protocol.
* The extension now loads all the OpenSSL ciphers and digest
algorithms. If some SSL certificates cannot be verified, they are
reported as an “unknown algorithm” error.
* The version of OpenSSL being used is now accessible using the
module attributes *note ssl.OPENSSL_VERSION: ba1. (a string), *note
ssl.OPENSSL_VERSION_INFO: ba2. (a 5-tuple), and *note
ssl.OPENSSL_VERSION_NUMBER: ba3. (an integer).
(Contributed by Antoine Pitrou in bpo-8850(3), bpo-1589(4), bpo-8322(5),
bpo-5639(6), bpo-4870(7), bpo-8484(8), and bpo-8321(9).)
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc2818.html
(2)
https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-LIST-FORMAT
(3) https://bugs.python.org/issue8850
(4) https://bugs.python.org/issue1589
(5) https://bugs.python.org/issue8322
(6) https://bugs.python.org/issue5639
(7) https://bugs.python.org/issue4870
(8) https://bugs.python.org/issue8484
(9) https://bugs.python.org/issue8321
File: python.info, Node: nntp, Next: certificates, Prev: ssl<9>, Up: New Improved and Deprecated Modules
1.7.9.29 nntp
.............
The *note nntplib: c0. module has a revamped implementation with better
bytes and text semantics as well as more practical APIs. These
improvements break compatibility with the nntplib version in Python 3.1,
which was partly dysfunctional in itself.
Support for secure connections through both implicit (using *note
nntplib.NNTP_SSL: ba5.) and explicit (using *note
nntplib.NNTP.starttls(): ba6.) TLS has also been added.
(Contributed by Antoine Pitrou in bpo-9360(1) and Andrew Vant in
bpo-1926(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9360
(2) https://bugs.python.org/issue1926
File: python.info, Node: certificates, Next: imaplib<3>, Prev: nntp, Up: New Improved and Deprecated Modules
1.7.9.30 certificates
.....................
*note http.client.HTTPSConnection: 393, *note
urllib.request.HTTPSHandler: ba8. and *note urllib.request.urlopen():
78c. now take optional arguments to allow for server certificate
checking against a set of Certificate Authorities, as recommended in
public uses of HTTPS.
(Added by Antoine Pitrou, bpo-9003(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9003
File: python.info, Node: imaplib<3>, Next: http client<4>, Prev: certificates, Up: New Improved and Deprecated Modules
1.7.9.31 imaplib
................
Support for explicit TLS on standard IMAP4 connections has been added
through the new *note imaplib.IMAP4.starttls: baa. method.
(Contributed by Lorenzo M. Catucci and Antoine Pitrou, bpo-4471(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4471
File: python.info, Node: http client<4>, Next: unittest<6>, Prev: imaplib<3>, Up: New Improved and Deprecated Modules
1.7.9.32 http.client
....................
There were a number of small API improvements in the *note http.client:
94. module. The old-style HTTP 0.9 simple responses are no longer
supported and the `strict' parameter is deprecated in all classes.
The *note HTTPConnection: 392. and *note HTTPSConnection: 393. classes
now have a `source_address' parameter for a (host, port) tuple
indicating where the HTTP connection is made from.
Support for certificate checking and HTTPS virtual hosts were added to
*note HTTPSConnection: 393.
The *note request(): 54f. method on connection objects allowed an
optional `body' argument so that a *note file object: b42. could be used
to supply the content of the request. Conveniently, the `body' argument
now also accepts an *note iterable: bac. object so long as it includes
an explicit ‘Content-Length’ header. This extended interface is much
more flexible than before.
To establish an HTTPS connection through a proxy server, there is a new
*note set_tunnel(): bad. method that sets the host and port for HTTP
Connect tunneling.
To match the behavior of *note http.server: 97, the HTTP client library
now also encodes headers with ISO-8859-1 (Latin-1) encoding. It was
already doing that for incoming headers, so now the behavior is
consistent for both incoming and outgoing traffic. (See work by Armin
Ronacher in bpo-10980(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10980
File: python.info, Node: unittest<6>, Next: random<2>, Prev: http client<4>, Up: New Improved and Deprecated Modules
1.7.9.33 unittest
.................
The unittest module has a number of improvements supporting test
discovery for packages, easier experimentation at the interactive
prompt, new testcase methods, improved diagnostic messages for test
failures, and better method names.
* The command-line call ‘python -m unittest’ can now accept file
paths instead of module names for running specific tests
(bpo-10620(1)). The new test discovery can find tests within
packages, locating any test importable from the top-level
directory. The top-level directory can be specified with the ‘-t’
option, a pattern for matching files with ‘-p’, and a directory to
start discovery with ‘-s’:
$ python -m unittest discover -s my_proj_dir -p _test.py
(Contributed by Michael Foord.)
* Experimentation at the interactive prompt is now easier because the
‘unittest.case.TestCase’ class can now be instantiated without
arguments:
>>> from unittest import TestCase
>>> TestCase().assertEqual(pow(2, 3), 8)
(Contributed by Michael Foord.)
* The *note unittest: 11b. module has two new methods, *note
assertWarns(): abc. and *note assertWarnsRegex(): abd. to verify
that a given warning type is triggered by the code under test:
with self.assertWarns(DeprecationWarning):
legacy_function('XYZ')
(Contributed by Antoine Pitrou, bpo-9754(2).)
Another new method, *note assertCountEqual(): baf. is used to
compare two iterables to determine if their element counts are
equal (whether the same elements are present with the same number
of occurrences regardless of order):
def test_anagram(self):
self.assertCountEqual('algorithm', 'logarithm')
(Contributed by Raymond Hettinger.)
* A principal feature of the unittest module is an effort to produce
meaningful diagnostics when a test fails. When possible, the
failure is recorded along with a diff of the output. This is
especially helpful for analyzing log files of failed test runs.
However, since diffs can sometime be voluminous, there is a new
*note maxDiff: bb0. attribute that sets maximum length of diffs
displayed.
* In addition, the method names in the module have undergone a number
of clean-ups.
For example, *note assertRegex(): bb1. is the new name for
‘assertRegexpMatches()’ which was misnamed because the test uses
*note re.search(): bb2, not *note re.match(): bb3. Other methods
using regular expressions are now named using short form “Regex” in
preference to “Regexp” – this matches the names used in other
unittest implementations, matches Python’s old name for the *note
re: dd. module, and it has unambiguous camel-casing.
(Contributed by Raymond Hettinger and implemented by Ezio Melotti.)
* To improve consistency, some long-standing method aliases are being
deprecated in favor of the preferred names:
Old Name Preferred Name
-----------------------------------------------------------------------
‘assert_()’ *note assertTrue(): bb4.
‘assertEquals()’ *note assertEqual(): bb5.
‘assertNotEquals()’ *note assertNotEqual(): bb6.
‘assertAlmostEquals()’ *note assertAlmostEqual(): bb7.
‘assertNotAlmostEquals()’ *note assertNotAlmostEqual(): bb8.
Likewise, the ‘TestCase.fail*’ methods deprecated in Python 3.1 are
expected to be removed in Python 3.3. Also see the *note
Deprecated aliases: bb9. section in the *note unittest: 11b.
documentation.
(Contributed by Ezio Melotti; bpo-9424(3).)
* The ‘assertDictContainsSubset()’ method was deprecated because it
was misimplemented with the arguments in the wrong order. This
created hard-to-debug optical illusions where tests like
‘TestCase().assertDictContainsSubset({'a':1, 'b':2}, {'a':1})’
would fail.
(Contributed by Raymond Hettinger.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10620
(2) https://bugs.python.org/issue9754
(3) https://bugs.python.org/issue9424
File: python.info, Node: random<2>, Next: poplib<3>, Prev: unittest<6>, Up: New Improved and Deprecated Modules
1.7.9.34 random
...............
The integer methods in the *note random: dc. module now do a better job
of producing uniform distributions. Previously, they computed
selections with ‘int(n*random())’ which had a slight bias whenever `n'
was not a power of two. Now, multiple selections are made from a range
up to the next power of two and a selection is kept only when it falls
within the range ‘0 <= x < n’. The functions and methods affected are
*note randrange(): bbb, *note randint(): bbc, *note choice(): bbd, *note
shuffle(): bbe. and *note sample(): bbf.
(Contributed by Raymond Hettinger; bpo-9025(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9025
File: python.info, Node: poplib<3>, Next: asyncore<2>, Prev: random<2>, Up: New Improved and Deprecated Modules
1.7.9.35 poplib
...............
*note POP3_SSL: bc1. class now accepts a `context' parameter, which is a
*note ssl.SSLContext: 3e4. object allowing bundling SSL configuration
options, certificates and private keys into a single (potentially
long-lived) structure.
(Contributed by Giampaolo Rodolà; bpo-8807(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8807
File: python.info, Node: asyncore<2>, Next: tempfile<2>, Prev: poplib<3>, Up: New Improved and Deprecated Modules
1.7.9.36 asyncore
.................
*note asyncore.dispatcher: bc3. now provides a *note handle_accepted():
bc4. method returning a ‘(sock, addr)’ pair which is called when a
connection has actually been established with a new remote endpoint.
This is supposed to be used as a replacement for old *note
handle_accept(): bc5. and avoids the user to call *note accept(): bc6.
directly.
(Contributed by Giampaolo Rodolà; bpo-6706(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6706
File: python.info, Node: tempfile<2>, Next: inspect<6>, Prev: asyncore<2>, Up: New Improved and Deprecated Modules
1.7.9.37 tempfile
.................
The *note tempfile: 103. module has a new context manager, *note
TemporaryDirectory: bc8. which provides easy deterministic cleanup of
temporary directories:
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary dir:', tmpdirname)
(Contributed by Neil Schemenauer and Nick Coghlan; bpo-5178(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5178
File: python.info, Node: inspect<6>, Next: pydoc<5>, Prev: tempfile<2>, Up: New Improved and Deprecated Modules
1.7.9.38 inspect
................
* The *note inspect: a0. module has a new function *note
getgeneratorstate(): bca. to easily identify the current state of a
generator-iterator:
>>> from inspect import getgeneratorstate
>>> def gen():
... yield 'demo'
>>> g = gen()
>>> getgeneratorstate(g)
'GEN_CREATED'
>>> next(g)
'demo'
>>> getgeneratorstate(g)
'GEN_SUSPENDED'
>>> next(g, None)
>>> getgeneratorstate(g)
'GEN_CLOSED'
(Contributed by Rodolpho Eckhardt and Nick Coghlan, bpo-10220(1).)
* To support lookups without the possibility of activating a dynamic
attribute, the *note inspect: a0. module has a new function, *note
getattr_static(): bcb. Unlike *note hasattr(): 447, this is a true
read-only search, guaranteed not to change state while it is
searching:
>>> class A:
... @property
... def f(self):
... print('Running')
... return 10
...
>>> a = A()
>>> getattr(a, 'f')
Running
10
>>> inspect.getattr_static(a, 'f')
(Contributed by Michael Foord.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10220
File: python.info, Node: pydoc<5>, Next: dis<3>, Prev: inspect<6>, Up: New Improved and Deprecated Modules
1.7.9.39 pydoc
..............
The *note pydoc: d9. module now provides a much-improved Web server
interface, as well as a new command-line option ‘-b’ to automatically
open a browser window to display that server:
$ pydoc3.2 -b
(Contributed by Ron Adam; bpo-2001(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue2001
File: python.info, Node: dis<3>, Next: dbm<6>, Prev: pydoc<5>, Up: New Improved and Deprecated Modules
1.7.9.40 dis
............
The *note dis: 38. module gained two new functions for inspecting code,
*note code_info(): bce. and *note show_code(): 832. Both provide
detailed code object information for the supplied function, method,
source code string or code object. The former returns a string and the
latter prints it:
>>> import dis, random
>>> dis.show_code(random.choice)
Name: choice
Filename: /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/random.py
Argument count: 2
Kw-only arguments: 0
Number of locals: 3
Stack size: 11
Flags: OPTIMIZED, NEWLOCALS, NOFREE
Constants:
0: 'Choose a random element from a non-empty sequence.'
1: 'Cannot choose from an empty sequence'
Names:
0: _randbelow
1: len
2: ValueError
3: IndexError
Variable names:
0: self
1: seq
2: i
In addition, the *note dis(): 384. function now accepts string arguments
so that the common idiom ‘dis(compile(s, '', 'eval'))’ can be shortened
to ‘dis(s)’:
>>> dis('3*x+1 if x%2==1 else x//2')
1 0 LOAD_NAME 0 (x)
3 LOAD_CONST 0 (2)
6 BINARY_MODULO
7 LOAD_CONST 1 (1)
10 COMPARE_OP 2 (==)
13 POP_JUMP_IF_FALSE 28
16 LOAD_CONST 2 (3)
19 LOAD_NAME 0 (x)
22 BINARY_MULTIPLY
23 LOAD_CONST 1 (1)
26 BINARY_ADD
27 RETURN_VALUE
>> 28 LOAD_NAME 0 (x)
31 LOAD_CONST 0 (2)
34 BINARY_FLOOR_DIVIDE
35 RETURN_VALUE
Taken together, these improvements make it easier to explore how CPython
is implemented and to see for yourself what the language syntax does
under-the-hood.
(Contributed by Nick Coghlan in bpo-9147(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9147
File: python.info, Node: dbm<6>, Next: ctypes<2>, Prev: dis<3>, Up: New Improved and Deprecated Modules
1.7.9.41 dbm
............
All database modules now support the ‘get()’ and ‘setdefault()’ methods.
(Suggested by Ray Allen in bpo-9523(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9523
File: python.info, Node: ctypes<2>, Next: site<2>, Prev: dbm<6>, Up: New Improved and Deprecated Modules
1.7.9.42 ctypes
...............
A new type, *note ctypes.c_ssize_t: bd1. represents the C ‘ssize_t’
datatype.
File: python.info, Node: site<2>, Next: sysconfig<2>, Prev: ctypes<2>, Up: New Improved and Deprecated Modules
1.7.9.43 site
.............
The *note site: eb. module has three new functions useful for reporting
on the details of a given Python installation.
* *note getsitepackages(): bd3. lists all global site-packages
directories.
* *note getuserbase(): bd4. reports on the user’s base directory
where data can be stored.
* *note getusersitepackages(): bd5. reveals the user-specific
site-packages directory path.
>>> import site
>>> site.getsitepackages()
['/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.2/lib/site-python',
'/Library/Python/3.2/site-packages']
>>> site.getuserbase()
'/Users/raymondhettinger/Library/Python/3.2'
>>> site.getusersitepackages()
'/Users/raymondhettinger/Library/Python/3.2/lib/python/site-packages'
Conveniently, some of site’s functionality is accessible directly from
the command-line:
$ python -m site --user-base
/Users/raymondhettinger/.local
$ python -m site --user-site
/Users/raymondhettinger/.local/lib/python3.2/site-packages
(Contributed by Tarek Ziadé in bpo-6693(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6693
File: python.info, Node: sysconfig<2>, Next: pdb<5>, Prev: site<2>, Up: New Improved and Deprecated Modules
1.7.9.44 sysconfig
..................
The new *note sysconfig: fe. module makes it straightforward to discover
installation paths and configuration variables that vary across
platforms and installations.
The module offers access simple access functions for platform and
version information:
* *note get_platform(): bd7. returning values like `linux-i586' or
`macosx-10.6-ppc'.
* *note get_python_version(): bd8. returns a Python version string
such as “3.2”.
It also provides access to the paths and variables corresponding to one
of seven named schemes used by *note distutils: 39. Those include
`posix_prefix', `posix_home', `posix_user', `nt', `nt_user', `os2',
`os2_home':
* *note get_paths(): bd9. makes a dictionary containing installation
paths for the current installation scheme.
* *note get_config_vars(): 965. returns a dictionary of platform
specific variables.
There is also a convenient command-line interface:
C:\Python32>python -m sysconfig
Platform: "win32"
Python version: "3.2"
Current installation scheme: "nt"
Paths:
data = "C:\Python32"
include = "C:\Python32\Include"
platinclude = "C:\Python32\Include"
platlib = "C:\Python32\Lib\site-packages"
platstdlib = "C:\Python32\Lib"
purelib = "C:\Python32\Lib\site-packages"
scripts = "C:\Python32\Scripts"
stdlib = "C:\Python32\Lib"
Variables:
BINDIR = "C:\Python32"
BINLIBDEST = "C:\Python32\Lib"
EXE = ".exe"
INCLUDEPY = "C:\Python32\Include"
LIBDEST = "C:\Python32\Lib"
SO = ".pyd"
VERSION = "32"
abiflags = ""
base = "C:\Python32"
exec_prefix = "C:\Python32"
platbase = "C:\Python32"
prefix = "C:\Python32"
projectbase = "C:\Python32"
py_version = "3.2"
py_version_nodot = "32"
py_version_short = "3.2"
srcdir = "C:\Python32"
userbase = "C:\Documents and Settings\Raymond\Application Data\Python"
(Moved out of Distutils by Tarek Ziadé.)
File: python.info, Node: pdb<5>, Next: configparser<2>, Prev: sysconfig<2>, Up: New Improved and Deprecated Modules
1.7.9.45 pdb
............
The *note pdb: c9. debugger module gained a number of usability
improvements:
* ‘pdb.py’ now has a ‘-c’ option that executes commands as given in a
‘.pdbrc’ script file.
* A ‘.pdbrc’ script file can contain ‘continue’ and ‘next’ commands
that continue debugging.
* The ‘Pdb’ class constructor now accepts a `nosigint' argument.
* New commands: ‘l(list)’, ‘ll(long list)’ and ‘source’ for listing
source code.
* New commands: ‘display’ and ‘undisplay’ for showing or hiding the
value of an expression if it has changed.
* New command: ‘interact’ for starting an interactive interpreter
containing the global and local names found in the current scope.
* Breakpoints can be cleared by breakpoint number.
(Contributed by Georg Brandl, Antonio Cuni and Ilya Sandler.)
File: python.info, Node: configparser<2>, Next: urllib parse<2>, Prev: pdb<5>, Up: New Improved and Deprecated Modules
1.7.9.46 configparser
.....................
The *note configparser: 23. module was modified to improve usability and
predictability of the default parser and its supported INI syntax. The
old ‘ConfigParser’ class was removed in favor of ‘SafeConfigParser’
which has in turn been renamed to *note ConfigParser: 4aa. Support for
inline comments is now turned off by default and section or option
duplicates are not allowed in a single configuration source.
Config parsers gained a new API based on the mapping protocol:
>>> parser = ConfigParser()
>>> parser.read_string("""
... [DEFAULT]
... location = upper left
... visible = yes
... editable = no
... color = blue
...
... [main]
... title = Main Menu
... color = green
...
... [options]
... title = Options
... """)
>>> parser['main']['color']
'green'
>>> parser['main']['editable']
'no'
>>> section = parser['options']
>>> section['title']
'Options'
>>> section['title'] = 'Options (editable: %(editable)s)'
>>> section['title']
'Options (editable: no)'
The new API is implemented on top of the classical API, so custom parser
subclasses should be able to use it without modifications.
The INI file structure accepted by config parsers can now be customized.
Users can specify alternative option/value delimiters and comment
prefixes, change the name of the `DEFAULT' section or switch the
interpolation syntax.
There is support for pluggable interpolation including an additional
interpolation handler *note ExtendedInterpolation: bdc.:
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
>>> parser.read_dict({'buildout': {'directory': '/home/ambv/zope9'},
... 'custom': {'prefix': '/usr/local'}})
>>> parser.read_string("""
... [buildout]
... parts =
... zope9
... instance
... find-links =
... ${buildout:directory}/downloads/dist
...
... [zope9]
... recipe = plone.recipe.zope9install
... location = /opt/zope
...
... [instance]
... recipe = plone.recipe.zope9instance
... zope9-location = ${zope9:location}
... zope-conf = ${custom:prefix}/etc/zope.conf
... """)
>>> parser['buildout']['find-links']
'\n/home/ambv/zope9/downloads/dist'
>>> parser['instance']['zope-conf']
'/usr/local/etc/zope.conf'
>>> instance = parser['instance']
>>> instance['zope-conf']
'/usr/local/etc/zope.conf'
>>> instance['zope9-location']
'/opt/zope'
A number of smaller features were also introduced, like support for
specifying encoding in read operations, specifying fallback values for
get-functions, or reading directly from dictionaries and strings.
(All changes contributed by Łukasz Langa.)
File: python.info, Node: urllib parse<2>, Next: mailbox, Prev: configparser<2>, Up: New Improved and Deprecated Modules
1.7.9.47 urllib.parse
.....................
A number of usability improvements were made for the *note urllib.parse:
11f. module.
The *note urlparse(): 5fa. function now supports IPv6(1) addresses as
described in RFC 2732(2):
>>> import urllib.parse
>>> urllib.parse.urlparse('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/')
ParseResult(scheme='http',
netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',
path='/foo/',
params='',
query='',
fragment='')
The *note urldefrag(): bde. function now returns a *note named tuple:
aa3.:
>>> r = urllib.parse.urldefrag('http://python.org/about/#target')
>>> r
DefragResult(url='http://python.org/about/', fragment='target')
>>> r[0]
'http://python.org/about/'
>>> r.fragment
'target'
And, the *note urlencode(): 78b. function is now much more flexible,
accepting either a string or bytes type for the `query' argument. If it
is a string, then the `safe', `encoding', and `error' parameters are
sent to *note quote_plus(): bdf. for encoding:
>>> urllib.parse.urlencode([
... ('type', 'telenovela'),
... ('name', '¿Dónde Está Elisa?')],
... encoding='latin-1')
'type=telenovela&name=%BFD%F3nde+Est%E1+Elisa%3F'
As detailed in *note Parsing ASCII Encoded Bytes: be0, all the *note
urllib.parse: 11f. functions now accept ASCII-encoded byte strings as
input, so long as they are not mixed with regular strings. If
ASCII-encoded byte strings are given as parameters, the return types
will also be an ASCII-encoded byte strings:
>>> urllib.parse.urlparse(b'http://www.python.org:80/about/')
ParseResultBytes(scheme=b'http', netloc=b'www.python.org:80',
path=b'/about/', params=b'', query=b'', fragment=b'')
(Work by Nick Coghlan, Dan Mahn, and Senthil Kumaran in bpo-2987(3),
bpo-5468(4), and bpo-9873(5).)
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/IPv6
(2) https://tools.ietf.org/html/rfc2732.html
(3) https://bugs.python.org/issue2987
(4) https://bugs.python.org/issue5468
(5) https://bugs.python.org/issue9873
File: python.info, Node: mailbox, Next: turtledemo, Prev: urllib parse<2>, Up: New Improved and Deprecated Modules
1.7.9.48 mailbox
................
Thanks to a concerted effort by R. David Murray, the *note mailbox: ae.
module has been fixed for Python 3.2. The challenge was that mailbox
had been originally designed with a text interface, but email messages
are best represented with *note bytes: 331. because various parts of a
message may have different encodings.
The solution harnessed the *note email: 69. package’s binary support for
parsing arbitrary email messages. In addition, the solution required a
number of API changes.
As expected, the *note add(): be2. method for *note mailbox.Mailbox:
be3. objects now accepts binary input.
*note StringIO: 82d. and text file input are deprecated. Also, string
input will fail early if non-ASCII characters are used. Previously it
would fail when the email was processed in a later step.
There is also support for binary output. The *note get_file(): be4.
method now returns a file in the binary mode (where it used to
incorrectly set the file to text-mode). There is also a new *note
get_bytes(): be5. method that returns a *note bytes: 331. representation
of a message corresponding to a given `key'.
It is still possible to get non-binary output using the old API’s *note
get_string(): be6. method, but that approach is not very useful.
Instead, it is best to extract messages from a *note Message: be7.
object or to load them from binary input.
(Contributed by R. David Murray, with efforts from Steffen Daode
Nurpmeso and an initial patch by Victor Stinner in bpo-9124(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue9124
File: python.info, Node: turtledemo, Prev: mailbox, Up: New Improved and Deprecated Modules
1.7.9.49 turtledemo
...................
The demonstration code for the *note turtle: 116. module was moved from
the `Demo' directory to main library. It includes over a dozen sample
scripts with lively displays. Being on *note sys.path: 488, it can now
be run directly from the command-line:
$ python -m turtledemo
(Moved from the Demo directory by Alexander Belopolsky in bpo-10199(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue10199
File: python.info, Node: Multi-threading, Next: Optimizations<6>, Prev: New Improved and Deprecated Modules, Up: What’s New In Python 3 2
1.7.10 Multi-threading
----------------------
* The mechanism for serializing execution of concurrently running
Python threads (generally known as the *note GIL: bea. or Global
Interpreter Lock) has been rewritten. Among the objectives were
more predictable switching intervals and reduced overhead due to
lock contention and the number of ensuing system calls. The notion
of a “check interval” to allow thread switches has been abandoned
and replaced by an absolute duration expressed in seconds. This
parameter is tunable through *note sys.setswitchinterval(): beb.
It currently defaults to 5 milliseconds.
Additional details about the implementation can be read from a
python-dev mailing-list message(1) (however, “priority requests” as
exposed in this message have not been kept for inclusion).
(Contributed by Antoine Pitrou.)
* Regular and recursive locks now accept an optional `timeout'
argument to their *note acquire(): 76c. method. (Contributed by
Antoine Pitrou; bpo-7316(2).)
* Similarly, *note threading.Semaphore.acquire(): bec. also gained a
`timeout' argument. (Contributed by Torsten Landschoff;
bpo-850728(3).)
* Regular and recursive lock acquisitions can now be interrupted by
signals on platforms using Pthreads. This means that Python
programs that deadlock while acquiring locks can be successfully
killed by repeatedly sending SIGINT to the process (by pressing
‘Ctrl+C’ in most shells). (Contributed by Reid Kleckner;
bpo-8844(4).)
---------- Footnotes ----------
(1)
https://mail.python.org/pipermail/python-dev/2009-October/093321.html
(2) https://bugs.python.org/issue7316
(3) https://bugs.python.org/issue850728
(4) https://bugs.python.org/issue8844
File: python.info, Node: Optimizations<6>, Next: Unicode, Prev: Multi-threading, Up: What’s New In Python 3 2
1.7.11 Optimizations
--------------------
A number of small performance enhancements have been added:
* Python’s peephole optimizer now recognizes patterns such ‘x in {1,
2, 3}’ as being a test for membership in a set of constants. The
optimizer recasts the *note set: b6f. as a *note frozenset: bee.
and stores the pre-built constant.
Now that the speed penalty is gone, it is practical to start
writing membership tests using set-notation. This style is both
semantically clear and operationally fast:
extension = name.rpartition('.')[2]
if extension in {'xml', 'html', 'xhtml', 'css'}:
handle(name)
(Patch and additional tests contributed by Dave Malcolm;
bpo-6690(1)).
* Serializing and unserializing data using the *note pickle: ca.
module is now several times faster.
(Contributed by Alexandre Vassalotti, Antoine Pitrou and the
Unladen Swallow team in bpo-9410(2) and bpo-3873(3).)
* The Timsort algorithm(4) used in *note list.sort(): 445. and *note
sorted(): 444. now runs faster and uses less memory when called
with a *note key function: 6e0. Previously, every element of a
list was wrapped with a temporary object that remembered the key
value associated with each element. Now, two arrays of keys and
values are sorted in parallel. This saves the memory consumed by
the sort wrappers, and it saves time lost to delegating
comparisons.
(Patch by Daniel Stutzbach in bpo-9915(5).)
* JSON decoding performance is improved and memory consumption is
reduced whenever the same string is repeated for multiple keys.
Also, JSON encoding now uses the C speedups when the ‘sort_keys’
argument is true.
(Contributed by Antoine Pitrou in bpo-7451(6) and by Raymond
Hettinger and Antoine Pitrou in bpo-10314(7).)
* Recursive locks (created with the *note threading.RLock(): bef.
API) now benefit from a C implementation which makes them as fast
as regular locks, and between 10x and 15x faster than their
previous pure Python implementation.
(Contributed by Antoine Pitrou; bpo-3001(8).)
* The fast-search algorithm in stringlib is now used by the
‘split()’, ‘rsplit()’, ‘splitlines()’ and ‘replace()’ methods on
*note bytes: 331, *note bytearray: 332. and *note str: 330.
objects. Likewise, the algorithm is also used by ‘rfind()’,
‘rindex()’, ‘rsplit()’ and ‘rpartition()’.
(Patch by Florent Xicluna in bpo-7622(9) and bpo-7462(10).)
* Integer to string conversions now work two “digits” at a time,
reducing the number of division and modulo operations.
(bpo-6713(11) by Gawain Bolton, Mark Dickinson, and Victor
Stinner.)
There were several other minor optimizations. Set differencing now runs
faster when one operand is much larger than the other (patch by Andress
Bennetts in bpo-8685(12)). The ‘array.repeat()’ method has a faster
implementation (bpo-1569291(13) by Alexander Belopolsky). The
‘BaseHTTPRequestHandler’ has more efficient buffering (bpo-3709(14) by
Andrew Schaaf). The *note operator.attrgetter(): 71b. function has been
sped-up (bpo-10160(15) by Christos Georgiou). And ‘ConfigParser’ loads
multi-line arguments a bit faster (bpo-7113(16) by Łukasz Langa).
---------- Footnotes ----------
(1) https://bugs.python.org/issue6690
(2) https://bugs.python.org/issue9410
(3) https://bugs.python.org/issue3873
(4) https://en.wikipedia.org/wiki/Timsort
(5) https://bugs.python.org/issue9915
(6) https://bugs.python.org/issue7451
(7) https://bugs.python.org/issue10314
(8) https://bugs.python.org/issue3001
(9) https://bugs.python.org/issue7622
(10) https://bugs.python.org/issue7462
(11) https://bugs.python.org/issue6713
(12) https://bugs.python.org/issue8685
(13) https://bugs.python.org/issue1569291
(14) https://bugs.python.org/issue3709
(15) https://bugs.python.org/issue10160
(16) https://bugs.python.org/issue7113
File: python.info, Node: Unicode, Next: Codecs, Prev: Optimizations<6>, Up: What’s New In Python 3 2
1.7.12 Unicode
--------------
Python has been updated to Unicode 6.0.0(1). The update to the standard
adds over 2,000 new characters including emoji(2) symbols which are
important for mobile phones.
In addition, the updated standard has altered the character properties
for two Kannada characters (U+0CF1, U+0CF2) and one New Tai Lue numeric
character (U+19DA), making the former eligible for use in identifiers
while disqualifying the latter. For more information, see Unicode
Character Database Changes(3).
---------- Footnotes ----------
(1) http://unicode.org/versions/Unicode6.0.0/
(2) https://en.wikipedia.org/wiki/Emoji
(3) http://www.unicode.org/versions/Unicode6.0.0/#Database_Changes
File: python.info, Node: Codecs, Next: Documentation, Prev: Unicode, Up: What’s New In Python 3 2
1.7.13 Codecs
-------------
Support was added for `cp720' Arabic DOS encoding (bpo-1616979(1)).
MBCS encoding no longer ignores the error handler argument. In the
default strict mode, it raises an *note UnicodeDecodeError: 1fd. when it
encounters an undecodable byte sequence and an *note UnicodeEncodeError:
1fc. for an unencodable character.
The MBCS codec supports ‘'strict'’ and ‘'ignore'’ error handlers for
decoding, and ‘'strict'’ and ‘'replace'’ for encoding.
To emulate Python3.1 MBCS encoding, select the ‘'ignore'’ handler for
decoding and the ‘'replace'’ handler for encoding.
On Mac OS X, Python decodes command line arguments with ‘'utf-8'’ rather
than the locale encoding.
By default, *note tarfile: 101. uses ‘'utf-8'’ encoding on Windows
(instead of ‘'mbcs'’) and the ‘'surrogateescape'’ error handler on all
operating systems.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1616979
File: python.info, Node: Documentation, Next: IDLE, Prev: Codecs, Up: What’s New In Python 3 2
1.7.14 Documentation
--------------------
The documentation continues to be improved.
* A table of quick links has been added to the top of lengthy
sections such as *note Built-in Functions: bf3. In the case of
*note itertools: a3, the links are accompanied by tables of
cheatsheet-style summaries to provide an overview and memory jog
without having to read all of the docs.
* In some cases, the pure Python source code can be a helpful adjunct
to the documentation, so now many modules now feature quick links
to the latest version of the source code. For example, the *note
functools: 85. module documentation has a quick link at the top
labeled:
`Source code' Lib/functools.py(1).
(Contributed by Raymond Hettinger; see rationale(2).)
* The docs now contain more examples and recipes. In particular,
*note re: dd. module has an extensive section, *note Regular
Expression Examples: bf4. Likewise, the *note itertools: a3.
module continues to be updated with new *note Itertools Recipes:
bf5.
* The *note datetime: 31. module now has an auxiliary implementation
in pure Python. No functionality was changed. This just provides
an easier-to-read alternate implementation.
(Contributed by Alexander Belopolsky in bpo-9528(3).)
* The unmaintained ‘Demo’ directory has been removed. Some demos
were integrated into the documentation, some were moved to the
‘Tools/demo’ directory, and others were removed altogether.
(Contributed by Georg Brandl in bpo-7962(4).)
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/functools.py
(2)
https://rhettinger.wordpress.com/2011/01/28/open-your-source-more/
(3) https://bugs.python.org/issue9528
(4) https://bugs.python.org/issue7962
File: python.info, Node: IDLE, Next: Code Repository, Prev: Documentation, Up: What’s New In Python 3 2
1.7.15 IDLE
-----------
* The format menu now has an option to clean source files by
stripping trailing whitespace.
(Contributed by Raymond Hettinger; bpo-5150(1).)
* IDLE on Mac OS X now works with both Carbon AquaTk and Cocoa
AquaTk.
(Contributed by Kevin Walzer, Ned Deily, and Ronald Oussoren;
bpo-6075(2).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5150
(2) https://bugs.python.org/issue6075
File: python.info, Node: Code Repository, Next: Build and C API Changes<5>, Prev: IDLE, Up: What’s New In Python 3 2
1.7.16 Code Repository
----------------------
In addition to the existing Subversion code repository at
‘http://svn.python.org’ there is now a Mercurial(1) repository at
‘https://hg.python.org/’.
After the 3.2 release, there are plans to switch to Mercurial as the
primary repository. This distributed version control system should make
it easier for members of the community to create and share external
changesets. See PEP 385(2) for details.
To learn to use the new version control system, see the Quick Start(3)
or the Guide to Mercurial Workflows(4).
---------- Footnotes ----------
(1) https://www.mercurial-scm.org/
(2) https://www.python.org/dev/peps/pep-0385
(3) https://www.mercurial-scm.org/wiki/QuickStart
(4) https://www.mercurial-scm.org/guide
File: python.info, Node: Build and C API Changes<5>, Next: Porting to Python 3 2, Prev: Code Repository, Up: What’s New In Python 3 2
1.7.17 Build and C API Changes
------------------------------
Changes to Python’s build process and to the C API include:
* The `idle', `pydoc' and `2to3' scripts are now installed with a
version-specific suffix on ‘make altinstall’ (bpo-10679(1)).
* The C functions that access the Unicode Database now accept and
return characters from the full Unicode range, even on narrow
unicode builds (Py_UNICODE_TOLOWER, Py_UNICODE_ISDECIMAL, and
others). A visible difference in Python is that *note
unicodedata.numeric(): bf9. now returns the correct value for large
code points, and *note repr(): 7cc. may consider more characters as
printable.
(Reported by Bupjoe Lee and fixed by Amaury Forgeot D’Arc;
bpo-5127(2).)
* Computed gotos are now enabled by default on supported compilers
(which are detected by the configure script). They can still be
disabled selectively by specifying ‘--without-computed-gotos’.
(Contributed by Antoine Pitrou; bpo-9203(3).)
* The option ‘--with-wctype-functions’ was removed. The built-in
unicode database is now used for all functions.
(Contributed by Amaury Forgeot D’Arc; bpo-9210(4).)
* Hash values are now values of a new type, ‘Py_hash_t’, which is
defined to be the same size as a pointer. Previously they were of
type long, which on some 64-bit operating systems is still only 32
bits long. As a result of this fix, *note set: b6f. and *note
dict: 1b8. can now hold more than ‘2**32’ entries on builds with
64-bit pointers (previously, they could grow to that size but their
performance degraded catastrophically).
(Suggested by Raymond Hettinger and implemented by Benjamin
Peterson; bpo-9778(5).)
* A new macro ‘Py_VA_COPY’ copies the state of the variable argument
list. It is equivalent to C99 `va_copy' but available on all
Python platforms (bpo-2443(6)).
* A new C API function *note PySys_SetArgvEx(): bfa. allows an
embedded interpreter to set *note sys.argv: bfb. without also
modifying *note sys.path: 488. (bpo-5753(7)).
* ‘PyEval_CallObject’ is now only available in macro form. The
function declaration, which was kept for backwards compatibility
reasons, is now removed – the macro was introduced in 1997
(bpo-8276(8)).
* There is a new function *note PyLong_AsLongLongAndOverflow(): bfc.
which is analogous to *note PyLong_AsLongAndOverflow(): bfd. They
both serve to convert Python *note int: 184. into a native
fixed-width type while providing detection of cases where the
conversion won’t fit (bpo-7767(9)).
* The *note PyUnicode_CompareWithASCIIString(): bfe. function now
returns `not equal' if the Python string is `NUL' terminated.
* There is a new function *note PyErr_NewExceptionWithDoc(): bff.
that is like *note PyErr_NewException(): c00. but allows a
docstring to be specified. This lets C exceptions have the same
self-documenting capabilities as their pure Python counterparts
(bpo-7033(10)).
* When compiled with the ‘--with-valgrind’ option, the pymalloc
allocator will be automatically disabled when running under
Valgrind. This gives improved memory leak detection when running
under Valgrind, while taking advantage of pymalloc at other times
(bpo-2422(11)).
* Removed the ‘O?’ format from the `PyArg_Parse' functions. The
format is no longer used and it had never been documented
(bpo-8837(12)).
There were a number of other small changes to the C-API. See the
Misc/NEWS(13) file for a complete list.
Also, there were a number of updates to the Mac OS X build, see
Mac/BuildScript/README.txt(14) for details. For users running a
32/64-bit build, there is a known problem with the default Tcl/Tk on Mac
OS X 10.6. Accordingly, we recommend installing an updated alternative
such as ActiveState Tcl/Tk 8.5.9(15). See
‘https://www.python.org/download/mac/tcltk/’ for additional details.
---------- Footnotes ----------
(1) https://bugs.python.org/issue10679
(2) https://bugs.python.org/issue5127
(3) https://bugs.python.org/issue9203
(4) https://bugs.python.org/issue9210
(5) https://bugs.python.org/issue9778
(6) https://bugs.python.org/issue2443
(7) https://bugs.python.org/issue5753
(8) https://bugs.python.org/issue8276
(9) https://bugs.python.org/issue7767
(10) https://bugs.python.org/issue7033
(11) https://bugs.python.org/issue2422
(12) https://bugs.python.org/issue8837
(13) https://github.com/python/cpython/tree/3.8/Misc/NEWS
(14)
https://github.com/python/cpython/tree/3.8/Mac/BuildScript/README.txt
(15) https://www.activestate.com/activetcl/downloads
File: python.info, Node: Porting to Python 3 2, Prev: Build and C API Changes<5>, Up: What’s New In Python 3 2
1.7.18 Porting to Python 3.2
----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code:
* The *note configparser: 23. module has a number of clean-ups. The
major change is to replace the old ‘ConfigParser’ class with
long-standing preferred alternative ‘SafeConfigParser’. In
addition there are a number of smaller incompatibilities:
* The interpolation syntax is now validated on *note get(): c02.
and *note set(): c03. operations. In the default
interpolation scheme, only two tokens with percent signs are
valid: ‘%(name)s’ and ‘%%’, the latter being an escaped
percent sign.
* The *note set(): c03. and *note add_section(): c04. methods
now verify that values are actual strings. Formerly,
unsupported types could be introduced unintentionally.
* Duplicate sections or options from a single source now raise
either *note DuplicateSectionError: c05. or *note
DuplicateOptionError: c06. Formerly, duplicates would
silently overwrite a previous entry.
* Inline comments are now disabled by default so now the `;'
character can be safely used in values.
* Comments now can be indented. Consequently, for `;' or `#' to
appear at the start of a line in multiline values, it has to
be interpolated. This keeps comment prefix characters in
values from being mistaken as comments.
* ‘""’ is now a valid value and is no longer automatically
converted to an empty string. For empty strings, use ‘"option
="’ in a line.
* The *note nntplib: c0. module was reworked extensively, meaning
that its APIs are often incompatible with the 3.1 APIs.
* *note bytearray: 332. objects can no longer be used as filenames;
instead, they should be converted to *note bytes: 331.
* The ‘array.tostring()’ and ‘array.fromstring()’ have been renamed
to ‘array.tobytes()’ and ‘array.frombytes()’ for clarity. The old
names have been deprecated. (See bpo-8990(1).)
* ‘PyArg_Parse*()’ functions:
* “t#” format has been removed: use “s#” or “s*” instead
* “w” and “w#” formats has been removed: use “w*” instead
* The ‘PyCObject’ type, deprecated in 3.1, has been removed. To wrap
opaque C pointers in Python objects, the *note PyCapsule: c07. API
should be used instead; the new type has a well-defined interface
for passing typing safety information and a less complicated
signature for calling a destructor.
* The ‘sys.setfilesystemencoding()’ function was removed because it
had a flawed design.
* The *note random.seed(): c08. function and method now salt string
seeds with an sha512 hash function. To access the previous version
of `seed' in order to reproduce Python 3.1 sequences, set the
`version' argument to `1', ‘random.seed(s, version=1)’.
* The previously deprecated ‘string.maketrans()’ function has been
removed in favor of the static methods *note bytes.maketrans():
c09. and *note bytearray.maketrans(): c0a. This change solves the
confusion around which types were supported by the *note string:
f6. module. Now, *note str: 330, *note bytes: 331, and *note
bytearray: 332. each have their own `maketrans' and `translate'
methods with intermediate translation tables of the appropriate
type.
(Contributed by Georg Brandl; bpo-5675(2).)
* The previously deprecated ‘contextlib.nested()’ function has been
removed in favor of a plain *note with: 6e9. statement which can
accept multiple context managers. The latter technique is faster
(because it is built-in), and it does a better job finalizing
multiple context managers when one of them raises an exception:
with open('mylog.txt') as infile, open('a.out', 'w') as outfile:
for line in infile:
if '' in line:
outfile.write(line)
(Contributed by Georg Brandl and Mattias Brändström; appspot issue
53094(3).)
* *note struct.pack(): c0b. now only allows bytes for the ‘s’ string
pack code. Formerly, it would accept text arguments and implicitly
encode them to bytes using UTF-8. This was problematic because it
made assumptions about the correct encoding and because a
variable-length encoding can fail when writing to fixed length
segment of a structure.
Code such as ‘struct.pack('<6sHHBBB', 'GIF87a', x, y)’ should be
rewritten with to use bytes instead of text,
‘struct.pack('<6sHHBBB', b'GIF87a', x, y)’.
(Discovered by David Beazley and fixed by Victor Stinner;
bpo-10783(4).)
* The *note xml.etree.ElementTree: 137. class now raises an *note
xml.etree.ElementTree.ParseError: c0c. when a parse fails.
Previously it raised an *note xml.parsers.expat.ExpatError: c0d.
* The new, longer *note str(): 330. value on floats may break
doctests which rely on the old output format.
* In *note subprocess.Popen: 2b9, the default value for `close_fds'
is now ‘True’ under Unix; under Windows, it is ‘True’ if the three
standard streams are set to ‘None’, ‘False’ otherwise. Previously,
`close_fds' was always ‘False’ by default, which produced difficult
to solve bugs or race conditions when open file descriptors would
leak into the child process.
* Support for legacy HTTP 0.9 has been removed from *note
urllib.request: 120. and *note http.client: 94. Such support is
still present on the server side (in *note http.server: 97.).
(Contributed by Antoine Pitrou, bpo-10711(5).)
* SSL sockets in timeout mode now raise *note socket.timeout: c0e.
when a timeout occurs, rather than a generic *note SSLError: c0f.
(Contributed by Antoine Pitrou, bpo-10272(6).)
* The misleading functions *note PyEval_AcquireLock(): 2b2. and *note
PyEval_ReleaseLock(): c10. have been officially deprecated. The
thread-state aware APIs (such as *note PyEval_SaveThread(): c11.
and *note PyEval_RestoreThread(): 2b4.) should be used instead.
* Due to security risks, ‘asyncore.handle_accept()’ has been
deprecated, and a new function, ‘asyncore.handle_accepted()’, was
added to replace it.
(Contributed by Giampaolo Rodola in bpo-6706(7).)
* Due to the new *note GIL: bea. implementation, *note
PyEval_InitThreads(): c12. cannot be called before *note
Py_Initialize(): 439. anymore.
---------- Footnotes ----------
(1) https://bugs.python.org/issue8990
(2) https://bugs.python.org/issue5675
(3) https://codereview.appspot.com/53094
(4) https://bugs.python.org/issue10783
(5) https://bugs.python.org/issue10711
(6) https://bugs.python.org/issue10272
(7) https://bugs.python.org/issue6706
File: python.info, Node: What’s New In Python 3 1, Next: What’s New In Python 3 0, Prev: What’s New In Python 3 2, Up: What’s New in Python
1.8 What’s New In Python 3.1
============================
Author: Raymond Hettinger
This article explains the new features in Python 3.1, compared to 3.0.
* Menu:
* PEP 372; Ordered Dictionaries: PEP 372 Ordered Dictionaries.
* PEP 378; Format Specifier for Thousands Separator: PEP 378 Format Specifier for Thousands Separator.
* Other Language Changes: Other Language Changes<8>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules<2>.
* Optimizations: Optimizations<7>.
* IDLE: IDLE<2>.
* Build and C API Changes: Build and C API Changes<6>.
* Porting to Python 3.1: Porting to Python 3 1.
File: python.info, Node: PEP 372 Ordered Dictionaries, Next: PEP 378 Format Specifier for Thousands Separator, Up: What’s New In Python 3 1
1.8.1 PEP 372: Ordered Dictionaries
-----------------------------------
Regular Python dictionaries iterate over key/value pairs in arbitrary
order. Over the years, a number of authors have written alternative
implementations that remember the order that the keys were originally
inserted. Based on the experiences from those implementations, a new
*note collections.OrderedDict: 1b9. class has been introduced.
The OrderedDict API is substantially the same as regular dictionaries
but will iterate over keys and values in a guaranteed order depending on
when a key was first inserted. If a new entry overwrites an existing
entry, the original insertion position is left unchanged. Deleting an
entry and reinserting it will move it to the end.
The standard library now supports use of ordered dictionaries in several
modules. The *note configparser: 23. module uses them by default. This
lets configuration files be read, modified, and then written back in
their original order. The `_asdict()' method for *note
collections.namedtuple(): 1b7. now returns an ordered dictionary with
the values appearing in the same order as the underlying tuple indices.
The *note json: a4. module is being built-out with an
`object_pairs_hook' to allow OrderedDicts to be built by the decoder.
Support was also added for third-party tools like PyYAML(1).
See also
........
PEP 372(2) - Ordered Dictionaries
PEP written by Armin Ronacher and Raymond Hettinger.
Implementation written by Raymond Hettinger.
---------- Footnotes ----------
(1) http://pyyaml.org/
(2) https://www.python.org/dev/peps/pep-0372
File: python.info, Node: PEP 378 Format Specifier for Thousands Separator, Next: Other Language Changes<8>, Prev: PEP 372 Ordered Dictionaries, Up: What’s New In Python 3 1
1.8.2 PEP 378: Format Specifier for Thousands Separator
-------------------------------------------------------
The built-in *note format(): 4db. function and the *note str.format():
4da. method use a mini-language that now includes a simple, non-locale
aware way to format a number with a thousands separator. That provides
a way to humanize a program’s output, improving its professional
appearance and readability:
>>> format(1234567, ',d')
'1,234,567'
>>> format(1234567.89, ',.2f')
'1,234,567.89'
>>> format(12345.6 + 8901234.12j, ',f')
'12,345.600000+8,901,234.120000j'
>>> format(Decimal('1234567.89'), ',f')
'1,234,567.89'
The supported types are *note int: 184, *note float: 187, *note complex:
189. and *note decimal.Decimal: 188.
Discussions are underway about how to specify alternative separators
like dots, spaces, apostrophes, or underscores. Locale-aware
applications should use the existing `n' format specifier which already
has some support for thousands separators.
See also
........
PEP 378(1) - Format Specifier for Thousands Separator
PEP written by Raymond Hettinger and implemented by Eric Smith and
Mark Dickinson.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0378
File: python.info, Node: Other Language Changes<8>, Next: New Improved and Deprecated Modules<2>, Prev: PEP 378 Format Specifier for Thousands Separator, Up: What’s New In Python 3 1
1.8.3 Other Language Changes
----------------------------
Some smaller changes made to the core Python language are:
* Directories and zip archives containing a ‘__main__.py’ file can
now be executed directly by passing their name to the interpreter.
The directory/zipfile is automatically inserted as the first entry
in sys.path. (Suggestion and initial patch by Andy Chu; revised
patch by Phillip J. Eby and Nick Coghlan; bpo-1739468(1).)
* The *note int(): 184. type gained a ‘bit_length’ method that
returns the number of bits necessary to represent its argument in
binary:
>>> n = 37
>>> bin(37)
'0b100101'
>>> n.bit_length()
6
>>> n = 2**123-1
>>> n.bit_length()
123
>>> (n+1).bit_length()
124
(Contributed by Fredrik Johansson, Victor Stinner, Raymond
Hettinger, and Mark Dickinson; bpo-3439(2).)
* The fields in *note format(): 4db. strings can now be automatically
numbered:
>>> 'Sir {} of {}'.format('Gallahad', 'Camelot')
'Sir Gallahad of Camelot'
Formerly, the string would have required numbered fields such as:
‘'Sir {0} of {1}'’.
(Contributed by Eric Smith; bpo-5237(3).)
* The ‘string.maketrans()’ function is deprecated and is replaced by
new static methods, *note bytes.maketrans(): c09. and *note
bytearray.maketrans(): c0a. This change solves the confusion
around which types were supported by the *note string: f6. module.
Now, *note str: 330, *note bytes: 331, and *note bytearray: 332.
each have their own `maketrans' and `translate' methods with
intermediate translation tables of the appropriate type.
(Contributed by Georg Brandl; bpo-5675(4).)
* The syntax of the *note with: 6e9. statement now allows multiple
context managers in a single statement:
>>> with open('mylog.txt') as infile, open('a.out', 'w') as outfile:
... for line in infile:
... if '' in line:
... outfile.write(line)
With the new syntax, the ‘contextlib.nested()’ function is no
longer needed and is now deprecated.
(Contributed by Georg Brandl and Mattias Brändström; appspot issue
53094(5).)
* ‘round(x, n)’ now returns an integer if `x' is an integer.
Previously it returned a float:
>>> round(1123, -2)
1100
(Contributed by Mark Dickinson; bpo-4707(6).)
* Python now uses David Gay’s algorithm for finding the shortest
floating point representation that doesn’t change its value. This
should help mitigate some of the confusion surrounding binary
floating point numbers.
The significance is easily seen with a number like ‘1.1’ which does
not have an exact equivalent in binary floating point. Since there
is no exact equivalent, an expression like ‘float('1.1')’ evaluates
to the nearest representable value which is ‘0x1.199999999999ap+0’
in hex or ‘1.100000000000000088817841970012523233890533447265625’
in decimal. That nearest value was and still is used in subsequent
floating point calculations.
What is new is how the number gets displayed. Formerly, Python
used a simple approach. The value of ‘repr(1.1)’ was computed as
‘format(1.1, '.17g')’ which evaluated to ‘'1.1000000000000001'’.
The advantage of using 17 digits was that it relied on IEEE-754
guarantees to assure that ‘eval(repr(1.1))’ would round-trip
exactly to its original value. The disadvantage is that many
people found the output to be confusing (mistaking intrinsic
limitations of binary floating point representation as being a
problem with Python itself).
The new algorithm for ‘repr(1.1)’ is smarter and returns ‘'1.1'’.
Effectively, it searches all equivalent string representations
(ones that get stored with the same underlying float value) and
returns the shortest representation.
The new algorithm tends to emit cleaner representations when
possible, but it does not change the underlying values. So, it is
still the case that ‘1.1 + 2.2 != 3.3’ even though the
representations may suggest otherwise.
The new algorithm depends on certain features in the underlying
floating point implementation. If the required features are not
found, the old algorithm will continue to be used. Also, the text
pickle protocols assure cross-platform portability by using the old
algorithm.
(Contributed by Eric Smith and Mark Dickinson; bpo-1580(7))
---------- Footnotes ----------
(1) https://bugs.python.org/issue1739468
(2) https://bugs.python.org/issue3439
(3) https://bugs.python.org/issue5237
(4) https://bugs.python.org/issue5675
(5) https://codereview.appspot.com/53094
(6) https://bugs.python.org/issue4707
(7) https://bugs.python.org/issue1580
File: python.info, Node: New Improved and Deprecated Modules<2>, Next: Optimizations<7>, Prev: Other Language Changes<8>, Up: What’s New In Python 3 1
1.8.4 New, Improved, and Deprecated Modules
-------------------------------------------
* Added a *note collections.Counter: 9da. class to support convenient
counting of unique items in a sequence or iterable:
>>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
Counter({'blue': 3, 'red': 2, 'green': 1})
(Contributed by Raymond Hettinger; bpo-1696199(1).)
* Added a new module, *note tkinter.ttk: 10f. for access to the Tk
themed widget set. The basic idea of ttk is to separate, to the
extent possible, the code implementing a widget’s behavior from the
code implementing its appearance.
(Contributed by Guilherme Polo; bpo-2983(2).)
* The *note gzip.GzipFile: 6dd. and *note bz2.BZ2File: 931. classes
now support the context management protocol:
>>> # Automatically close file after writing
>>> with gzip.GzipFile(filename, "wb") as f:
... f.write(b"xxx")
(Contributed by Antoine Pitrou.)
* The *note decimal: 36. module now supports methods for creating a
decimal object from a binary *note float: 187. The conversion is
exact but can sometimes be surprising:
>>> Decimal.from_float(1.1)
Decimal('1.100000000000000088817841970012523233890533447265625')
The long decimal result shows the actual binary fraction being
stored for `1.1'. The fraction has many digits because `1.1'
cannot be exactly represented in binary.
(Contributed by Raymond Hettinger and Mark Dickinson.)
* The *note itertools: a3. module grew two new functions. The *note
itertools.combinations_with_replacement(): c19. function is one of
four for generating combinatorics including permutations and
Cartesian products. The *note itertools.compress(): c1a. function
mimics its namesake from APL. Also, the existing *note
itertools.count(): c1b. function now has an optional `step'
argument and can accept any type of counting sequence including
*note fractions.Fraction: 185. and *note decimal.Decimal: 188.:
>>> [p+q for p,q in combinations_with_replacement('LOVE', 2)]
['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']
>>> list(compress(data=range(10), selectors=[0,0,1,1,0,1,0,1,0,0]))
[2, 3, 5, 7]
>>> c = count(start=Fraction(1,2), step=Fraction(1,6))
>>> [next(c), next(c), next(c), next(c)]
[Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)]
(Contributed by Raymond Hettinger.)
* *note collections.namedtuple(): 1b7. now supports a keyword
argument `rename' which lets invalid fieldnames be automatically
converted to positional names in the form _0, _1, etc. This is
useful when the field names are being created by an external source
such as a CSV header, SQL field list, or user input:
>>> query = input()
SELECT region, dept, count(*) FROM main GROUPBY region, dept
>>> cursor.execute(query)
>>> query_fields = [desc[0] for desc in cursor.description]
>>> UserQuery = namedtuple('UserQuery', query_fields, rename=True)
>>> pprint.pprint([UserQuery(*row) for row in cursor])
[UserQuery(region='South', dept='Shipping', _2=185),
UserQuery(region='North', dept='Accounting', _2=37),
UserQuery(region='West', dept='Sales', _2=419)]
(Contributed by Raymond Hettinger; bpo-1818(3).)
* The *note re.sub(): 47b, *note re.subn(): 734. and *note
re.split(): 3ca. functions now accept a flags parameter.
(Contributed by Gregory Smith.)
* The *note logging: aa. module now implements a simple *note
logging.NullHandler: c1c. class for applications that are not using
logging but are calling library code that does. Setting-up a null
handler will suppress spurious warnings such as “No handlers could
be found for logger foo”:
>>> h = logging.NullHandler()
>>> logging.getLogger("foo").addHandler(h)
(Contributed by Vinay Sajip; bpo-4384(4)).
* The *note runpy: e2. module which supports the ‘-m’ command line
switch now supports the execution of packages by looking for and
executing a ‘__main__’ submodule when a package name is supplied.
(Contributed by Andi Vajda; bpo-4195(5).)
* The *note pdb: c9. module can now access and display source code
loaded via *note zipimport: 143. (or any other conformant PEP
302(6) loader).
(Contributed by Alexander Belopolsky; bpo-4201(7).)
* *note functools.partial: 7c8. objects can now be pickled.
(Suggested by Antoine Pitrou and Jesse Noller. Implemented by Jack
Diederich; bpo-5228(8).)
* Add *note pydoc: d9. help topics for symbols so that ‘help('@')’
works as expected in the interactive environment.
(Contributed by David Laban; bpo-4739(9).)
* The *note unittest: 11b. module now supports skipping individual
tests or classes of tests. And it supports marking a test as an
expected failure, a test that is known to be broken, but shouldn’t
be counted as a failure on a TestResult:
class TestGizmo(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_gizmo_on_windows(self):
...
@unittest.expectedFailure
def test_gimzo_without_required_library(self):
...
Also, tests for exceptions have been builtout to work with context
managers using the *note with: 6e9. statement:
def test_division_by_zero(self):
with self.assertRaises(ZeroDivisionError):
x / 0
In addition, several new assertion methods were added including
‘assertSetEqual()’, ‘assertDictEqual()’,
‘assertDictContainsSubset()’, ‘assertListEqual()’,
‘assertTupleEqual()’, ‘assertSequenceEqual()’,
‘assertRaisesRegexp()’, ‘assertIsNone()’, and ‘assertIsNotNone()’.
(Contributed by Benjamin Peterson and Antoine Pitrou.)
* The *note io: a1. module has three new constants for the ‘seek()’
method ‘SEEK_SET’, ‘SEEK_CUR’, and ‘SEEK_END’.
* The *note sys.version_info: b1a. tuple is now a named tuple:
>>> sys.version_info
sys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2)
(Contributed by Ross Light; bpo-4285(10).)
* The *note nntplib: c0. and *note imaplib: 98. modules now support
IPv6.
(Contributed by Derek Morr; bpo-1655(11) and bpo-1664(12).)
* The *note pickle: ca. module has been adapted for better
interoperability with Python 2.x when used with protocol 2 or
lower. The reorganization of the standard library changed the
formal reference for many objects. For example, ‘__builtin__.set’
in Python 2 is called ‘builtins.set’ in Python 3. This change
confounded efforts to share data between different versions of
Python. But now when protocol 2 or lower is selected, the pickler
will automatically use the old Python 2 names for both loading and
dumping. This remapping is turned-on by default but can be
disabled with the `fix_imports' option:
>>> s = {1, 2, 3}
>>> pickle.dumps(s, protocol=0)
b'c__builtin__\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'
>>> pickle.dumps(s, protocol=0, fix_imports=False)
b'cbuiltins\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'
An unfortunate but unavoidable side-effect of this change is that
protocol 2 pickles produced by Python 3.1 won’t be readable with
Python 3.0. The latest pickle protocol, protocol 3, should be used
when migrating data between Python 3.x implementations, as it
doesn’t attempt to remain compatible with Python 2.x.
(Contributed by Alexandre Vassalotti and Antoine Pitrou,
bpo-6137(13).)
* A new module, *note importlib: 9b. was added. It provides a
complete, portable, pure Python reference implementation of the
*note import: c1d. statement and its counterpart, the *note
__import__(): 610. function. It represents a substantial step
forward in documenting and defining the actions that take place
during imports.
(Contributed by Brett Cannon.)
---------- Footnotes ----------
(1) https://bugs.python.org/issue1696199
(2) https://bugs.python.org/issue2983
(3) https://bugs.python.org/issue1818
(4) https://bugs.python.org/issue4384
(5) https://bugs.python.org/issue4195
(6) https://www.python.org/dev/peps/pep-0302
(7) https://bugs.python.org/issue4201
(8) https://bugs.python.org/issue5228
(9) https://bugs.python.org/issue4739
(10) https://bugs.python.org/issue4285
(11) https://bugs.python.org/issue1655
(12) https://bugs.python.org/issue1664
(13) https://bugs.python.org/issue6137
File: python.info, Node: Optimizations<7>, Next: IDLE<2>, Prev: New Improved and Deprecated Modules<2>, Up: What’s New In Python 3 1
1.8.5 Optimizations
-------------------
Major performance enhancements have been added:
* The new I/O library (as defined in PEP 3116(1)) was mostly written
in Python and quickly proved to be a problematic bottleneck in
Python 3.0. In Python 3.1, the I/O library has been entirely
rewritten in C and is 2 to 20 times faster depending on the task at
hand. The pure Python version is still available for
experimentation purposes through the ‘_pyio’ module.
(Contributed by Amaury Forgeot d’Arc and Antoine Pitrou.)
* Added a heuristic so that tuples and dicts containing only
untrackable objects are not tracked by the garbage collector. This
can reduce the size of collections and therefore the garbage
collection overhead on long-running programs, depending on their
particular use of datatypes.
(Contributed by Antoine Pitrou, bpo-4688(2).)
* Enabling a configure option named ‘--with-computed-gotos’ on
compilers that support it (notably: gcc, SunPro, icc), the bytecode
evaluation loop is compiled with a new dispatch mechanism which
gives speedups of up to 20%, depending on the system, the compiler,
and the benchmark.
(Contributed by Antoine Pitrou along with a number of other
participants, bpo-4753(3)).
* The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times
faster.
(Contributed by Antoine Pitrou and Amaury Forgeot d’Arc,
bpo-4868(4).)
* The *note json: a4. module now has a C extension to substantially
improve its performance. In addition, the API was modified so that
json works only with *note str: 330, not with *note bytes: 331.
That change makes the module closely match the JSON
specification(5) which is defined in terms of Unicode.
(Contributed by Bob Ippolito and converted to Py3.1 by Antoine
Pitrou and Benjamin Peterson; bpo-4136(6).)
* Unpickling now interns the attribute names of pickled objects.
This saves memory and allows pickles to be smaller.
(Contributed by Jake McGuire and Antoine Pitrou; bpo-5084(7).)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3116
(2) https://bugs.python.org/issue4688
(3) https://bugs.python.org/issue4753
(4) https://bugs.python.org/issue4868
(5) http://json.org/
(6) https://bugs.python.org/issue4136
(7) https://bugs.python.org/issue5084
File: python.info, Node: IDLE<2>, Next: Build and C API Changes<6>, Prev: Optimizations<7>, Up: What’s New In Python 3 1
1.8.6 IDLE
----------
* IDLE’s format menu now provides an option to strip trailing
whitespace from a source file.
(Contributed by Roger D. Serwy; bpo-5150(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5150
File: python.info, Node: Build and C API Changes<6>, Next: Porting to Python 3 1, Prev: IDLE<2>, Up: What’s New In Python 3 1
1.8.7 Build and C API Changes
-----------------------------
Changes to Python’s build process and to the C API include:
* Integers are now stored internally either in base 2**15 or in base
2**30, the base being determined at build time. Previously, they
were always stored in base 2**15. Using base 2**30 gives
significant performance improvements on 64-bit machines, but
benchmark results on 32-bit machines have been mixed. Therefore,
the default is to use base 2**30 on 64-bit machines and base 2**15
on 32-bit machines; on Unix, there’s a new configure option
‘--enable-big-digits’ that can be used to override this default.
Apart from the performance improvements this change should be
invisible to end users, with one exception: for testing and
debugging purposes there’s a new *note sys.int_info: c21. that
provides information about the internal format, giving the number
of bits per digit and the size in bytes of the C type used to store
each digit:
>>> import sys
>>> sys.int_info
sys.int_info(bits_per_digit=30, sizeof_digit=4)
(Contributed by Mark Dickinson; bpo-4258(1).)
* The *note PyLong_AsUnsignedLongLong(): c22. function now handles a
negative `pylong' by raising *note OverflowError: 960. instead of
*note TypeError: 192.
(Contributed by Mark Dickinson and Lisandro Dalcrin; bpo-5175(2).)
* Deprecated ‘PyNumber_Int()’. Use *note PyNumber_Long(): 26c.
instead.
(Contributed by Mark Dickinson; bpo-4910(3).)
* Added a new *note PyOS_string_to_double(): c23. function to replace
the deprecated functions ‘PyOS_ascii_strtod()’ and
‘PyOS_ascii_atof()’.
(Contributed by Mark Dickinson; bpo-5914(4).)
* Added *note PyCapsule: c07. as a replacement for the ‘PyCObject’
API. The principal difference is that the new type has a well
defined interface for passing typing safety information and a less
complicated signature for calling a destructor. The old type had a
problematic API and is now deprecated.
(Contributed by Larry Hastings; bpo-5630(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4258
(2) https://bugs.python.org/issue5175
(3) https://bugs.python.org/issue4910
(4) https://bugs.python.org/issue5914
(5) https://bugs.python.org/issue5630
File: python.info, Node: Porting to Python 3 1, Prev: Build and C API Changes<6>, Up: What’s New In Python 3 1
1.8.8 Porting to Python 3.1
---------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code:
* The new floating point string representations can break existing
doctests. For example:
def e():
'''Compute the base of natural logarithms.
>>> e()
2.7182818284590451
'''
return sum(1/math.factorial(x) for x in reversed(range(30)))
doctest.testmod()
**********************************************************************
Failed example:
e()
Expected:
2.7182818284590451
Got:
2.718281828459045
**********************************************************************
* The automatic name remapping in the pickle module for protocol 2 or
lower can make Python 3.1 pickles unreadable in Python 3.0. One
solution is to use protocol 3. Another solution is to set the
`fix_imports' option to ‘False’. See the discussion above for more
details.
File: python.info, Node: What’s New In Python 3 0, Next: What’s New in Python 2 7, Prev: What’s New In Python 3 1, Up: What’s New in Python
1.9 What’s New In Python 3.0
============================
Author: Guido van Rossum
This article explains the new features in Python 3.0, compared to 2.6.
Python 3.0, also known as “Python 3000” or “Py3K”, is the first ever
`intentionally backwards incompatible' Python release. There are more
changes than in a typical release, and more that are important for all
Python users. Nevertheless, after digesting the changes, you’ll find
that Python really hasn’t changed all that much – by and large, we’re
mostly fixing well-known annoyances and warts, and removing a lot of old
cruft.
This article doesn’t attempt to provide a complete specification of all
new features, but instead tries to give a convenient overview. For full
details, you should refer to the documentation for Python 3.0, and/or
the many PEPs referenced in the text. If you want to understand the
complete implementation and design rationale for a particular feature,
PEPs usually have more details than the regular documentation; but note
that PEPs usually are not kept up-to-date once a feature has been fully
implemented.
Due to time constraints this document is not as complete as it should
have been. As always for a new release, the ‘Misc/NEWS’ file in the
source distribution contains a wealth of detailed information about
every small thing that was changed.
* Menu:
* Common Stumbling Blocks::
* Overview Of Syntax Changes::
* Changes Already Present In Python 2.6: Changes Already Present In Python 2 6.
* Library Changes::
* PEP 3101; A New Approach To String Formatting: PEP 3101 A New Approach To String Formatting.
* Changes To Exceptions::
* Miscellaneous Other Changes::
* Build and C API Changes: Build and C API Changes<7>.
* Performance::
* Porting To Python 3.0: Porting To Python 3 0.
File: python.info, Node: Common Stumbling Blocks, Next: Overview Of Syntax Changes, Up: What’s New In Python 3 0
1.9.1 Common Stumbling Blocks
-----------------------------
This section lists those few changes that are most likely to trip you up
if you’re used to Python 2.5.
* Menu:
* Print Is A Function::
* Views And Iterators Instead Of Lists::
* Ordering Comparisons::
* Integers::
* Text Vs. Data Instead Of Unicode Vs. 8-bit: Text Vs Data Instead Of Unicode Vs 8-bit.
File: python.info, Node: Print Is A Function, Next: Views And Iterators Instead Of Lists, Up: Common Stumbling Blocks
1.9.1.1 Print Is A Function
...........................
The ‘print’ statement has been replaced with a *note print(): 886.
function, with keyword arguments to replace most of the special syntax
of the old ‘print’ statement ( PEP 3105(1)). Examples:
Old: print "The answer is", 2*2
New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)
Old: print (x, y) # prints repr((x, y))
New: print((x, y)) # Not the same as print(x, y)!
You can also customize the separator between items, e.g.:
print("There are <", 2**32, "> possibilities!", sep="")
which produces:
There are <4294967296> possibilities!
Note:
* The *note print(): 886. function doesn’t support the “softspace”
feature of the old ‘print’ statement. For example, in Python 2.x,
‘print "A\n", "B"’ would write ‘"A\nB\n"’; but in Python 3.0,
‘print("A\n", "B")’ writes ‘"A\n B\n"’.
* Initially, you’ll be finding yourself typing the old ‘print x’ a
lot in interactive mode. Time to retrain your fingers to type
‘print(x)’ instead!
* When using the ‘2to3’ source-to-source conversion tool, all ‘print’
statements are automatically converted to *note print(): 886.
function calls, so this is mostly a non-issue for larger projects.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3105
File: python.info, Node: Views And Iterators Instead Of Lists, Next: Ordering Comparisons, Prev: Print Is A Function, Up: Common Stumbling Blocks
1.9.1.2 Views And Iterators Instead Of Lists
............................................
Some well-known APIs no longer return lists:
* *note dict: 1b8. methods *note dict.keys(): c2a, *note
dict.items(): c2b. and *note dict.values(): c2c. return “views”
instead of lists. For example, this no longer works: ‘k =
d.keys(); k.sort()’. Use ‘k = sorted(d)’ instead (this works in
Python 2.5 too and is just as efficient).
* Also, the ‘dict.iterkeys()’, ‘dict.iteritems()’ and
‘dict.itervalues()’ methods are no longer supported.
* *note map(): c2d. and *note filter(): c2e. return iterators. If
you really need a list and the input sequences are all of equal
length, a quick fix is to wrap *note map(): c2d. in *note list():
262, e.g. ‘list(map(...))’, but a better fix is often to use a
list comprehension (especially when the original code uses *note
lambda: c2f.), or rewriting the code so it doesn’t need a list at
all. Particularly tricky is *note map(): c2d. invoked for the side
effects of the function; the correct transformation is to use a
regular *note for: c30. loop (since creating a list would just be
wasteful).
If the input sequences are not of equal length, *note map(): c2d.
will stop at the termination of the shortest of the sequences. For
full compatibility with *note map(): c2d. from Python 2.x, also
wrap the sequences in *note itertools.zip_longest(): c31, e.g.
‘map(func, *sequences)’ becomes ‘list(map(func,
itertools.zip_longest(*sequences)))’.
* *note range(): 9be. now behaves like ‘xrange()’ used to behave,
except it works with values of arbitrary size. The latter no
longer exists.
* *note zip(): c32. now returns an iterator.
File: python.info, Node: Ordering Comparisons, Next: Integers, Prev: Views And Iterators Instead Of Lists, Up: Common Stumbling Blocks
1.9.1.3 Ordering Comparisons
............................
Python 3.0 has simplified the rules for ordering comparisons:
* The ordering comparison operators (‘<’, ‘<=’, ‘>=’, ‘>’) raise a
TypeError exception when the operands don’t have a meaningful
natural ordering. Thus, expressions like ‘1 < ''’, ‘0 > None’ or
‘len <= len’ are no longer valid, and e.g. ‘None < None’ raises
*note TypeError: 192. instead of returning ‘False’. A corollary is
that sorting a heterogeneous list no longer makes sense – all the
elements must be comparable to each other. Note that this does not
apply to the ‘==’ and ‘!=’ operators: objects of different
incomparable types always compare unequal to each other.
* ‘builtin.sorted()’ and *note list.sort(): 445. no longer accept the
`cmp' argument providing a comparison function. Use the `key'
argument instead. N.B. the `key' and `reverse' arguments are now
“keyword-only”.
* The ‘cmp()’ function should be treated as gone, and the ‘__cmp__()’
special method is no longer supported. Use *note __lt__(): c34.
for sorting, *note __eq__(): 33f. with *note __hash__(): 340, and
other rich comparisons as needed. (If you really need the ‘cmp()’
functionality, you could use the expression ‘(a > b) - (a < b)’ as
the equivalent for ‘cmp(a, b)’.)
File: python.info, Node: Integers, Next: Text Vs Data Instead Of Unicode Vs 8-bit, Prev: Ordering Comparisons, Up: Common Stumbling Blocks
1.9.1.4 Integers
................
* PEP 237(1): Essentially, ‘long’ renamed to *note int: 184. That
is, there is only one built-in integral type, named *note int:
184.; but it behaves mostly like the old ‘long’ type.
* PEP 238(2): An expression like ‘1/2’ returns a float. Use ‘1//2’
to get the truncating behavior. (The latter syntax has existed for
years, at least since Python 2.2.)
* The ‘sys.maxint’ constant was removed, since there is no longer a
limit to the value of integers. However, *note sys.maxsize: b43.
can be used as an integer larger than any practical list or string
index. It conforms to the implementation’s “natural” integer size
and is typically the same as ‘sys.maxint’ in previous releases on
the same platform (assuming the same build options).
* The *note repr(): 7cc. of a long integer doesn’t include the
trailing ‘L’ anymore, so code that unconditionally strips that
character will chop off the last digit instead. (Use *note str():
330. instead.)
* Octal literals are no longer of the form ‘0720’; use ‘0o720’
instead.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0237
(2) https://www.python.org/dev/peps/pep-0238
File: python.info, Node: Text Vs Data Instead Of Unicode Vs 8-bit, Prev: Integers, Up: Common Stumbling Blocks
1.9.1.5 Text Vs. Data Instead Of Unicode Vs. 8-bit
..................................................
Everything you thought you knew about binary data and Unicode has
changed.
* Python 3.0 uses the concepts of `text' and (binary) `data' instead
of Unicode strings and 8-bit strings. All text is Unicode; however
`encoded' Unicode is represented as binary data. The type used to
hold text is *note str: 330, the type used to hold data is *note
bytes: 331. The biggest difference with the 2.x situation is that
any attempt to mix text and data in Python 3.0 raises *note
TypeError: 192, whereas if you were to mix Unicode and 8-bit
strings in Python 2.x, it would work if the 8-bit string happened
to contain only 7-bit (ASCII) bytes, but you would get *note
UnicodeDecodeError: 1fd. if it contained non-ASCII values. This
value-specific behavior has caused numerous sad faces over the
years.
* As a consequence of this change in philosophy, pretty much all code
that uses Unicode, encodings or binary data most likely has to
change. The change is for the better, as in the 2.x world there
were numerous bugs having to do with mixing encoded and unencoded
text. To be prepared in Python 2.x, start using ‘unicode’ for all
unencoded text, and *note str: 330. for binary or encoded data
only. Then the ‘2to3’ tool will do most of the work for you.
* You can no longer use ‘u"..."’ literals for Unicode text. However,
you must use ‘b"..."’ literals for binary data.
* As the *note str: 330. and *note bytes: 331. types cannot be mixed,
you must always explicitly convert between them. Use *note
str.encode(): c37. to go from *note str: 330. to *note bytes: 331,
and *note bytes.decode(): c38. to go from *note bytes: 331. to
*note str: 330. You can also use ‘bytes(s, encoding=...)’ and
‘str(b, encoding=...)’, respectively.
* Like *note str: 330, the *note bytes: 331. type is immutable.
There is a separate `mutable' type to hold buffered binary data,
*note bytearray: 332. Nearly all APIs that accept *note bytes:
331. also accept *note bytearray: 332. The mutable API is based on
‘collections.MutableSequence’.
* All backslashes in raw string literals are interpreted literally.
This means that ‘'\U'’ and ‘'\u'’ escapes in raw strings are not
treated specially. For example, ‘r'\u20ac'’ is a string of 6
characters in Python 3.0, whereas in 2.6, ‘ur'\u20ac'’ was the
single “euro” character. (Of course, this change only affects raw
string literals; the euro character is ‘'\u20ac'’ in Python 3.0.)
* The built-in ‘basestring’ abstract type was removed. Use *note
str: 330. instead. The *note str: 330. and *note bytes: 331. types
don’t have functionality enough in common to warrant a shared base
class. The ‘2to3’ tool (see below) replaces every occurrence of
‘basestring’ with *note str: 330.
* Files opened as text files (still the default mode for *note
open(): 4f0.) always use an encoding to map between strings (in
memory) and bytes (on disk). Binary files (opened with a ‘b’ in
the mode argument) always use bytes in memory. This means that if
a file is opened using an incorrect mode or encoding, I/O will
likely fail loudly, instead of silently producing incorrect data.
It also means that even Unix users will have to specify the correct
mode (text or binary) when opening a file. There is a
platform-dependent default encoding, which on Unixy platforms can
be set with the ‘LANG’ environment variable (and sometimes also
with some other platform-specific locale-related environment
variables). In many cases, but not all, the system default is
UTF-8; you should never count on this default. Any application
reading or writing more than pure ASCII text should probably have a
way to override the encoding. There is no longer any need for
using the encoding-aware streams in the *note codecs: 1c. module.
* The initial values of *note sys.stdin: 30d, *note sys.stdout: 30e.
and *note sys.stderr: 30f. are now unicode-only text files (i.e.,
they are instances of *note io.TextIOBase: c39.). To read and
write bytes data with these streams, you need to use their *note
io.TextIOBase.buffer: c3a. attribute.
* Filenames are passed to and returned from APIs as (Unicode)
strings. This can present platform-specific problems because on
some platforms filenames are arbitrary byte strings. (On the other
hand, on Windows filenames are natively stored as Unicode.) As a
work-around, most APIs (e.g. *note open(): 4f0. and many functions
in the *note os: c4. module) that take filenames accept *note
bytes: 331. objects as well as strings, and a few APIs have a way
to ask for a *note bytes: 331. return value. Thus, *note
os.listdir(): a41. returns a list of *note bytes: 331. instances if
the argument is a *note bytes: 331. instance, and *note
os.getcwdb(): 2b8. returns the current working directory as a *note
bytes: 331. instance. Note that when *note os.listdir(): a41.
returns a list of strings, filenames that cannot be decoded
properly are omitted rather than raising *note UnicodeError: c3b.
* Some system APIs like *note os.environ: b37. and *note sys.argv:
bfb. can also present problems when the bytes made available by the
system is not interpretable using the default encoding. Setting
the ‘LANG’ variable and rerunning the program is probably the best
approach.
* PEP 3138(1): The *note repr(): 7cc. of a string no longer escapes
non-ASCII characters. It still escapes control characters and code
points with non-printable status in the Unicode standard, however.
* PEP 3120(2): The default source encoding is now UTF-8.
* PEP 3131(3): Non-ASCII letters are now allowed in identifiers.
(However, the standard library remains ASCII-only with the
exception of contributor names in comments.)
* The ‘StringIO’ and ‘cStringIO’ modules are gone. Instead, import
the *note io: a1. module and use *note io.StringIO: 82d. or *note
io.BytesIO: 79b. for text and data respectively.
* See also the *note Unicode HOWTO: c3c, which was updated for Python
3.0.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3138
(2) https://www.python.org/dev/peps/pep-3120
(3) https://www.python.org/dev/peps/pep-3131
File: python.info, Node: Overview Of Syntax Changes, Next: Changes Already Present In Python 2 6, Prev: Common Stumbling Blocks, Up: What’s New In Python 3 0
1.9.2 Overview Of Syntax Changes
--------------------------------
This section gives a brief overview of every `syntactic' change in
Python 3.0.
* Menu:
* New Syntax::
* Changed Syntax::
* Removed Syntax::
File: python.info, Node: New Syntax, Next: Changed Syntax, Up: Overview Of Syntax Changes
1.9.2.1 New Syntax
..................
* PEP 3107(1): Function argument and return value annotations. This
provides a standardized way of annotating a function’s parameters
and return value. There are no semantics attached to such
annotations except that they can be introspected at runtime using
the ‘__annotations__’ attribute. The intent is to encourage
experimentation through metaclasses, decorators or frameworks.
* PEP 3102(2): Keyword-only arguments. Named parameters occurring
after ‘*args’ in the parameter list `must' be specified using
keyword syntax in the call. You can also use a bare ‘*’ in the
parameter list to indicate that you don’t accept a variable-length
argument list, but you do have keyword-only arguments.
* Keyword arguments are allowed after the list of base classes in a
class definition. This is used by the new convention for
specifying a metaclass (see next section), but can be used for
other purposes as well, as long as the metaclass supports it.
* PEP 3104(3): *note nonlocal: c3f. statement. Using ‘nonlocal x’
you can now assign directly to a variable in an outer (but
non-global) scope. ‘nonlocal’ is a new reserved word.
* PEP 3132(4): Extended Iterable Unpacking. You can now write things
like ‘a, b, *rest = some_sequence’. And even ‘*rest, a = stuff’.
The ‘rest’ object is always a (possibly empty) list; the right-hand
side may be any iterable. Example:
(a, *rest, b) = range(5)
This sets `a' to ‘0’, `b' to ‘4’, and `rest' to ‘[1, 2, 3]’.
* Dictionary comprehensions: ‘{k: v for k, v in stuff}’ means the
same thing as ‘dict(stuff)’ but is more flexible. (This is PEP
274(5) vindicated. :-)
* Set literals, e.g. ‘{1, 2}’. Note that ‘{}’ is an empty
dictionary; use ‘set()’ for an empty set. Set comprehensions are
also supported; e.g., ‘{x for x in stuff}’ means the same thing as
‘set(stuff)’ but is more flexible.
* New octal literals, e.g. ‘0o720’ (already in 2.6). The old octal
literals (‘0720’) are gone.
* New binary literals, e.g. ‘0b1010’ (already in 2.6), and there is
a new corresponding built-in function, *note bin(): c40.
* Bytes literals are introduced with a leading ‘b’ or ‘B’, and there
is a new corresponding built-in function, *note bytes(): 331.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3107
(2) https://www.python.org/dev/peps/pep-3102
(3) https://www.python.org/dev/peps/pep-3104
(4) https://www.python.org/dev/peps/pep-3132
(5) https://www.python.org/dev/peps/pep-0274
File: python.info, Node: Changed Syntax, Next: Removed Syntax, Prev: New Syntax, Up: Overview Of Syntax Changes
1.9.2.2 Changed Syntax
......................
* PEP 3109(1) and PEP 3134(2): new *note raise: c42. statement
syntax: ‘raise [`expr' [from `expr']]’. See below.
* ‘as’ and *note with: 6e9. are now reserved words. (Since 2.6,
actually.)
* ‘True’, ‘False’, and ‘None’ are reserved words. (2.6 partially
enforced the restrictions on ‘None’ already.)
* Change from *note except: b3e. `exc', `var' to ‘except’ `exc' ‘as’
`var'. See PEP 3110(3).
* PEP 3115(4): New Metaclass Syntax. Instead of:
class C:
__metaclass__ = M
...
you must now use:
class C(metaclass=M):
...
The module-global ‘__metaclass__’ variable is no longer supported.
(It was a crutch to make it easier to default to new-style classes
without deriving every class from *note object: 2b0.)
* List comprehensions no longer support the syntactic form ‘[... for
`var' in `item1', `item2', ...]’. Use ‘[... for `var' in (`item1',
`item2', ...)]’ instead. Also note that list comprehensions have
different semantics: they are closer to syntactic sugar for a
generator expression inside a *note list(): 262. constructor, and
in particular the loop control variables are no longer leaked into
the surrounding scope.
* The `ellipsis' (‘...’) can be used as an atomic expression
anywhere. (Previously it was only allowed in slices.) Also, it
`must' now be spelled as ‘...’. (Previously it could also be
spelled as ‘. . .’, by a mere accident of the grammar.)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3109
(2) https://www.python.org/dev/peps/pep-3134
(3) https://www.python.org/dev/peps/pep-3110
(4) https://www.python.org/dev/peps/pep-3115
File: python.info, Node: Removed Syntax, Prev: Changed Syntax, Up: Overview Of Syntax Changes
1.9.2.3 Removed Syntax
......................
* PEP 3113(1): Tuple parameter unpacking removed. You can no longer
write ‘def foo(a, (b, c)): ...’. Use ‘def foo(a, b_c): b, c = b_c’
instead.
* Removed backticks (use *note repr(): 7cc. instead).
* Removed ‘<>’ (use ‘!=’ instead).
* Removed keyword: *note exec(): c44. is no longer a keyword; it
remains as a function. (Fortunately the function syntax was also
accepted in 2.x.) Also note that *note exec(): c44. no longer
takes a stream argument; instead of ‘exec(f)’ you can use
‘exec(f.read())’.
* Integer literals no longer support a trailing ‘l’ or ‘L’.
* String literals no longer support a leading ‘u’ or ‘U’.
* The *note from: c45. `module' *note import: c1d. ‘*’ syntax is only
allowed at the module level, no longer inside functions.
* The only acceptable syntax for relative imports is ‘from
.[`module'] import `name'’. All *note import: c1d. forms not
starting with ‘.’ are interpreted as absolute imports. ( PEP
328(2))
* Classic classes are gone.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3113
(2) https://www.python.org/dev/peps/pep-0328
File: python.info, Node: Changes Already Present In Python 2 6, Next: Library Changes, Prev: Overview Of Syntax Changes, Up: What’s New In Python 3 0
1.9.3 Changes Already Present In Python 2.6
-------------------------------------------
Since many users presumably make the jump straight from Python 2.5 to
Python 3.0, this section reminds the reader of new features that were
originally designed for Python 3.0 but that were back-ported to Python
2.6. The corresponding sections in *note What’s New in Python 2.6: c47.
should be consulted for longer descriptions.
* *note PEP 343; The ‘with’ statement: c48. The *note with: 6e9.
statement is now a standard feature and no longer needs to be
imported from the *note __future__: 0. Also check out *note
Writing Context Managers: c49. and *note The contextlib module:
c4a.
* *note PEP 366; Explicit Relative Imports From a Main Module: c4b.
This enhances the usefulness of the *note -m: 337. option when the
referenced module lives in a package.
* *note PEP 370; Per-user site-packages Directory: c4c.
* *note PEP 371; The multiprocessing Package: c4d.
* *note PEP 3101; Advanced String Formatting: c4e. Note: the 2.6
description mentions the *note format(): 4db. method for both 8-bit
and Unicode strings. In 3.0, only the *note str: 330. type (text
strings with Unicode support) supports this method; the *note
bytes: 331. type does not. The plan is to eventually make this the
only API for string formatting, and to start deprecating the ‘%’
operator in Python 3.1.
* *note PEP 3105; print As a Function: c4f. This is now a standard
feature and no longer needs to be imported from *note __future__:
0. More details were given above.
* *note PEP 3110; Exception-Handling Changes: c50. The *note except:
b3e. `exc' ‘as’ `var' syntax is now standard and ‘except’ `exc',
`var' is no longer supported. (Of course, the ‘as’ `var' part is
still optional.)
* *note PEP 3112; Byte Literals: c51. The ‘b"..."’ string literal
notation (and its variants like ‘b'...'’, ‘b"""..."""’, and
‘br"..."’) now produces a literal of type *note bytes: 331.
* *note PEP 3116; New I/O Library: c52. The *note io: a1. module is
now the standard way of doing file I/O. The built-in *note open():
4f0. function is now an alias for *note io.open(): 65a. and has
additional keyword arguments `encoding', `errors', `newline' and
`closefd'. Also note that an invalid `mode' argument now raises
*note ValueError: 1fb, not *note IOError: 992. The binary file
object underlying a text file object can be accessed as ‘f.buffer’
(but beware that the text object maintains a buffer of itself in
order to speed up the encoding and decoding operations).
* *note PEP 3118; Revised Buffer Protocol: c53. The old builtin
‘buffer()’ is now really gone; the new builtin *note memoryview():
25c. provides (mostly) similar functionality.
* *note PEP 3119; Abstract Base Classes: c54. The *note abc: 4.
module and the ABCs defined in the *note collections: 1e. module
plays a somewhat more prominent role in the language now, and
built-in collection types like *note dict: 1b8. and *note list:
262. conform to the ‘collections.MutableMapping’ and
‘collections.MutableSequence’ ABCs, respectively.
* *note PEP 3127; Integer Literal Support and Syntax: c55. As
mentioned above, the new octal literal notation is the only one
supported, and binary literals have been added.
* *note PEP 3129; Class Decorators: c56.
* *note PEP 3141; A Type Hierarchy for Numbers: c57. The *note
numbers: c1. module is another new use of ABCs, defining Python’s
“numeric tower”. Also note the new *note fractions: 83. module
which implements *note numbers.Rational: c58.
File: python.info, Node: Library Changes, Next: PEP 3101 A New Approach To String Formatting, Prev: Changes Already Present In Python 2 6, Up: What’s New In Python 3 0
1.9.4 Library Changes
---------------------
Due to time constraints, this document does not exhaustively cover the
very extensive changes to the standard library. PEP 3108(1) is the
reference for the major changes to the library. Here’s a capsule
review:
* Many old modules were removed. Some, like ‘gopherlib’ (no longer
used) and ‘md5’ (replaced by *note hashlib: 8d.), were already
deprecated by PEP 4(2). Others were removed as a result of the
removal of support for various platforms such as Irix, BeOS and Mac
OS 9 (see PEP 11(3)). Some modules were also selected for removal
in Python 3.0 due to lack of use or because a better replacement
exists. See PEP 3108(4) for an exhaustive list.
* The ‘bsddb3’ package was removed because its presence in the core
standard library has proved over time to be a particular burden for
the core developers due to testing instability and Berkeley DB’s
release schedule. However, the package is alive and well,
externally maintained at
‘https://www.jcea.es/programacion/pybsddb.htm’.
* Some modules were renamed because their old name disobeyed PEP
8(5), or for various other reasons. Here’s the list:
Old Name New Name
--------------------------------------------------------
_winreg winreg
ConfigParser configparser
copy_reg copyreg
Queue queue
SocketServer socketserver
markupbase _markupbase
repr reprlib
test.test_support test.support
* A common pattern in Python 2.x is to have one version of a module
implemented in pure Python, with an optional accelerated version
implemented as a C extension; for example, *note pickle: ca. and
‘cPickle’. This places the burden of importing the accelerated
version and falling back on the pure Python version on each user of
these modules. In Python 3.0, the accelerated versions are
considered implementation details of the pure Python versions.
Users should always import the standard version, which attempts to
import the accelerated version and falls back to the pure Python
version. The *note pickle: ca. / ‘cPickle’ pair received this
treatment. The *note profile: d3. module is on the list for 3.1.
The ‘StringIO’ module has been turned into a class in the *note io:
a1. module.
* Some related modules have been grouped into packages, and usually
the submodule names have been simplified. The resulting new
packages are:
* *note dbm: 32. (‘anydbm’, ‘dbhash’, *note dbm: 32, ‘dumbdbm’,
‘gdbm’, ‘whichdb’).
* *note html: 90. (‘HTMLParser’, ‘htmlentitydefs’).
* *note http: 93. (‘httplib’, ‘BaseHTTPServer’, ‘CGIHTTPServer’,
‘SimpleHTTPServer’, ‘Cookie’, ‘cookielib’).
* *note tkinter: 10c. (all ‘Tkinter’-related modules except
*note turtle: 116.). The target audience of *note turtle:
116. doesn’t really care about *note tkinter: 10c. Also note
that as of Python 2.6, the functionality of *note turtle: 116.
has been greatly enhanced.
* *note urllib: 11d. (*note urllib: 11d, ‘urllib2’, ‘urlparse’,
‘robotparse’).
* ‘xmlrpc’ (‘xmlrpclib’, ‘DocXMLRPCServer’,
‘SimpleXMLRPCServer’).
Some other changes to standard library modules, not covered by PEP
3108(6):
* Killed ‘sets’. Use the built-in *note set(): b6f. class.
* Cleanup of the *note sys: fd. module: removed ‘sys.exitfunc()’,
‘sys.exc_clear()’, ‘sys.exc_type’, ‘sys.exc_value’,
‘sys.exc_traceback’. (Note that *note sys.last_type: c5a. etc.
remain.)
* Cleanup of the *note array.array: 451. type: the ‘read()’ and
‘write()’ methods are gone; use ‘fromfile()’ and ‘tofile()’
instead. Also, the ‘'c'’ typecode for array is gone – use either
‘'b'’ for bytes or ‘'u'’ for Unicode characters.
* Cleanup of the *note operator: c2. module: removed
‘sequenceIncludes()’ and ‘isCallable()’.
* Cleanup of the ‘thread’ module: ‘acquire_lock()’ and
‘release_lock()’ are gone; use ‘acquire()’ and ‘release()’ instead.
* Cleanup of the *note random: dc. module: removed the ‘jumpahead()’
API.
* The ‘new’ module is gone.
* The functions ‘os.tmpnam()’, ‘os.tempnam()’ and ‘os.tmpfile()’ have
been removed in favor of the *note tempfile: 103. module.
* The *note tokenize: 111. module has been changed to work with
bytes. The main entry point is now *note tokenize.tokenize(): c5b,
instead of generate_tokens.
* ‘string.letters’ and its friends (‘string.lowercase’ and
‘string.uppercase’) are gone. Use *note string.ascii_letters: c5c.
etc. instead. (The reason for the removal is that
‘string.letters’ and friends had locale-specific behavior, which is
a bad idea for such attractively-named global “constants”.)
* Renamed module ‘__builtin__’ to *note builtins: 13. (removing the
underscores, adding an ‘s’). The ‘__builtins__’ variable found in
most global namespaces is unchanged. To modify a builtin, you
should use *note builtins: 13, not ‘__builtins__’!
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3108
(2) https://www.python.org/dev/peps/pep-0004
(3) https://www.python.org/dev/peps/pep-0011
(4) https://www.python.org/dev/peps/pep-3108
(5) https://www.python.org/dev/peps/pep-0008
(6) https://www.python.org/dev/peps/pep-3108
File: python.info, Node: PEP 3101 A New Approach To String Formatting, Next: Changes To Exceptions, Prev: Library Changes, Up: What’s New In Python 3 0
1.9.5 `PEP 3101': A New Approach To String Formatting
-----------------------------------------------------
* A new system for built-in string formatting operations replaces the
‘%’ string formatting operator. (However, the ‘%’ operator is
still supported; it will be deprecated in Python 3.1 and removed
from the language at some later time.) Read PEP 3101(1) for the
full scoop.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3101
File: python.info, Node: Changes To Exceptions, Next: Miscellaneous Other Changes, Prev: PEP 3101 A New Approach To String Formatting, Up: What’s New In Python 3 0
1.9.6 Changes To Exceptions
---------------------------
The APIs for raising and catching exception have been cleaned up and new
powerful features added:
* PEP 352(1): All exceptions must be derived (directly or indirectly)
from *note BaseException: 1a8. This is the root of the exception
hierarchy. This is not new as a recommendation, but the
`requirement' to inherit from *note BaseException: 1a8. is new.
(Python 2.6 still allowed classic classes to be raised, and placed
no restriction on what you can catch.) As a consequence, string
exceptions are finally truly and utterly dead.
* Almost all exceptions should actually derive from *note Exception:
1a9.; *note BaseException: 1a8. should only be used as a base class
for exceptions that should only be handled at the top level, such
as *note SystemExit: 5fc. or *note KeyboardInterrupt: 197. The
recommended idiom for handling all exceptions except for this
latter category is to use *note except: b3e. *note Exception: 1a9.
* ‘StandardError’ was removed.
* Exceptions no longer behave as sequences. Use the ‘args’ attribute
instead.
* PEP 3109(2): Raising exceptions. You must now use ‘raise
`Exception'(`args')’ instead of ‘raise `Exception', `args'’.
Additionally, you can no longer explicitly specify a traceback;
instead, if you `have' to do this, you can assign directly to the
‘__traceback__’ attribute (see below).
* PEP 3110(3): Catching exceptions. You must now use ‘except
`SomeException' as `variable'’ instead of ‘except `SomeException',
`variable'’. Moreover, the `variable' is explicitly deleted when
the *note except: b3e. block is left.
* PEP 3134(4): Exception chaining. There are two cases: implicit
chaining and explicit chaining. Implicit chaining happens when an
exception is raised in an *note except: b3e. or *note finally: 182.
handler block. This usually happens due to a bug in the handler
block; we call this a `secondary' exception. In this case, the
original exception (that was being handled) is saved as the
‘__context__’ attribute of the secondary exception. Explicit
chaining is invoked with this syntax:
raise SecondaryException() from primary_exception
(where `primary_exception' is any expression that produces an
exception object, probably an exception that was previously
caught). In this case, the primary exception is stored on the
‘__cause__’ attribute of the secondary exception. The traceback
printed when an unhandled exception occurs walks the chain of
‘__cause__’ and ‘__context__’ attributes and prints a separate
traceback for each component of the chain, with the primary
exception at the top. (Java users may recognize this behavior.)
* PEP 3134(5): Exception objects now store their traceback as the
‘__traceback__’ attribute. This means that an exception object now
contains all the information pertaining to an exception, and there
are fewer reasons to use *note sys.exc_info(): c5f. (though the
latter is not removed).
* A few exception messages are improved when Windows fails to load an
extension module. For example, ‘error code 193’ is now ‘%1 is not
a valid Win32 application’. Strings now deal with non-English
locales.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0352
(2) https://www.python.org/dev/peps/pep-3109
(3) https://www.python.org/dev/peps/pep-3110
(4) https://www.python.org/dev/peps/pep-3134
(5) https://www.python.org/dev/peps/pep-3134
File: python.info, Node: Miscellaneous Other Changes, Next: Build and C API Changes<7>, Prev: Changes To Exceptions, Up: What’s New In Python 3 0
1.9.7 Miscellaneous Other Changes
---------------------------------
* Menu:
* Operators And Special Methods::
* Builtins::
File: python.info, Node: Operators And Special Methods, Next: Builtins, Up: Miscellaneous Other Changes
1.9.7.1 Operators And Special Methods
.....................................
* ‘!=’ now returns the opposite of ‘==’, unless ‘==’ returns *note
NotImplemented: 84c.
* The concept of “unbound methods” has been removed from the
language. When referencing a method as a class attribute, you now
get a plain function object.
* ‘__getslice__()’, ‘__setslice__()’ and ‘__delslice__()’ were
killed. The syntax ‘a[i:j]’ now translates to
‘a.__getitem__(slice(i, j))’ (or *note __setitem__(): c62. or *note
__delitem__(): c63, when used as an assignment or deletion target,
respectively).
* PEP 3114(1): the standard *note next(): 682. method has been
renamed to *note __next__(): c64.
* The ‘__oct__()’ and ‘__hex__()’ special methods are removed – *note
oct(): c65. and *note hex(): c66. use *note __index__(): 18a. now
to convert the argument to an integer.
* Removed support for ‘__members__’ and ‘__methods__’.
* The function attributes named ‘func_X’ have been renamed to use the
‘__X__’ form, freeing up these names in the function attribute
namespace for user-defined attributes. To wit, ‘func_closure’,
‘func_code’, ‘func_defaults’, ‘func_dict’, ‘func_doc’,
‘func_globals’, ‘func_name’ were renamed to ‘__closure__’,
‘__code__’, ‘__defaults__’, *note __dict__: 4fc, ‘__doc__’,
‘__globals__’, *note __name__: c67, respectively.
* ‘__nonzero__()’ is now *note __bool__(): c68.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3114
File: python.info, Node: Builtins, Prev: Operators And Special Methods, Up: Miscellaneous Other Changes
1.9.7.2 Builtins
................
* PEP 3135(1): New *note super(): 4e2. You can now invoke *note
super(): 4e2. without arguments and (assuming this is in a regular
instance method defined inside a *note class: c6a. statement) the
right class and instance will automatically be chosen. With
arguments, the behavior of *note super(): 4e2. is unchanged.
* PEP 3111(2): ‘raw_input()’ was renamed to *note input(): c6b. That
is, the new *note input(): c6b. function reads a line from *note
sys.stdin: 30d. and returns it with the trailing newline stripped.
It raises *note EOFError: c6c. if the input is terminated
prematurely. To get the old behavior of *note input(): c6b, use
‘eval(input())’.
* A new built-in function *note next(): 682. was added to call the
*note __next__(): c64. method on an object.
* The *note round(): c6d. function rounding strategy and return type
have changed. Exact halfway cases are now rounded to the nearest
even result instead of away from zero. (For example, ‘round(2.5)’
now returns ‘2’ rather than ‘3’.) ‘round(x[, n])’ now delegates to
‘x.__round__([n])’ instead of always returning a float. It
generally returns an integer when called with a single argument and
a value of the same type as ‘x’ when called with two arguments.
* Moved ‘intern()’ to *note sys.intern(): c6e.
* Removed: ‘apply()’. Instead of ‘apply(f, args)’ use ‘f(*args)’.
* Removed *note callable(): b44. Instead of ‘callable(f)’ you can
use ‘isinstance(f, collections.Callable)’. The
‘operator.isCallable()’ function is also gone.
* Removed ‘coerce()’. This function no longer serves a purpose now
that classic classes are gone.
* Removed ‘execfile()’. Instead of ‘execfile(fn)’ use
‘exec(open(fn).read())’.
* Removed the ‘file’ type. Use *note open(): 4f0. There are now
several different kinds of streams that open can return in the
*note io: a1. module.
* Removed ‘reduce()’. Use *note functools.reduce(): c6f. if you
really need it; however, 99 percent of the time an explicit *note
for: c30. loop is more readable.
* Removed ‘reload()’. Use *note imp.reload(): c70.
* Removed. ‘dict.has_key()’ – use the *note in: 7a3. operator
instead.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3135
(2) https://www.python.org/dev/peps/pep-3111
File: python.info, Node: Build and C API Changes<7>, Next: Performance, Prev: Miscellaneous Other Changes, Up: What’s New In Python 3 0
1.9.8 Build and C API Changes
-----------------------------
Due to time constraints, here is a `very' incomplete list of changes to
the C API.
* Support for several platforms was dropped, including but not
limited to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.
* PEP 3118(1): New Buffer API.
* PEP 3121(2): Extension Module Initialization & Finalization.
* PEP 3123(3): Making *note PyObject_HEAD: c72. conform to standard
C.
* No more C API support for restricted execution.
* ‘PyNumber_Coerce()’, ‘PyNumber_CoerceEx()’, ‘PyMember_Get()’, and
‘PyMember_Set()’ C APIs are removed.
* New C API *note PyImport_ImportModuleNoBlock(): 9c1, works like
*note PyImport_ImportModule(): c73. but won’t block on the import
lock (returning an error instead).
* Renamed the boolean conversion C-level slot and method:
‘nb_nonzero’ is now ‘nb_bool’.
* Removed ‘METH_OLDARGS’ and ‘WITH_CYCLE_GC’ from the C API.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3118
(2) https://www.python.org/dev/peps/pep-3121
(3) https://www.python.org/dev/peps/pep-3123
File: python.info, Node: Performance, Next: Porting To Python 3 0, Prev: Build and C API Changes<7>, Up: What’s New In Python 3 0
1.9.9 Performance
-----------------
The net result of the 3.0 generalizations is that Python 3.0 runs the
pystone benchmark around 10% slower than Python 2.5. Most likely the
biggest cause is the removal of special-casing for small integers.
There’s room for improvement, but it will happen after 3.0 is released!
File: python.info, Node: Porting To Python 3 0, Prev: Performance, Up: What’s New In Python 3 0
1.9.10 Porting To Python 3.0
----------------------------
For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
best strategy is the following:
0. (Prerequisite:) Start with excellent test coverage.
1. Port to Python 2.6. This should be no more work than the average
port from Python 2.x to Python 2.(x+1). Make sure all your tests
pass.
2. (Still using 2.6:) Turn on the ‘-3’ command line switch. This
enables warnings about features that will be removed (or change) in
3.0. Run your test suite again, and fix code that you get warnings
about until there are no warnings left, and all your tests still
pass.
3. Run the ‘2to3’ source-to-source translator over your source code
tree. (See *note 2to3 - Automated Python 2 to 3 code translation:
c76. for more on this tool.) Run the result of the translation
under Python 3.0. Manually fix up any remaining issues, fixing
problems until all tests pass again.
It is not recommended to try to write source code that runs unchanged
under both Python 2.6 and 3.0; you’d have to use a very contorted coding
style, e.g. avoiding ‘print’ statements, metaclasses, and much more.
If you are maintaining a library that needs to support both Python 2.6
and Python 3.0, the best approach is to modify step 3 above by editing
the 2.6 version of the source code and running the ‘2to3’ translator
again, rather than editing the 3.0 version of the source code.
For porting C extensions to Python 3.0, please see *note Porting
Extension Modules to Python 3: c77.
File: python.info, Node: What’s New in Python 2 7, Next: What’s New in Python 2 6, Prev: What’s New In Python 3 0, Up: What’s New in Python
1.10 What’s New in Python 2.7
=============================
Author: A.M. Kuchling (amk at amk.ca)
This article explains the new features in Python 2.7. Python 2.7 was
released on July 3, 2010.
Numeric handling has been improved in many ways, for both floating-point
numbers and for the *note Decimal: 188. class. There are some useful
additions to the standard library, such as a greatly enhanced *note
unittest: 11b. module, the *note argparse: 6. module for parsing
command-line options, convenient *note OrderedDict: 1b9. and *note
Counter: 9da. classes in the *note collections: 1e. module, and many
other improvements.
Python 2.7 is planned to be the last of the 2.x releases, so we worked
on making it a good release for the long term. To help with porting to
Python 3, several new features from the Python 3.x series have been
included in 2.7.
This article doesn’t attempt to provide a complete specification of the
new features, but instead provides a convenient overview. For full
details, you should refer to the documentation for Python 2.7 at
‘https://docs.python.org’. If you want to understand the rationale for
the design and implementation, refer to the PEP for a particular new
feature or the issue on ‘https://bugs.python.org’ in which a change was
discussed. Whenever possible, “What’s New in Python” links to the
bug/patch item for each change.
* Menu:
* The Future for Python 2.x: The Future for Python 2 x.
* Changes to the Handling of Deprecation Warnings::
* Python 3.1 Features: Python 3 1 Features.
* PEP 372; Adding an Ordered Dictionary to collections: PEP 372 Adding an Ordered Dictionary to collections.
* PEP 378; Format Specifier for Thousands Separator: PEP 378 Format Specifier for Thousands Separator<2>.
* PEP 389; The argparse Module for Parsing Command Lines: PEP 389 The argparse Module for Parsing Command Lines.
* PEP 391; Dictionary-Based Configuration For Logging: PEP 391 Dictionary-Based Configuration For Logging.
* PEP 3106; Dictionary Views: PEP 3106 Dictionary Views.
* PEP 3137; The memoryview Object: PEP 3137 The memoryview Object.
* Other Language Changes: Other Language Changes<9>.
* New and Improved Modules::
* Build and C API Changes: Build and C API Changes<8>.
* Other Changes and Fixes::
* Porting to Python 2.7: Porting to Python 2 7.
* New Features Added to Python 2.7 Maintenance Releases: New Features Added to Python 2 7 Maintenance Releases.
* Acknowledgements::
File: python.info, Node: The Future for Python 2 x, Next: Changes to the Handling of Deprecation Warnings, Up: What’s New in Python 2 7
1.10.1 The Future for Python 2.x
--------------------------------
Python 2.7 is the last major release in the 2.x series, as the Python
maintainers have shifted the focus of their new feature development
efforts to the Python 3.x series. This means that while Python 2
continues to receive bug fixes, and to be updated to build correctly on
new hardware and versions of supported operated systems, there will be
no new full feature releases for the language or standard library.
However, while there is a large common subset between Python 2.7 and
Python 3, and many of the changes involved in migrating to that common
subset, or directly to Python 3, can be safely automated, some other
changes (notably those associated with Unicode handling) may require
careful consideration, and preferably robust automated regression test
suites, to migrate effectively.
This means that Python 2.7 will remain in place for a long time,
providing a stable and supported base platform for production systems
that have not yet been ported to Python 3. The full expected lifecycle
of the Python 2.7 series is detailed in PEP 373(1).
Some key consequences of the long-term significance of 2.7 are:
* As noted above, the 2.7 release has a much longer period of
maintenance when compared to earlier 2.x versions. Python 2.7 is
currently expected to remain supported by the core development team
(receiving security updates and other bug fixes) until at least
2020 (10 years after its initial release, compared to the more
typical support period of 18–24 months).
* As the Python 2.7 standard library ages, making effective use of
the Python Package Index (either directly or via a redistributor)
becomes more important for Python 2 users. In addition to a wide
variety of third party packages for various tasks, the available
packages include backports of new modules and features from the
Python 3 standard library that are compatible with Python 2, as
well as various tools and libraries that can make it easier to
migrate to Python 3. The Python Packaging User Guide(2) provides
guidance on downloading and installing software from the Python
Package Index.
* While the preferred approach to enhancing Python 2 is now the
publication of new packages on the Python Package Index, this
approach doesn’t necessarily work in all cases, especially those
related to network security. In exceptional cases that cannot be
handled adequately by publishing new or updated packages on PyPI,
the Python Enhancement Proposal process may be used to make the
case for adding new features directly to the Python 2 standard
library. Any such additions, and the maintenance releases where
they were added, will be noted in the *note New Features Added to
Python 2.7 Maintenance Releases: c7c. section below.
For projects wishing to migrate from Python 2 to Python 3, or for
library and framework developers wishing to support users on both Python
2 and Python 3, there are a variety of tools and guides available to
help decide on a suitable approach and manage some of the technical
details involved. The recommended starting point is the *note Porting
Python 2 Code to Python 3: c7d. HOWTO guide.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0373
(2) https://packaging.python.org
File: python.info, Node: Changes to the Handling of Deprecation Warnings, Next: Python 3 1 Features, Prev: The Future for Python 2 x, Up: What’s New in Python 2 7
1.10.2 Changes to the Handling of Deprecation Warnings
------------------------------------------------------
For Python 2.7, a policy decision was made to silence warnings only of
interest to developers by default. *note DeprecationWarning: 278. and
its descendants are now ignored unless otherwise requested, preventing
users from seeing warnings triggered by an application. This change was
also made in the branch that became Python 3.2. (Discussed on
stdlib-sig and carried out in bpo-7319(1).)
In previous releases, *note DeprecationWarning: 278. messages were
enabled by default, providing Python developers with a clear indication
of where their code may break in a future major version of Python.
However, there are increasingly many users of Python-based applications
who are not directly involved in the development of those applications.
*note DeprecationWarning: 278. messages are irrelevant to such users,
making them worry about an application that’s actually working correctly
and burdening application developers with responding to these concerns.
You can re-enable display of *note DeprecationWarning: 278. messages by
running Python with the *note -Wdefault: 417. (short form: *note -Wd:
417.) switch, or by setting the *note PYTHONWARNINGS: 418. environment
variable to ‘"default"’ (or ‘"d"’) before running Python. Python code
can also re-enable them by calling ‘warnings.simplefilter('default')’.
The ‘unittest’ module also automatically reenables deprecation warnings
when running tests.
---------- Footnotes ----------
(1) https://bugs.python.org/issue7319
File: python.info, Node: Python 3 1 Features, Next: PEP 372 Adding an Ordered Dictionary to collections, Prev: Changes to the Handling of Deprecation Warnings, Up: What’s New in Python 2 7
1.10.3 Python 3.1 Features
--------------------------
Much as Python 2.6 incorporated features from Python 3.0, version 2.7
incorporates some of the new features in Python 3.1. The 2.x series
continues to provide tools for migrating to the 3.x series.
A partial list of 3.1 features that were backported to 2.7:
* The syntax for set literals (‘{1,2,3}’ is a mutable set).
* Dictionary and set comprehensions (‘{i: i*2 for i in range(3)}’).
* Multiple context managers in a single *note with: 6e9. statement.
* A new version of the *note io: a1. library, rewritten in C for
performance.
* The ordered-dictionary type described in *note PEP 372; Adding an
Ordered Dictionary to collections: c80.
* The new ‘","’ format specifier described in *note PEP 378; Format
Specifier for Thousands Separator: c81.
* The *note memoryview: 25c. object.
* A small subset of the *note importlib: 9b. module, *note described
below: c82.
* The *note repr(): 7cc. of a float ‘x’ is shorter in many cases:
it’s now based on the shortest decimal string that’s guaranteed to
round back to ‘x’. As in previous versions of Python, it’s
guaranteed that ‘float(repr(x))’ recovers ‘x’.
* Float-to-string and string-to-float conversions are correctly
rounded. The *note round(): c6d. function is also now correctly
rounded.
* The *note PyCapsule: c07. type, used to provide a C API for
extension modules.
* The *note PyLong_AsLongAndOverflow(): bfd. C API function.
Other new Python3-mode warnings include:
* ‘operator.isCallable()’ and ‘operator.sequenceIncludes()’, which
are not supported in 3.x, now trigger warnings.
* The ‘-3’ switch now automatically enables the ‘-Qwarn’ switch that
causes warnings about using classic division with integers and long
integers.
File: python.info, Node: PEP 372 Adding an Ordered Dictionary to collections, Next: PEP 378 Format Specifier for Thousands Separator<2>, Prev: Python 3 1 Features, Up: What’s New in Python 2 7
1.10.4 PEP 372: Adding an Ordered Dictionary to collections
-----------------------------------------------------------
Regular Python dictionaries iterate over key/value pairs in arbitrary
order. Over the years, a number of authors have written alternative
implementations that remember the order that the keys were originally
inserted. Based on the experiences from those implementations, 2.7
introduces a new *note OrderedDict: 1b9. class in the *note collections:
1e. module.
The *note OrderedDict: 1b9. API provides the same interface as regular
dictionaries but iterates over keys and values in a guaranteed order
depending on when a key was first inserted:
>>> from collections import OrderedDict
>>> d = OrderedDict([('first', 1),
... ('second', 2),
... ('third', 3)])
>>> d.items()
[('first', 1), ('second', 2), ('third', 3)]
If a new entry overwrites an existing entry, the original insertion
position is left unchanged:
>>> d['second'] = 4
>>> d.items()
[('first', 1), ('second', 4), ('third', 3)]
Deleting an entry and reinserting it will move it to the end:
>>> del d['second']
>>> d['second'] = 5
>>> d.items()
[('first', 1), ('third', 3), ('second', 5)]
The *note popitem(): c84. method has an optional `last' argument that
defaults to ‘True’. If `last' is true, the most recently added key is
returned and removed; if it’s false, the oldest key is selected:
>>> od = OrderedDict([(x,0) for x in range(20)])
>>> od.popitem()
(19, 0)
>>> od.popitem()
(18, 0)
>>> od.popitem(last=False)
(0, 0)
>>> od.popitem(last=False)
(1, 0)
Comparing two ordered dictionaries checks both the keys and values, and
requires that the insertion order was the same:
>>> od1 = OrderedDict([('first', 1),
... ('second', 2),
... ('third', 3)])
>>> od2 = OrderedDict([('third', 3),
... ('first', 1),
... ('second', 2)])
>>> od1 == od2
False
>>> # Move 'third' key to the end
>>> del od2['third']; od2['third'] = 3
>>> od1 == od2
True
Comparing an *note OrderedDict: 1b9. with a regular dictionary ignores
the insertion order and just compares the keys and values.
How does the *note OrderedDict: 1b9. work? It maintains a doubly-linked
list of keys, appending new keys to the list as they’re inserted. A
secondary dictionary maps keys to their corresponding list node, so
deletion doesn’t have to traverse the entire linked list and therefore
remains O(1).
The standard library now supports use of ordered dictionaries in several
modules.
* The ‘ConfigParser’ module uses them by default, meaning that
configuration files can now be read, modified, and then written
back in their original order.
* The *note _asdict(): 1b6. method for *note
collections.namedtuple(): 1b7. now returns an ordered dictionary
with the values appearing in the same order as the underlying tuple
indices.
* The *note json: a4. module’s *note JSONDecoder: 607. class
constructor was extended with an `object_pairs_hook' parameter to
allow ‘OrderedDict’ instances to be built by the decoder. Support
was also added for third-party tools like PyYAML(1).
See also
........
PEP 372(2) - Adding an ordered dictionary to collections
PEP written by Armin Ronacher and Raymond Hettinger; implemented by
Raymond Hettinger.
---------- Footnotes ----------
(1) http://pyyaml.org/
(2) https://www.python.org/dev/peps/pep-0372
File: python.info, Node: PEP 378 Format Specifier for Thousands Separator<2>, Next: PEP 389 The argparse Module for Parsing Command Lines, Prev: PEP 372 Adding an Ordered Dictionary to collections, Up: What’s New in Python 2 7
1.10.5 PEP 378: Format Specifier for Thousands Separator
--------------------------------------------------------
To make program output more readable, it can be useful to add separators
to large numbers, rendering them as 18,446,744,073,709,551,616 instead
of 18446744073709551616.
The fully general solution for doing this is the *note locale: a9.
module, which can use different separators (“,” in North America, “.” in
Europe) and different grouping sizes, but *note locale: a9. is
complicated to use and unsuitable for multi-threaded applications where
different threads are producing output for different locales.
Therefore, a simple comma-grouping mechanism has been added to the
mini-language used by the *note str.format(): 4da. method. When
formatting a floating-point number, simply include a comma between the
width and the precision:
>>> '{:20,.2f}'.format(18446744073709551616.0)
'18,446,744,073,709,551,616.00'
When formatting an integer, include the comma after the width:
>>> '{:20,d}'.format(18446744073709551616)
'18,446,744,073,709,551,616'
This mechanism is not adaptable at all; commas are always used as the
separator and the grouping is always into three-digit groups. The
comma-formatting mechanism isn’t as general as the *note locale: a9.
module, but it’s easier to use.
See also
........
PEP 378(1) - Format Specifier for Thousands Separator
PEP written by Raymond Hettinger; implemented by Eric Smith.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0378
File: python.info, Node: PEP 389 The argparse Module for Parsing Command Lines, Next: PEP 391 Dictionary-Based Configuration For Logging, Prev: PEP 378 Format Specifier for Thousands Separator<2>, Up: What’s New in Python 2 7
1.10.6 PEP 389: The argparse Module for Parsing Command Lines
-------------------------------------------------------------
The *note argparse: 6. module for parsing command-line arguments was
added as a more powerful replacement for the *note optparse: c3. module.
This means Python now supports three different modules for parsing
command-line arguments: *note getopt: 87, *note optparse: c3, and *note
argparse: 6. The *note getopt: 87. module closely resembles the C
library’s ‘getopt()’ function, so it remains useful if you’re writing a
Python prototype that will eventually be rewritten in C. *note optparse:
c3. becomes redundant, but there are no plans to remove it because there
are many scripts still using it, and there’s no automated way to update
these scripts. (Making the *note argparse: 6. API consistent with *note
optparse: c3.’s interface was discussed but rejected as too messy and
difficult.)
In short, if you’re writing a new script and don’t need to worry about
compatibility with earlier versions of Python, use *note argparse: 6.
instead of *note optparse: c3.
Here’s an example:
import argparse
parser = argparse.ArgumentParser(description='Command-line example.')
# Add optional switches
parser.add_argument('-v', action='store_true', dest='is_verbose',
help='produce verbose output')
parser.add_argument('-o', action='store', dest='output',
metavar='FILE',
help='direct output to FILE instead of stdout')
parser.add_argument('-C', action='store', type=int, dest='context',
metavar='NUM', default=0,
help='display NUM lines of added context')
# Allow any number of additional arguments.
parser.add_argument(nargs='*', action='store', dest='inputs',
help='input filenames (default is stdin)')
args = parser.parse_args()
print args.__dict__
Unless you override it, ‘-h’ and ‘--help’ switches are automatically
added, and produce neatly formatted output:
-> ./python.exe argparse-example.py --help
usage: argparse-example.py [-h] [-v] [-o FILE] [-C NUM] [inputs [inputs ...]]
Command-line example.
positional arguments:
inputs input filenames (default is stdin)
optional arguments:
-h, --help show this help message and exit
-v produce verbose output
-o FILE direct output to FILE instead of stdout
-C NUM display NUM lines of added context
As with *note optparse: c3, the command-line switches and arguments are
returned as an object with attributes named by the `dest' parameters:
-> ./python.exe argparse-example.py -v
{'output': None,
'is_verbose': True,
'context': 0,
'inputs': []}
-> ./python.exe argparse-example.py -v -o /tmp/output -C 4 file1 file2
{'output': '/tmp/output',
'is_verbose': True,
'context': 4,
'inputs': ['file1', 'file2']}
*note argparse: 6. has much fancier validation than *note optparse: c3.;
you can specify an exact number of arguments as an integer, 0 or more
arguments by passing ‘'*'’, 1 or more by passing ‘'+'’, or an optional
argument with ‘'?'’. A top-level parser can contain sub-parsers to
define subcommands that have different sets of switches, as in ‘svn
commit’, ‘svn checkout’, etc. You can specify an argument’s type as
*note FileType: 820, which will automatically open files for you and
understands that ‘'-'’ means standard input or output.
See also
........
*note argparse: 6. documentation
The documentation page of the argparse module.
*note Upgrading optparse code: b29.
Part of the Python documentation, describing how to convert code
that uses *note optparse: c3.
PEP 389(1) - argparse - New Command Line Parsing Module
PEP written and implemented by Steven Bethard.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0389
File: python.info, Node: PEP 391 Dictionary-Based Configuration For Logging, Next: PEP 3106 Dictionary Views, Prev: PEP 389 The argparse Module for Parsing Command Lines, Up: What’s New in Python 2 7
1.10.7 PEP 391: Dictionary-Based Configuration For Logging
----------------------------------------------------------
The *note logging: aa. module is very flexible; applications can define
a tree of logging subsystems, and each logger in this tree can filter
out certain messages, format them differently, and direct messages to a
varying number of handlers.
All this flexibility can require a lot of configuration. You can write
Python statements to create objects and set their properties, but a
complex set-up requires verbose but boring code. *note logging: aa.
also supports a ‘fileConfig()’ function that parses a file, but the file
format doesn’t support configuring filters, and it’s messier to generate
programmatically.
Python 2.7 adds a ‘dictConfig()’ function that uses a dictionary to
configure logging. There are many ways to produce a dictionary from
different sources: construct one with code; parse a file containing
JSON; or use a YAML parsing library if one is installed. For more
information see *note Configuration functions: c88.
The following example configures two loggers, the root logger and a
logger named “network”. Messages sent to the root logger will be sent
to the system log using the syslog protocol, and messages to the
“network” logger will be written to a ‘network.log’ file that will be
rotated once the log reaches 1MB.
import logging
import logging.config
configdict = {
'version': 1, # Configuration schema in use; must be 1 for now
'formatters': {
'standard': {
'format': ('%(asctime)s %(name)-15s '
'%(levelname)-8s %(message)s')}},
'handlers': {'netlog': {'backupCount': 10,
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/logs/network.log',
'formatter': 'standard',
'level': 'INFO',
'maxBytes': 1000000},
'syslog': {'class': 'logging.handlers.SysLogHandler',
'formatter': 'standard',
'level': 'ERROR'}},
# Specify all the subordinate loggers
'loggers': {
'network': {
'handlers': ['netlog']
}
},
# Specify properties of the root logger
'root': {
'handlers': ['syslog']
},
}
# Set up configuration
logging.config.dictConfig(configdict)
# As an example, log two error messages
logger = logging.getLogger('/')
logger.error('Database not found')
netlogger = logging.getLogger('network')
netlogger.error('Connection failed')
Three smaller enhancements to the *note logging: aa. module, all
implemented by Vinay Sajip, are:
* The *note SysLogHandler: a1e. class now supports syslogging over
TCP. The constructor has a `socktype' parameter giving the type of
socket to use, either *note socket.SOCK_DGRAM: c89. for UDP or
*note socket.SOCK_STREAM: c8a. for TCP. The default protocol
remains UDP.
* *note Logger: 3a7. instances gained a *note getChild(): c8b. method
that retrieves a descendant logger using a relative path. For
example, once you retrieve a logger by doing ‘log =
getLogger('app')’, calling ‘log.getChild('network.listen')’ is
equivalent to ‘getLogger('app.network.listen')’.
* The *note LoggerAdapter: c8c. class gained an ‘isEnabledFor()’
method that takes a `level' and returns whether the underlying
logger would process a message of that level of importance.
See also
........
PEP 391(1) - Dictionary-Based Configuration For Logging
PEP written and implemented by Vinay Sajip.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0391
File: python.info, Node: PEP 3106 Dictionary Views, Next: PEP 3137 The memoryview Object, Prev: PEP 391 Dictionary-Based Configuration For Logging, Up: What’s New in Python 2 7
1.10.8 PEP 3106: Dictionary Views
---------------------------------
The dictionary methods *note keys(): c2a, *note values(): c2c, and *note
items(): c2b. are different in Python 3.x. They return an object called
a `view' instead of a fully materialized list.
It’s not possible to change the return values of *note keys(): c2a,
*note values(): c2c, and *note items(): c2b. in Python 2.7 because too
much code would break. Instead the 3.x versions were added under the
new names ‘viewkeys()’, ‘viewvalues()’, and ‘viewitems()’.
>>> d = dict((i*10, chr(65+i)) for i in range(26))
>>> d
{0: 'A', 130: 'N', 10: 'B', 140: 'O', 20: ..., 250: 'Z'}
>>> d.viewkeys()
dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])
Views can be iterated over, but the key and item views also behave like
sets. The ‘&’ operator performs intersection, and ‘|’ performs a union:
>>> d1 = dict((i*10, chr(65+i)) for i in range(26))
>>> d2 = dict((i**.5, i) for i in range(1000))
>>> d1.viewkeys() & d2.viewkeys()
set([0.0, 10.0, 20.0, 30.0])
>>> d1.viewkeys() | range(0, 30)
set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])
The view keeps track of the dictionary and its contents change as the
dictionary is modified:
>>> vk = d.viewkeys()
>>> vk
dict_keys([0, 130, 10, ..., 250])
>>> d[260] = '&'
>>> vk
dict_keys([0, 130, 260, 10, ..., 250])
However, note that you can’t add or remove keys while you’re iterating
over the view:
>>> for k in vk:
... d[k*2] = k
...
Traceback (most recent call last):
File "", line 1, in
RuntimeError: dictionary changed size during iteration
You can use the view methods in Python 2.x code, and the 2to3 converter
will change them to the standard *note keys(): c2a, *note values(): c2c,
and *note items(): c2b. methods.
See also
........
PEP 3106(1) - Revamping dict.keys(), .values() and .items()
PEP written by Guido van Rossum. Backported to 2.7 by Alexandre
Vassalotti; bpo-1967(2).
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3106
(2) https://bugs.python.org/issue1967
File: python.info, Node: PEP 3137 The memoryview Object, Next: Other Language Changes<9>, Prev: PEP 3106 Dictionary Views, Up: What’s New in Python 2 7
1.10.9 PEP 3137: The memoryview Object
--------------------------------------
The *note memoryview: 25c. object provides a view of another object’s
memory content that matches the *note bytes: 331. type’s interface.
>>> import string
>>> m = memoryview(string.letters)
>>> m
>>> len(m) # Returns length of underlying object
52
>>> m[0], m[25], m[26] # Indexing returns one byte
('a', 'z', 'A')
>>> m2 = m[0:26] # Slicing returns another memoryview
>>> m2
The content of the view can be converted to a string of bytes or a list
of integers:
>>> m2.tobytes()
'abcdefghijklmnopqrstuvwxyz'
>>> m2.tolist()
[97, 98, 99, 100, 101, 102, 103, ... 121, 122]
>>>
*note memoryview: 25c. objects allow modifying the underlying object if
it’s a mutable object.
>>> m2[0] = 75
Traceback (most recent call last):
File "", line 1, in
TypeError: cannot modify read-only memory
>>> b = bytearray(string.letters) # Creating a mutable object
>>> b
bytearray(b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
>>> mb = memoryview(b)
>>> mb[0] = '*' # Assign to view, changing the bytearray.
>>> b[0:5] # The bytearray has been changed.
bytearray(b'*bcde')
>>>
See also
........
PEP 3137(1) - Immutable Bytes and Mutable Buffer
PEP written by Guido van Rossum. Implemented by Travis Oliphant,
Antoine Pitrou and others. Backported to 2.7 by Antoine Pitrou;
bpo-2396(2).
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3137
(2) https://bugs.python.org/issue2396
File: python.info, Node: Other Language Changes<9>, Next: New and Improved Modules, Prev: PEP 3137 The memoryview Object, Up: What’s New in Python 2 7
1.10.10 Other Language Changes
------------------------------
Some smaller changes made to the core Python language are:
* The syntax for set literals has been backported from Python 3.x.
Curly brackets are used to surround the contents of the resulting
mutable set; set literals are distinguished from dictionaries by
not containing colons and values. ‘{}’ continues to represent an
empty dictionary; use ‘set()’ for an empty set.
>>> {1, 2, 3, 4, 5}
set([1, 2, 3, 4, 5])
>>> set() # empty set
set([])
>>> {} # empty dict
{}
Backported by Alexandre Vassalotti; bpo-2335(1).
* Dictionary and set comprehensions are another feature backported
from 3.x, generalizing list/generator comprehensions to use the
literal syntax for sets and dictionaries.
>>> {x: x*x for x in range(6)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
>>> {('a'*x) for x in range(6)}
set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
Backported by Alexandre Vassalotti; bpo-2333(2).
* The *note with: 6e9. statement can now use multiple context
managers in one statement. Context managers are processed from
left to right and each one is treated as beginning a new ‘with’
statement. This means that:
with A() as a, B() as b:
... suite of statements ...
is equivalent to:
with A() as a:
with B() as b:
... suite of statements ...
The ‘contextlib.nested()’ function provides a very similar
function, so it’s no longer necessary and has been deprecated.
(Proposed in ‘https://codereview.appspot.com/53094’; implemented by
Georg Brandl.)
* Conversions between floating-point numbers and strings are now
correctly rounded on most platforms. These conversions occur in
many different places: *note str(): 330. on floats and complex
numbers; the *note float: 187. and *note complex: 189.
constructors; numeric formatting; serializing and deserializing
floats and complex numbers using the *note marshal: b0, *note
pickle: ca. and *note json: a4. modules; parsing of float and
imaginary literals in Python code; and *note Decimal: 188.-to-float
conversion.
Related to this, the *note repr(): 7cc. of a floating-point number
`x' now returns a result based on the shortest decimal string
that’s guaranteed to round back to `x' under correct rounding (with
round-half-to-even rounding mode). Previously it gave a string
based on rounding x to 17 decimal digits.
The rounding library responsible for this improvement works on
Windows and on Unix platforms using the gcc, icc, or suncc
compilers. There may be a small number of platforms where correct
operation of this code cannot be guaranteed, so the code is not
used on such systems. You can find out which code is being used by
checking *note sys.float_repr_style: c90, which will be ‘short’ if
the new code is in use and ‘legacy’ if it isn’t.
Implemented by Eric Smith and Mark Dickinson, using David Gay’s
‘dtoa.c’ library; bpo-7117(3).
* Conversions from long integers and regular integers to floating
point now round differently, returning the floating-point number
closest to the number. This doesn’t matter for small integers that
can be converted exactly, but for large numbers that will
unavoidably lose precision, Python 2.7 now approximates more
closely. For example, Python 2.6 computed the following:
>>> n = 295147905179352891391
>>> float(n)
2.9514790517935283e+20
>>> n - long(float(n))
65535L
Python 2.7’s floating-point result is larger, but much closer to
the true value:
>>> n = 295147905179352891391
>>> float(n)
2.9514790517935289e+20
>>> n - long(float(n))
-1L
(Implemented by Mark Dickinson; bpo-3166(4).)
Integer division is also more accurate in its rounding behaviours.
(Also implemented by Mark Dickinson; bpo-1811(5).)
* Implicit coercion for complex numbers has been removed; the
interpreter will no longer ever attempt to call a ‘__coerce__()’
method on complex objects. (Removed by Meador Inge and Mark
Dickinson; bpo-5211(6).)
* The *note str.format(): 4da. method now supports automatic
numbering of the replacement fields. This makes using *note
str.format(): 4da. more closely resemble using ‘%s’ formatting:
>>> '{}:{}:{}'.format(2009, 04, 'Sunday')
'2009:4:Sunday'
>>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
'2009:4:Sunday'
The auto-numbering takes the fields from left to right, so the
first ‘{...}’ specifier will use the first argument to *note
str.format(): 4da, the next specifier will use the next argument,
and so on. You can’t mix auto-numbering and explicit numbering –
either number all of your specifier fields or none of them – but
you can mix auto-numbering and named fields, as in the second
example above. (Contributed by Eric Smith; bpo-5237(7).)
Complex numbers now correctly support usage with *note format():
4db, and default to being right-aligned. Specifying a precision or
comma-separation applies to both the real and imaginary parts of
the number, but a specified field width and alignment is applied to
the whole of the resulting ‘1.5+3j’ output. (Contributed by Eric
Smith; bpo-1588(8) and bpo-7988(9).)
The ‘F’ format code now always formats its output using uppercase
characters, so it will now produce ‘INF’ and ‘NAN’. (Contributed
by Eric Smith; bpo-3382(10).)
A low-level change: the *note object.__format__(): 94e. method now
triggers a *note PendingDeprecationWarning: 279. if it’s passed a
format string, because the *note __format__(): 94e. method for
*note object: 2b0. converts the object to a string representation
and formats that. Previously the method silently applied the
format string to the string representation, but that could hide
mistakes in Python code. If you’re supplying formatting
information such as an alignment or precision, presumably you’re
expecting the formatting to be applied in some object-specific way.
(Fixed by Eric Smith; bpo-7994(11).)
* The *note int(): 184. and ‘long()’ types gained a ‘bit_length’
method that returns the number of bits necessary to represent its
argument in binary:
>>> n = 37
>>> bin(n)
'0b100101'
>>> n.bit_length()
6
>>> n = 2**123-1
>>> n.bit_length()
123
>>> (n+1).bit_length()
124
(Contributed by Fredrik Johansson and Victor Stinner;
bpo-3439(12).)
* The *note import: c1d. statement will no longer try an absolute
import if a relative import (e.g. ‘from .os import sep’) fails.
This fixes a bug, but could possibly break certain ‘import’
statements that were only working by accident. (Fixed by Meador
Inge; bpo-7902(13).)
* It’s now possible for a subclass of the built-in ‘unicode’ type to
override the ‘__unicode__()’ method. (Implemented by Victor
Stinner; bpo-1583863(14).)
* The *note bytearray: 332. type’s *note translate(): c91. method now
accepts ‘None’ as its first argument. (Fixed by Georg Brandl;
bpo-4759(15).)
* When using ‘@classmethod’ and ‘@staticmethod’ to wrap methods as
class or static methods, the wrapper object now exposes the wrapped
function as their ‘__func__’ attribute. (Contributed by Amaury
Forgeot d’Arc, after a suggestion by George Sakkis; bpo-5982(16).)
* When a restricted set of attributes were set using ‘__slots__’,
deleting an unset attribute would not raise *note AttributeError:
39b. as you would expect. Fixed by Benjamin Peterson;
bpo-7604(17).)
* Two new encodings are now supported: “cp720”, used primarily for
Arabic text; and “cp858”, a variant of CP 850 that adds the euro
symbol. (CP720 contributed by Alexander Belchenko and Amaury
Forgeot d’Arc in bpo-1616979(18); CP858 contributed by Tim Hatch in
bpo-8016(19).)
* The ‘file’ object will now set the ‘filename’ attribute on the
*note IOError: 992. exception when trying to open a directory on
POSIX platforms (noted by Jan Kaliszewski; bpo-4764(20)), and now
explicitly checks for and forbids writing to read-only file objects
instead of trusting the C library to catch and report the error
(fixed by Stefan Krah; bpo-5677(21)).
* The Python tokenizer now translates line endings itself, so the
*note compile(): 1b4. built-in function now accepts code using any
line-ending convention. Additionally, it no longer requires that
the code end in a newline.
* Extra parentheses in function definitions are illegal in Python
3.x, meaning that you get a syntax error from ‘def f((x)): pass’.
In Python3-warning mode, Python 2.7 will now warn about this odd
usage. (Noted by James Lingard; bpo-7362(22).)
* It’s now possible to create weak references to old-style class
objects. New-style classes were always weak-referenceable. (Fixed
by Antoine Pitrou; bpo-8268(23).)
* When a module object is garbage-collected, the module’s dictionary
is now only cleared if no one else is holding a reference to the
dictionary (bpo-7140(24)).
* Menu:
* Interpreter Changes::
* Optimizations: Optimizations<8>.
---------- Footnotes ----------
(1) https://bugs.python.org/issue2335
(2) https://bugs.python.org/issue2333
(3) https://bugs.python.org/issue7117
(4) https://bugs.python.org/issue3166
(5) https://bugs.python.org/issue1811
(6) https://bugs.python.org/issue5211
(7) https://bugs.python.org/issue5237
(8) https://bugs.python.org/issue1588
(9) https://bugs.python.org/issue7988
(10) https://bugs.python.org/issue3382
(11) https://bugs.python.org/issue7994
(12) https://bugs.python.org/issue3439
(13) https://bugs.python.org/issue7902
(14) https://bugs.python.org/issue1583863
(15) https://bugs.python.org/issue4759
(16) https://bugs.python.org/issue5982
(17) https://bugs.python.org/issue7604
(18) https://bugs.python.org/issue1616979
(19) https://bugs.python.org/issue8016
(20) https://bugs.python.org/issue4764
(21) https://bugs.python.org/issue5677
(22) https://bugs.python.org/issue7362
(23) https://bugs.python.org/issue8268
(24) https://bugs.python.org/issue7140
File: python.info, Node: Interpreter Changes, Next: Optimizations<8>, Up: Other Language Changes<9>
1.10.10.1 Interpreter Changes
.............................
A new environment variable, *note PYTHONWARNINGS: 418, allows
controlling warnings. It should be set to a string containing warning
settings, equivalent to those used with the *note -W: 417. switch,
separated by commas. (Contributed by Brian Curtin; bpo-7301(1).)
For example, the following setting will print warnings every time they
occur, but turn warnings from the ‘Cookie’ module into an error. (The
exact syntax for setting an environment variable varies across operating
systems and shells.)
export PYTHONWARNINGS=all,error:::Cookie:0
---------- Footnotes ----------
(1) https://bugs.python.org/issue7301
File: python.info, Node: Optimizations<8>, Prev: Interpreter Changes, Up: Other Language Changes<9>
1.10.10.2 Optimizations
.......................
Several performance enhancements have been added:
* A new opcode was added to perform the initial setup for *note with:
6e9. statements, looking up the *note __enter__(): c95. and *note
__exit__(): c96. methods. (Contributed by Benjamin Peterson.)
* The garbage collector now performs better for one common usage
pattern: when many objects are being allocated without deallocating
any of them. This would previously take quadratic time for garbage
collection, but now the number of full garbage collections is
reduced as the number of objects on the heap grows. The new logic
only performs a full garbage collection pass when the middle
generation has been collected 10 times and when the number of
survivor objects from the middle generation exceeds 10% of the
number of objects in the oldest generation. (Suggested by Martin
von Löwis and implemented by Antoine Pitrou; bpo-4074(1).)
* The garbage collector tries to avoid tracking simple containers
which can’t be part of a cycle. In Python 2.7, this is now true
for tuples and dicts containing atomic types (such as ints,
strings, etc.). Transitively, a dict containing tuples of atomic
types won’t be tracked either. This helps reduce the cost of each
garbage collection by decreasing the number of objects to be
considered and traversed by the collector. (Contributed by Antoine
Pitrou; bpo-4688(2).)
* Long integers are now stored internally either in base 2**15 or in
base 2**30, the base being determined at build time. Previously,
they were always stored in base 2**15. Using base 2**30 gives
significant performance improvements on 64-bit machines, but
benchmark results on 32-bit machines have been mixed. Therefore,
the default is to use base 2**30 on 64-bit machines and base 2**15
on 32-bit machines; on Unix, there’s a new configure option
‘--enable-big-digits’ that can be used to override this default.
Apart from the performance improvements this change should be
invisible to end users, with one exception: for testing and
debugging purposes there’s a new structseq ‘sys.long_info’ that
provides information about the internal format, giving the number
of bits per digit and the size in bytes of the C type used to store
each digit:
>>> import sys
>>> sys.long_info
sys.long_info(bits_per_digit=30, sizeof_digit=4)
(Contributed by Mark Dickinson; bpo-4258(3).)
Another set of changes made long objects a few bytes smaller: 2
bytes smaller on 32-bit systems and 6 bytes on 64-bit.
(Contributed by Mark Dickinson; bpo-5260(4).)
* The division algorithm for long integers has been made faster by
tightening the inner loop, doing shifts instead of multiplications,
and fixing an unnecessary extra iteration. Various benchmarks show
speedups of between 50% and 150% for long integer divisions and
modulo operations. (Contributed by Mark Dickinson; bpo-5512(5).)
Bitwise operations are also significantly faster (initial patch by
Gregory Smith; bpo-1087418(6)).
* The implementation of ‘%’ checks for the left-side operand being a
Python string and special-cases it; this results in a 1–3%
performance increase for applications that frequently use ‘%’ with
strings, such as templating libraries. (Implemented by Collin
Winter; bpo-5176(7).)
* List comprehensions with an ‘if’ condition are compiled into faster
bytecode. (Patch by Antoine Pitrou, back-ported to 2.7 by Jeffrey
Yasskin; bpo-4715(8).)
* Converting an integer or long integer to a decimal string was made
faster by special-casing base 10 instead of using a generalized
conversion function that supports arbitrary bases. (Patch by
Gawain Bolton; bpo-6713(9).)
* The ‘split()’, ‘replace()’, ‘rindex()’, ‘rpartition()’, and
‘rsplit()’ methods of string-like types (strings, Unicode strings,
and *note bytearray: 332. objects) now use a fast reverse-search
algorithm instead of a character-by-character scan. This is
sometimes faster by a factor of 10. (Added by Florent Xicluna;
bpo-7462(10) and bpo-7622(11).)
* The *note pickle: ca. and ‘cPickle’ modules now automatically
intern the strings used for attribute names, reducing memory usage
of the objects resulting from unpickling. (Contributed by Jake
McGuire; bpo-5084(12).)
* The ‘cPickle’ module now special-cases dictionaries, nearly halving
the time required to pickle them. (Contributed by Collin Winter;
bpo-5670(13).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4074
(2) https://bugs.python.org/issue4688
(3) https://bugs.python.org/issue4258
(4) https://bugs.python.org/issue5260
(5) https://bugs.python.org/issue5512
(6) https://bugs.python.org/issue1087418
(7) https://bugs.python.org/issue5176
(8) https://bugs.python.org/issue4715
(9) https://bugs.python.org/issue6713
(10) https://bugs.python.org/issue7462
(11) https://bugs.python.org/issue7622
(12) https://bugs.python.org/issue5084
(13) https://bugs.python.org/issue5670
File: python.info, Node: New and Improved Modules, Next: Build and C API Changes<8>, Prev: Other Language Changes<9>, Up: What’s New in Python 2 7
1.10.11 New and Improved Modules
--------------------------------
As in every release, Python’s standard library received a number of
enhancements and bug fixes. Here’s a partial list of the most notable
changes, sorted alphabetically by module name. Consult the ‘Misc/NEWS’
file in the source tree for a more complete list of changes, or look
through the Subversion logs for all the details.
* The *note bdb: f. module’s base debugging class *note Bdb: c98.
gained a feature for skipping modules. The constructor now takes
an iterable containing glob-style patterns such as ‘django.*’; the
debugger will not step into stack frames from a module that matches
one of these patterns. (Contributed by Maru Newby after a
suggestion by Senthil Kumaran; bpo-5142(1).)
* The *note binascii: 10. module now supports the buffer API, so it
can be used with *note memoryview: 25c. instances and other similar
buffer objects. (Backported from 3.x by Florent Xicluna;
bpo-7703(2).)
* Updated module: the ‘bsddb’ module has been updated from
4.7.2devel9 to version 4.8.4 of the pybsddb package(3). The new
version features better Python 3.x compatibility, various bug
fixes, and adds several new BerkeleyDB flags and methods. (Updated
by Jesús Cea Avión; bpo-8156(4). The pybsddb changelog can be read
at ‘http://hg.jcea.es/pybsddb/file/tip/ChangeLog’.)
* The *note bz2: 14. module’s *note BZ2File: 931. now supports the
context management protocol, so you can write ‘with
bz2.BZ2File(...) as f:’. (Contributed by Hagen Fürstenau;
bpo-3860(5).)
* New class: the *note Counter: 9da. class in the *note collections:
1e. module is useful for tallying data. *note Counter: 9da.
instances behave mostly like dictionaries but return zero for
missing keys instead of raising a *note KeyError: 2c7.:
>>> from collections import Counter
>>> c = Counter()
>>> for letter in 'here is a sample of english text':
... c[letter] += 1
...
>>> c
Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
'p': 1, 'r': 1, 'x': 1})
>>> c['e']
5
>>> c['z']
0
There are three additional *note Counter: 9da. methods. *note
most_common(): c99. returns the N most common elements and their
counts. *note elements(): c9a. returns an iterator over the
contained elements, repeating each element as many times as its
count. *note subtract(): b5a. takes an iterable and subtracts one
for each element instead of adding; if the argument is a dictionary
or another ‘Counter’, the counts are subtracted.
>>> c.most_common(5)
[(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
>>> c.elements() ->
'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
's', 's', 'r', 't', 't', 'x'
>>> c['e']
5
>>> c.subtract('very heavy on the letter e')
>>> c['e'] # Count is now lower
-1
Contributed by Raymond Hettinger; bpo-1696199(6).
New class: *note OrderedDict: 1b9. is described in the earlier
section *note PEP 372; Adding an Ordered Dictionary to collections:
c80.
New method: The *note deque: 531. data type now has a *note
count(): b5c. method that returns the number of contained elements
equal to the supplied argument `x', and a *note reverse(): b5d.
method that reverses the elements of the deque in-place. *note
deque: 531. also exposes its maximum length as the read-only *note
maxlen: c9b. attribute. (Both features added by Raymond
Hettinger.)
The *note namedtuple: 1b7. class now has an optional `rename'
parameter. If `rename' is true, field names that are invalid
because they’ve been repeated or aren’t legal Python identifiers
will be renamed to legal names that are derived from the field’s
position within the list of fields:
>>> from collections import namedtuple
>>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
>>> T._fields
('field1', '_1', '_2', 'field2')
(Added by Raymond Hettinger; bpo-1818(7).)
Finally, the ‘Mapping’ abstract base class now returns *note
NotImplemented: 84c. if a mapping is compared to another type that
isn’t a ‘Mapping’. (Fixed by Daniel Stutzbach; bpo-8729(8).)
* Constructors for the parsing classes in the ‘ConfigParser’ module
now take an `allow_no_value' parameter, defaulting to false; if
true, options without values will be allowed. For example:
>>> import ConfigParser, StringIO
>>> sample_config = """
... [mysqld]
... user = mysql
... pid-file = /var/run/mysqld/mysqld.pid
... skip-bdb
... """
>>> config = ConfigParser.RawConfigParser(allow_no_value=True)
>>> config.readfp(StringIO.StringIO(sample_config))
>>> config.get('mysqld', 'user')
'mysql'
>>> print config.get('mysqld', 'skip-bdb')
None
>>> print config.get('mysqld', 'unknown')
Traceback (most recent call last):
...
NoOptionError: No option 'unknown' in section: 'mysqld'
(Contributed by Mats Kindahl; bpo-7005(9).)
* Deprecated function: ‘contextlib.nested()’, which allows handling
more than one context manager with a single *note with: 6e9.
statement, has been deprecated, because the ‘with’ statement now
supports multiple context managers.
* The ‘cookielib’ module now ignores cookies that have an invalid
version field, one that doesn’t contain an integer value. (Fixed
by John J. Lee; bpo-3924(10).)
* The *note copy: 26. module’s *note deepcopy(): 3cc. function will
now correctly copy bound instance methods. (Implemented by Robert
Collins; bpo-1515(11).)
* The *note ctypes: 2b. module now always converts ‘None’ to a C
‘NULL’ pointer for arguments declared as pointers. (Changed by
Thomas Heller; bpo-4606(12).) The underlying libffi library(13)
has been updated to version 3.0.9, containing various fixes for
different platforms. (Updated by Matthias Klose; bpo-8142(14).)
* New method: the *note datetime: 31. module’s *note timedelta: 195.
class gained a *note total_seconds(): c9c. method that returns the
number of seconds in the duration. (Contributed by Brian Quinlan;
bpo-5788(15).)
* New method: the *note Decimal: 188. class gained a *note
from_float(): b80. class method that performs an exact conversion
of a floating-point number to a *note Decimal: 188. This exact
conversion strives for the closest decimal approximation to the
floating-point representation’s value; the resulting decimal value
will therefore still include the inaccuracy, if any. For example,
‘Decimal.from_float(0.1)’ returns
‘Decimal('0.1000000000000000055511151231257827021181583404541015625')’.
(Implemented by Raymond Hettinger; bpo-4796(16).)
Comparing instances of *note Decimal: 188. with floating-point
numbers now produces sensible results based on the numeric values
of the operands. Previously such comparisons would fall back to
Python’s default rules for comparing objects, which produced
arbitrary results based on their type. Note that you still cannot
combine ‘Decimal’ and floating-point in other operations such as
addition, since you should be explicitly choosing how to convert
between float and *note Decimal: 188. (Fixed by Mark Dickinson;
bpo-2531(17).)
The constructor for *note Decimal: 188. now accepts floating-point
numbers (added by Raymond Hettinger; bpo-8257(18)) and non-European
Unicode characters such as Arabic-Indic digits (contributed by Mark
Dickinson; bpo-6595(19)).
Most of the methods of the *note Context: 9f0. class now accept
integers as well as *note Decimal: 188. instances; the only
exceptions are the *note canonical(): c9d. and *note
is_canonical(): c9e. methods. (Patch by Juan José Conti;
bpo-7633(20).)
When using *note Decimal: 188. instances with a string’s *note
format(): 4da. method, the default alignment was previously
left-alignment. This has been changed to right-alignment, which is
more sensible for numeric types. (Changed by Mark Dickinson;
bpo-6857(21).)
Comparisons involving a signaling NaN value (or ‘sNAN’) now signal
‘InvalidOperation’ instead of silently returning a true or false
value depending on the comparison operator. Quiet NaN values (or
‘NaN’) are now hashable. (Fixed by Mark Dickinson; bpo-7279(22).)
* The *note difflib: 37. module now produces output that is more
compatible with modern ‘diff’/‘patch’ tools through one small
change, using a tab character instead of spaces as a separator in
the header giving the filename. (Fixed by Anatoly Techtonik;
bpo-7585(23).)
* The Distutils ‘sdist’ command now always regenerates the ‘MANIFEST’
file, since even if the ‘MANIFEST.in’ or ‘setup.py’ files haven’t
been modified, the user might have created some new files that
should be included. (Fixed by Tarek Ziadé; bpo-8688(24).)
* The *note doctest: 67. module’s ‘IGNORE_EXCEPTION_DETAIL’ flag will
now ignore the name of the module containing the exception being
tested. (Patch by Lennart Regebro; bpo-7490(25).)
* The *note email: 69. module’s *note Message: 541. class will now
accept a Unicode-valued payload, automatically converting the
payload to the encoding specified by ‘output_charset’. (Added by
R. David Murray; bpo-1368247(26).)
* The *note Fraction: 185. class now accepts a single float or *note
Decimal: 188. instance, or two rational numbers, as arguments to
its constructor. (Implemented by Mark Dickinson; rationals added
in bpo-5812(27), and float/decimal in bpo-8294(28).)
Ordering comparisons (‘<’, ‘<=’, ‘>’, ‘>=’) between fractions and
complex numbers now raise a *note TypeError: 192. This fixes an
oversight, making the *note Fraction: 185. match the other numeric
types.
* New class: *note FTP_TLS: a03. in the *note ftplib: 84. module
provides secure FTP connections using TLS encapsulation of
authentication as well as subsequent control and data transfers.
(Contributed by Giampaolo Rodola; bpo-2054(29).)
The *note storbinary(): c9f. method for binary uploads can now
restart uploads thanks to an added `rest' parameter (patch by Pablo
Mouzo; bpo-6845(30).)
* New class decorator: *note total_ordering(): 84b. in the *note
functools: 85. module takes a class that defines an *note __eq__():
33f. method and one of *note __lt__(): c34, *note __le__(): ca0,
*note __gt__(): ca1, or *note __ge__(): ca2, and generates the
missing comparison methods. Since the ‘__cmp__()’ method is being
deprecated in Python 3.x, this decorator makes it easier to define
ordered classes. (Added by Raymond Hettinger; bpo-5479(31).)
New function: *note cmp_to_key(): b56. will take an old-style
comparison function that expects two arguments and return a new
callable that can be used as the `key' parameter to functions such
as *note sorted(): 444, *note min(): 809. and *note max(): 80a,
etc. The primary intended use is to help with making code
compatible with Python 3.x. (Added by Raymond Hettinger.)
* New function: the *note gc: 86. module’s *note is_tracked(): ca3.
returns true if a given instance is tracked by the garbage
collector, false otherwise. (Contributed by Antoine Pitrou;
bpo-4688(32).)
* The *note gzip: 8c. module’s *note GzipFile: 6dd. now supports the
context management protocol, so you can write ‘with
gzip.GzipFile(...) as f:’ (contributed by Hagen Fürstenau;
bpo-3860(33)), and it now implements the *note io.BufferedIOBase:
588. ABC, so you can wrap it with *note io.BufferedReader: b8a. for
faster processing (contributed by Nir Aides; bpo-7471(34)). It’s
also now possible to override the modification time recorded in a
gzipped file by providing an optional timestamp to the constructor.
(Contributed by Jacques Frechet; bpo-4272(35).)
Files in gzip format can be padded with trailing zero bytes; the
*note gzip: 8c. module will now consume these trailing bytes.
(Fixed by Tadek Pietraszek and Brian Curtin; bpo-2846(36).)
* New attribute: the *note hashlib: 8d. module now has an
‘algorithms’ attribute containing a tuple naming the supported
algorithms. In Python 2.7, ‘hashlib.algorithms’ contains ‘('md5',
'sha1', 'sha224', 'sha256', 'sha384', 'sha512')’. (Contributed by
Carl Chenet; bpo-7418(37).)
* The default ‘HTTPResponse’ class used by the ‘httplib’ module now
supports buffering, resulting in much faster reading of HTTP
responses. (Contributed by Kristján Valur Jónsson; bpo-4879(38).)
The ‘HTTPConnection’ and ‘HTTPSConnection’ classes now support a
`source_address' parameter, a ‘(host, port)’ 2-tuple giving the
source address that will be used for the connection. (Contributed
by Eldon Ziegler; bpo-3972(39).)
* The ‘ihooks’ module now supports relative imports. Note that
‘ihooks’ is an older module for customizing imports, superseded by
the ‘imputil’ module added in Python 2.0. (Relative import support
added by Neil Schemenauer.)
* The *note imaplib: 98. module now supports IPv6 addresses.
(Contributed by Derek Morr; bpo-1655(40).)
* New function: the *note inspect: a0. module’s *note getcallargs():
7b6. takes a callable and its positional and keyword arguments, and
figures out which of the callable’s parameters will receive each
argument, returning a dictionary mapping argument names to their
values. For example:
>>> from inspect import getcallargs
>>> def f(a, b=1, *pos, **named):
... pass
>>> getcallargs(f, 1, 2, 3)
{'a': 1, 'b': 2, 'pos': (3,), 'named': {}}
>>> getcallargs(f, a=2, x=4)
{'a': 2, 'b': 1, 'pos': (), 'named': {'x': 4}}
>>> getcallargs(f)
Traceback (most recent call last):
...
TypeError: f() takes at least 1 argument (0 given)
Contributed by George Sakkis; bpo-3135(41).
* Updated module: The *note io: a1. library has been upgraded to the
version shipped with Python 3.1. For 3.1, the I/O library was
entirely rewritten in C and is 2 to 20 times faster depending on
the task being performed. The original Python version was renamed
to the ‘_pyio’ module.
One minor resulting change: the *note io.TextIOBase: c39. class now
has an ‘errors’ attribute giving the error setting used for
encoding and decoding errors (one of ‘'strict'’, ‘'replace'’,
‘'ignore'’).
The *note io.FileIO: ca4. class now raises an *note OSError: 1d3.
when passed an invalid file descriptor. (Implemented by Benjamin
Peterson; bpo-4991(42).) The *note truncate(): ca5. method now
preserves the file position; previously it would change the file
position to the end of the new file. (Fixed by Pascal Chambon;
bpo-6939(43).)
* New function: ‘itertools.compress(data, selectors)’ takes two
iterators. Elements of `data' are returned if the corresponding
value in `selectors' is true:
itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
A, C, E, F
New function: ‘itertools.combinations_with_replacement(iter, r)’
returns all the possible `r'-length combinations of elements from
the iterable `iter'. Unlike *note combinations(): ca6, individual
elements can be repeated in the generated combinations:
itertools.combinations_with_replacement('abc', 2) =>
('a', 'a'), ('a', 'b'), ('a', 'c'),
('b', 'b'), ('b', 'c'), ('c', 'c')
Note that elements are treated as unique depending on their
position in the input, not their actual values.
The *note itertools.count(): c1b. function now has a `step'
argument that allows incrementing by values other than 1. *note
count(): c1b. also now allows keyword arguments, and using
non-integer values such as floats or *note Decimal: 188. instances.
(Implemented by Raymond Hettinger; bpo-5032(44).)
*note itertools.combinations(): ca6. and *note itertools.product():
ca7. previously raised *note ValueError: 1fb. for values of `r'
larger than the input iterable. This was deemed a specification
error, so they now return an empty iterator. (Fixed by Raymond
Hettinger; bpo-4816(45).)
* Updated module: The *note json: a4. module was upgraded to version
2.0.9 of the simplejson package, which includes a C extension that
makes encoding and decoding faster. (Contributed by Bob Ippolito;
bpo-4136(46).)
To support the new *note collections.OrderedDict: 1b9. type, *note
json.load(): 55f. now has an optional `object_pairs_hook' parameter
that will be called with any object literal that decodes to a list
of pairs. (Contributed by Raymond Hettinger; bpo-5381(47).)
* The *note mailbox: ae. module’s *note Maildir: ca8. class now
records the timestamp on the directories it reads, and only
re-reads them if the modification time has subsequently changed.
This improves performance by avoiding unneeded directory scans.
(Fixed by A.M. Kuchling and Antoine Pitrou; bpo-1607951(48),
bpo-6896(49).)
* New functions: the *note math: b1. module gained *note erf(): 452.
and *note erfc(): 453. for the error function and the complementary
error function, *note expm1(): b68. which computes ‘e**x - 1’ with
more precision than using *note exp(): ca9. and subtracting 1,
*note gamma(): b69. for the Gamma function, and *note lgamma():
b6a. for the natural log of the Gamma function. (Contributed by
Mark Dickinson and nirinA raseliarison; bpo-3366(50).)
* The *note multiprocessing: b7. module’s ‘Manager*’ classes can now
be passed a callable that will be called whenever a subprocess is
started, along with a set of arguments that will be passed to the
callable. (Contributed by lekma; bpo-5585(51).)
The ‘Pool’ class, which controls a pool of worker processes, now
has an optional `maxtasksperchild' parameter. Worker processes
will perform the specified number of tasks and then exit, causing
the ‘Pool’ to start a new worker. This is useful if tasks may leak
memory or other resources, or if some tasks will cause the worker
to become very large. (Contributed by Charles Cazabon;
bpo-6963(52).)
* The *note nntplib: c0. module now supports IPv6 addresses.
(Contributed by Derek Morr; bpo-1664(53).)
* New functions: the *note os: c4. module wraps the following POSIX
system calls: *note getresgid(): caa. and *note getresuid(): cab,
which return the real, effective, and saved GIDs and UIDs; *note
setresgid(): cac. and *note setresuid(): cad, which set real,
effective, and saved GIDs and UIDs to new values; *note
initgroups(): cae, which initialize the group access list for the
current process. (GID/UID functions contributed by Travis H.;
bpo-6508(54). Support for initgroups added by Jean-Paul Calderone;
bpo-7333(55).)
The *note os.fork(): 961. function now re-initializes the import
lock in the child process; this fixes problems on Solaris when
*note fork(): 961. is called from a thread. (Fixed by Zsolt
Cserna; bpo-7242(56).)
* In the *note os.path: c5. module, the *note normpath(): caf. and
*note abspath(): cb0. functions now preserve Unicode; if their
input path is a Unicode string, the return value is also a Unicode
string. (*note normpath(): caf. fixed by Matt Giuca in
bpo-5827(57); *note abspath(): cb0. fixed by Ezio Melotti in
bpo-3426(58).)
* The *note pydoc: d9. module now has help for the various symbols
that Python uses. You can now do ‘help('<<')’ or ‘help('@')’, for
example. (Contributed by David Laban; bpo-4739(59).)
* The *note re: dd. module’s *note split(): 3ca, *note sub(): 47b,
and *note subn(): 734. now accept an optional `flags' argument, for
consistency with the other functions in the module. (Added by
Gregory P. Smith.)
* New function: *note run_path(): cb1. in the *note runpy: e2. module
will execute the code at a provided `path' argument. `path' can be
the path of a Python source file (‘example.py’), a compiled
bytecode file (‘example.pyc’), a directory (‘./package/’), or a zip
archive (‘example.zip’). If a directory or zip path is provided,
it will be added to the front of ‘sys.path’ and the module *note
__main__: 1. will be imported. It’s expected that the directory or
zip contains a ‘__main__.py’; if it doesn’t, some other
‘__main__.py’ might be imported from a location later in
‘sys.path’. This makes more of the machinery of *note runpy: e2.
available to scripts that want to mimic the way Python’s command
line processes an explicit path name. (Added by Nick Coghlan;
bpo-6816(60).)
* New function: in the *note shutil: e9. module, *note
make_archive(): 21b. takes a filename, archive type (zip or
tar-format), and a directory path, and creates an archive
containing the directory’s contents. (Added by Tarek Ziadé.)
*note shutil: e9.’s *note copyfile(): 258. and *note copytree():
21a. functions now raise a ‘SpecialFileError’ exception when asked
to copy a named pipe. Previously the code would treat named pipes
like a regular file by opening them for reading, and this would
block indefinitely. (Fixed by Antoine Pitrou; bpo-3002(61).)
* The *note signal: ea. module no longer re-installs the signal
handler unless this is truly necessary, which fixes a bug that
could make it impossible to catch the EINTR signal robustly.
(Fixed by Charles-Francois Natali; bpo-8354(62).)
* New functions: in the *note site: eb. module, three new functions
return various site- and user-specific paths. *note
getsitepackages(): bd3. returns a list containing all global
site-packages directories, *note getusersitepackages(): bd5.
returns the path of the user’s site-packages directory, and *note
getuserbase(): bd4. returns the value of the ‘USER_BASE’
environment variable, giving the path to a directory that can be
used to store data. (Contributed by Tarek Ziadé; bpo-6693(63).)
The *note site: eb. module now reports exceptions occurring when
the ‘sitecustomize’ module is imported, and will no longer catch
and swallow the *note KeyboardInterrupt: 197. exception. (Fixed by
Victor Stinner; bpo-3137(64).)
* The *note create_connection(): b9e. function gained a
`source_address' parameter, a ‘(host, port)’ 2-tuple giving the
source address that will be used for the connection. (Contributed
by Eldon Ziegler; bpo-3972(65).)
The *note recv_into(): cb2. and *note recvfrom_into(): cb3. methods
will now write into objects that support the buffer API, most
usefully the *note bytearray: 332. and *note memoryview: 25c.
objects. (Implemented by Antoine Pitrou; bpo-8104(66).)
* The ‘SocketServer’ module’s ‘TCPServer’ class now supports socket
timeouts and disabling the Nagle algorithm. The
‘disable_nagle_algorithm’ class attribute defaults to ‘False’; if
overridden to be true, new request connections will have the
TCP_NODELAY option set to prevent buffering many small sends into a
single TCP packet. The ‘timeout’ class attribute can hold a
timeout in seconds that will be applied to the request socket; if
no request is received within that time, ‘handle_timeout()’ will be
called and ‘handle_request()’ will return. (Contributed by
Kristján Valur Jónsson; bpo-6192(67) and bpo-6267(68).)
* Updated module: the *note sqlite3: f2. module has been updated to
version 2.6.0 of the pysqlite package(69). Version 2.6.0 includes
a number of bugfixes, and adds the ability to load SQLite
extensions from shared libraries. Call the
‘enable_load_extension(True)’ method to enable extensions, and then
call *note load_extension(): b9a. to load a particular shared
library. (Updated by Gerhard Häring.)
* The *note ssl: f3. module’s *note SSLSocket: 3e2. objects now
support the buffer API, which fixed a test suite failure (fix by
Antoine Pitrou; bpo-7133(70)) and automatically set OpenSSL’s
‘SSL_MODE_AUTO_RETRY’, which will prevent an error code being
returned from ‘recv()’ operations that trigger an SSL renegotiation
(fix by Antoine Pitrou; bpo-8222(71)).
The *note ssl.wrap_socket(): 46f. constructor function now takes a
`ciphers' argument that’s a string listing the encryption
algorithms to be allowed; the format of the string is described in
the OpenSSL documentation(72). (Added by Antoine Pitrou;
bpo-8322(73).)
Another change makes the extension load all of OpenSSL’s ciphers
and digest algorithms so that they’re all available. Some SSL
certificates couldn’t be verified, reporting an “unknown algorithm”
error. (Reported by Beda Kosata, and fixed by Antoine Pitrou;
bpo-8484(74).)
The version of OpenSSL being used is now available as the module
attributes *note ssl.OPENSSL_VERSION: ba1. (a string), *note
ssl.OPENSSL_VERSION_INFO: ba2. (a 5-tuple), and *note
ssl.OPENSSL_VERSION_NUMBER: ba3. (an integer). (Added by Antoine
Pitrou; bpo-8321(75).)
* The *note struct: f8. module will no longer silently ignore
overflow errors when a value is too large for a particular integer
format code (one of ‘bBhHiIlLqQ’); it now always raises a *note
struct.error: cb4. exception. (Changed by Mark Dickinson;
bpo-1523(76).) The *note pack(): c0b. function will also attempt
to use *note __index__(): 18a. to convert and pack non-integers
before trying the *note __int__(): 18b. method or reporting an
error. (Changed by Mark Dickinson; bpo-8300(77).)
* New function: the *note subprocess: f9. module’s *note
check_output(): 8db. runs a command with a specified set of
arguments and returns the command’s output as a string when the
command runs without error, or raises a *note CalledProcessError:
cb5. exception otherwise.
>>> subprocess.check_output(['df', '-h', '.'])
'Filesystem Size Used Avail Capacity Mounted on\n
/dev/disk0s2 52G 49G 3.0G 94% /\n'
>>> subprocess.check_output(['df', '-h', '/bogus'])
...
subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
(Contributed by Gregory P. Smith.)
The *note subprocess: f9. module will now retry its internal system
calls on receiving an ‘EINTR’ signal. (Reported by several people;
final patch by Gregory P. Smith in bpo-1068268(78).)
* New function: *note is_declared_global(): cb6. in the *note
symtable: fc. module returns true for variables that are explicitly
declared to be global, false for ones that are implicitly global.
(Contributed by Jeremy Hylton.)
* The *note syslog: ff. module will now use the value of
‘sys.argv[0]’ as the identifier instead of the previous default
value of ‘'python'’. (Changed by Sean Reifschneider;
bpo-8451(79).)
* The ‘sys.version_info’ value is now a named tuple, with attributes
named ‘major’, ‘minor’, ‘micro’, ‘releaselevel’, and ‘serial’.
(Contributed by Ross Light; bpo-4285(80).)
*note sys.getwindowsversion(): 595. also returns a named tuple,
with attributes named ‘major’, ‘minor’, ‘build’, *note platform:
ce, ‘service_pack’, ‘service_pack_major’, ‘service_pack_minor’,
‘suite_mask’, and ‘product_type’. (Contributed by Brian Curtin;
bpo-7766(81).)
* The *note tarfile: 101. module’s default error handling has
changed, to no longer suppress fatal errors. The default error
level was previously 0, which meant that errors would only result
in a message being written to the debug log, but because the debug
log is not activated by default, these errors go unnoticed. The
default error level is now 1, which raises an exception if there’s
an error. (Changed by Lars Gustäbel; bpo-7357(82).)
*note tarfile: 101. now supports filtering the *note TarInfo: b8d.
objects being added to a tar file. When you call *note add(): 47c,
you may supply an optional `filter' argument that’s a callable.
The `filter' callable will be passed the *note TarInfo: b8d. for
every file being added, and can modify and return it. If the
callable returns ‘None’, the file will be excluded from the
resulting archive. This is more powerful than the existing
`exclude' argument, which has therefore been deprecated. (Added by
Lars Gustäbel; bpo-6856(83).) The *note TarFile: b8c. class also
now supports the context management protocol. (Added by Lars
Gustäbel; bpo-7232(84).)
* The *note wait(): cb7. method of the *note threading.Event: aad.
class now returns the internal flag on exit. This means the method
will usually return true because *note wait(): cb7. is supposed to
block until the internal flag becomes true. The return value will
only be false if a timeout was provided and the operation timed
out. (Contributed by Tim Lesher; bpo-1674032(85).)
* The Unicode database provided by the *note unicodedata: 11a. module
is now used internally to determine which characters are numeric,
whitespace, or represent line breaks. The database also includes
information from the ‘Unihan.txt’ data file (patch by Anders
Chrigström and Amaury Forgeot d’Arc; bpo-1571184(86)) and has been
updated to version 5.2.0 (updated by Florent Xicluna;
bpo-8024(87)).
* The ‘urlparse’ module’s ‘urlsplit()’ now handles unknown URL
schemes in a fashion compliant with RFC 3986(88): if the URL is of
the form ‘"://..."’, the text before the ‘://’ is
treated as the scheme, even if it’s a made-up scheme that the
module doesn’t know about. This change may break code that worked
around the old behaviour. For example, Python 2.6.4 or 2.5 will
return the following:
>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', '', '//host/filename?query', '', '')
Python 2.7 (and Python 2.6.5) will return:
>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', 'host', '/filename?query', '', '')
(Python 2.7 actually produces slightly different output, since it
returns a named tuple instead of a standard tuple.)
The ‘urlparse’ module also supports IPv6 literal addresses as
defined by RFC 2732(89) (contributed by Senthil Kumaran;
bpo-2987(90)).
>>> urlparse.urlparse('http://[1080::8:800:200C:417A]/foo')
ParseResult(scheme='http', netloc='[1080::8:800:200C:417A]',
path='/foo', params='', query='', fragment='')
* New class: the *note WeakSet: cb8. class in the *note weakref: 128.
module is a set that only holds weak references to its elements;
elements will be removed once there are no references pointing to
them. (Originally implemented in Python 3.x by Raymond Hettinger,
and backported to 2.7 by Michael Foord.)
* The ElementTree library, ‘xml.etree’, no longer escapes ampersands
and angle brackets when outputting an XML processing instruction
(which looks like ‘’) or comment
(which looks like ‘’). (Patch by Neil Muller;
bpo-2746(91).)
* The XML-RPC client and server, provided by the ‘xmlrpclib’ and
‘SimpleXMLRPCServer’ modules, have improved performance by
supporting HTTP/1.1 keep-alive and by optionally using gzip
encoding to compress the XML being exchanged. The gzip compression
is controlled by the ‘encode_threshold’ attribute of
‘SimpleXMLRPCRequestHandler’, which contains a size in bytes;
responses larger than this will be compressed. (Contributed by
Kristján Valur Jónsson; bpo-6267(92).)
* The *note zipfile: 142. module’s *note ZipFile: 41f. now supports
the context management protocol, so you can write ‘with
zipfile.ZipFile(...) as f:’. (Contributed by Brian Curtin;
bpo-5511(93).)
*note zipfile: 142. now also supports archiving empty directories
and extracts them correctly. (Fixed by Kuba Wieczorek;
bpo-4710(94).) Reading files out of an archive is faster, and
interleaving *note read(): cb9. and ‘readline()’ now works
correctly. (Contributed by Nir Aides; bpo-7610(95).)
The *note is_zipfile(): cba. function now accepts a file object, in
addition to the path names accepted in earlier versions.
(Contributed by Gabriel Genellina; bpo-4756(96).)
The *note writestr(): cbb. method now has an optional
`compress_type' parameter that lets you override the default
compression method specified in the *note ZipFile: 41f.
constructor. (Contributed by Ronald Oussoren; bpo-6003(97).)
* Menu:
* New module; importlib: New module importlib.
* New module; sysconfig: New module sysconfig.
* ttk; Themed Widgets for Tk: ttk Themed Widgets for Tk.
* Updated module; unittest: Updated module unittest.
* Updated module; ElementTree 1.3: Updated module ElementTree 1 3.
---------- Footnotes ----------
(1) https://bugs.python.org/issue5142
(2) https://bugs.python.org/issue7703
(3) https://www.jcea.es/programacion/pybsddb.htm
(4) https://bugs.python.org/issue8156
(5) https://bugs.python.org/issue3860
(6) https://bugs.python.org/issue1696199
(7) https://bugs.python.org/issue1818
(8) https://bugs.python.org/issue8729
(9) https://bugs.python.org/issue7005
(10) https://bugs.python.org/issue3924
(11) https://bugs.python.org/issue1515
(12) https://bugs.python.org/issue4606
(13) https://sourceware.org/libffi/
(14) https://bugs.python.org/issue8142
(15) https://bugs.python.org/issue5788
(16) https://bugs.python.org/issue4796
(17) https://bugs.python.org/issue2531
(18) https://bugs.python.org/issue8257
(19) https://bugs.python.org/issue6595
(20) https://bugs.python.org/issue7633
(21) https://bugs.python.org/issue6857
(22) https://bugs.python.org/issue7279
(23) https://bugs.python.org/issue7585
(24) https://bugs.python.org/issue8688
(25) https://bugs.python.org/issue7490
(26) https://bugs.python.org/issue1368247
(27) https://bugs.python.org/issue5812
(28) https://bugs.python.org/issue8294
(29) https://bugs.python.org/issue2054
(30) https://bugs.python.org/issue6845
(31) https://bugs.python.org/issue5479
(32) https://bugs.python.org/issue4688
(33) https://bugs.python.org/issue3860
(34) https://bugs.python.org/issue7471
(35) https://bugs.python.org/issue4272
(36) https://bugs.python.org/issue2846
(37) https://bugs.python.org/issue7418
(38) https://bugs.python.org/issue4879
(39) https://bugs.python.org/issue3972
(40) https://bugs.python.org/issue1655
(41) https://bugs.python.org/issue3135
(42) https://bugs.python.org/issue4991
(43) https://bugs.python.org/issue6939
(44) https://bugs.python.org/issue5032
(45) https://bugs.python.org/issue4816
(46) https://bugs.python.org/issue4136
(47) https://bugs.python.org/issue5381
(48) https://bugs.python.org/issue1607951
(49) https://bugs.python.org/issue6896
(50) https://bugs.python.org/issue3366
(51) https://bugs.python.org/issue5585
(52) https://bugs.python.org/issue6963
(53) https://bugs.python.org/issue1664
(54) https://bugs.python.org/issue6508
(55) https://bugs.python.org/issue7333
(56) https://bugs.python.org/issue7242
(57) https://bugs.python.org/issue5827
(58) https://bugs.python.org/issue3426
(59) https://bugs.python.org/issue4739
(60) https://bugs.python.org/issue6816
(61) https://bugs.python.org/issue3002
(62) https://bugs.python.org/issue8354
(63) https://bugs.python.org/issue6693
(64) https://bugs.python.org/issue3137
(65) https://bugs.python.org/issue3972
(66) https://bugs.python.org/issue8104
(67) https://bugs.python.org/issue6192
(68) https://bugs.python.org/issue6267
(69) https://github.com/ghaering/pysqlite
(70) https://bugs.python.org/issue7133
(71) https://bugs.python.org/issue8222
(72)
https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-LIST-FORMAT
(73) https://bugs.python.org/issue8322
(74) https://bugs.python.org/issue8484
(75) https://bugs.python.org/issue8321
(76) https://bugs.python.org/issue1523
(77) https://bugs.python.org/issue8300
(78) https://bugs.python.org/issue1068268
(79) https://bugs.python.org/issue8451
(80) https://bugs.python.org/issue4285
(81) https://bugs.python.org/issue7766
(82) https://bugs.python.org/issue7357
(83) https://bugs.python.org/issue6856
(84) https://bugs.python.org/issue7232
(85) https://bugs.python.org/issue1674032
(86) https://bugs.python.org/issue1571184
(87) https://bugs.python.org/issue8024
(88) https://tools.ietf.org/html/rfc3986.html
(89) https://tools.ietf.org/html/rfc2732.html
(90) https://bugs.python.org/issue2987
(91) https://bugs.python.org/issue2746
(92) https://bugs.python.org/issue6267
(93) https://bugs.python.org/issue5511
(94) https://bugs.python.org/issue4710
(95) https://bugs.python.org/issue7610
(96) https://bugs.python.org/issue4756
(97) https://bugs.python.org/issue6003
File: python.info, Node: New module importlib, Next: New module sysconfig, Up: New and Improved Modules
1.10.11.1 New module: importlib
...............................
Python 3.1 includes the *note importlib: 9b. package, a
re-implementation of the logic underlying Python’s *note import: c1d.
statement. *note importlib: 9b. is useful for implementors of Python
interpreters and to users who wish to write new importers that can
participate in the import process. Python 2.7 doesn’t contain the
complete *note importlib: 9b. package, but instead has a tiny subset
that contains a single function, *note import_module(): b13.
‘import_module(name, package=None)’ imports a module. `name' is a
string containing the module or package’s name. It’s possible to do
relative imports by providing a string that begins with a ‘.’ character,
such as ‘..utils.errors’. For relative imports, the `package' argument
must be provided and is the name of the package that will be used as the
anchor for the relative import. *note import_module(): b13. both
inserts the imported module into ‘sys.modules’ and returns the module
object.
Here are some examples:
>>> from importlib import import_module
>>> anydbm = import_module('anydbm') # Standard absolute import
>>> anydbm
>>> # Relative import
>>> file_util = import_module('..file_util', 'distutils.command')
>>> file_util
*note importlib: 9b. was implemented by Brett Cannon and introduced in
Python 3.1.
File: python.info, Node: New module sysconfig, Next: ttk Themed Widgets for Tk, Prev: New module importlib, Up: New and Improved Modules
1.10.11.2 New module: sysconfig
...............................
The *note sysconfig: fe. module has been pulled out of the Distutils
package, becoming a new top-level module in its own right. *note
sysconfig: fe. provides functions for getting information about Python’s
build process: compiler switches, installation paths, the platform name,
and whether Python is running from its source directory.
Some of the functions in the module are:
* *note get_config_var(): 964. returns variables from Python’s
Makefile and the ‘pyconfig.h’ file.
* *note get_config_vars(): 965. returns a dictionary containing all
of the configuration variables.
* *note get_path(): cbe. returns the configured path for a particular
type of module: the standard library, site-specific modules,
platform-specific modules, etc.
* *note is_python_build(): cbf. returns true if you’re running a
binary from a Python source tree, and false otherwise.
Consult the *note sysconfig: fe. documentation for more details and for
a complete list of functions.
The Distutils package and *note sysconfig: fe. are now maintained by
Tarek Ziadé, who has also started a Distutils2 package (source
repository at ‘https://hg.python.org/distutils2/’) for developing a
next-generation version of Distutils.
File: python.info, Node: ttk Themed Widgets for Tk, Next: Updated module unittest, Prev: New module sysconfig, Up: New and Improved Modules
1.10.11.3 ttk: Themed Widgets for Tk
....................................
Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
widgets but have a more customizable appearance and can therefore more
closely resemble the native platform’s widgets. This widget set was
originally called Tile, but was renamed to Ttk (for “themed Tk”) on
being added to Tcl/Tck release 8.5.
To learn more, read the ‘ttk’ module documentation. You may also wish
to read the Tcl/Tk manual page describing the Ttk theme engine,
available at ‘https://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm’. Some
screenshots of the Python/Ttk code in use are at
‘https://code.google.com/archive/p/python-ttk/wikis/Screenshots.wiki’.
The ‘ttk’ module was written by Guilherme Polo and added in bpo-2983(1).
An alternate version called ‘Tile.py’, written by Martin Franklin and
maintained by Kevin Walzer, was proposed for inclusion in bpo-2618(2),
but the authors argued that Guilherme Polo’s work was more
comprehensive.
---------- Footnotes ----------
(1) https://bugs.python.org/issue2983
(2) https://bugs.python.org/issue2618
File: python.info, Node: Updated module unittest, Next: Updated module ElementTree 1 3, Prev: ttk Themed Widgets for Tk, Up: New and Improved Modules
1.10.11.4 Updated module: unittest
..................................
The *note unittest: 11b. module was greatly enhanced; many new features
were added. Most of these features were implemented by Michael Foord,
unless otherwise noted. The enhanced version of the module is
downloadable separately for use with Python versions 2.4 to 2.6,
packaged as the ‘unittest2’ package, from
‘https://pypi.org/project/unittest2’.
When used from the command line, the module can automatically discover
tests. It’s not as fancy as py.test(1) or nose(2), but provides a
simple way to run tests kept within a set of package directories. For
example, the following command will search the ‘test/’ subdirectory for
any importable test files named ‘test*.py’:
python -m unittest discover -s test
Consult the *note unittest: 11b. module documentation for more details.
(Developed in bpo-6001(3).)
The *note main(): 902. function supports some other new options:
* *note -b: cc3. or ‘--buffer’ will buffer the standard output and
standard error streams during each test. If the test passes, any
resulting output will be discarded; on failure, the buffered output
will be displayed.
* *note -c: cc4. or ‘--catch’ will cause the control-C interrupt to
be handled more gracefully. Instead of interrupting the test
process immediately, the currently running test will be completed
and then the partial results up to the interruption will be
reported. If you’re impatient, a second press of control-C will
cause an immediate interruption.
This control-C handler tries to avoid causing problems when the
code being tested or the tests being run have defined a signal
handler of their own, by noticing that a signal handler was already
set and calling it. If this doesn’t work for you, there’s a *note
removeHandler(): cc5. decorator that can be used to mark tests that
should have the control-C handling disabled.
* *note -f: cc6. or ‘--failfast’ makes test execution stop
immediately when a test fails instead of continuing to execute
further tests. (Suggested by Cliff Dyer and implemented by Michael
Foord; bpo-8074(4).)
The progress messages now show ‘x’ for expected failures and ‘u’ for
unexpected successes when run in verbose mode. (Contributed by Benjamin
Peterson.)
Test cases can raise the *note SkipTest: 903. exception to skip a test
(bpo-1034053(5)).
The error messages for *note assertEqual(): bb5, *note assertTrue():
bb4, and *note assertFalse(): cc7. failures now provide more
information. If you set the *note longMessage: cc8. attribute of your
*note TestCase: 8ff. classes to true, both the standard error message
and any additional message you provide will be printed for failures.
(Added by Michael Foord; bpo-5663(6).)
The *note assertRaises(): aba. method now returns a context handler when
called without providing a callable object to run. For example, you can
write this:
with self.assertRaises(KeyError):
{}['foo']
(Implemented by Antoine Pitrou; bpo-4444(7).)
Module- and class-level setup and teardown fixtures are now supported.
Modules can contain ‘setUpModule()’ and ‘tearDownModule()’ functions.
Classes can have *note setUpClass(): 24c. and *note tearDownClass():
cc9. methods that must be defined as class methods (using ‘@classmethod’
or equivalent). These functions and methods are invoked when the test
runner switches to a test case in a different module or class.
The methods *note addCleanup(): 2a2. and *note doCleanups(): cca. were
added. *note addCleanup(): 2a2. lets you add cleanup functions that
will be called unconditionally (after *note setUp(): ccb. if *note
setUp(): ccb. fails, otherwise after *note tearDown(): ccc.). This
allows for much simpler resource allocation and deallocation during
tests (bpo-5679(8)).
A number of new methods were added that provide more specialized tests.
Many of these methods were written by Google engineers for use in their
test suites; Gregory P. Smith, Michael Foord, and GvR worked on merging
them into Python’s version of *note unittest: 11b.
* *note assertIsNone(): ccd. and *note assertIsNotNone(): cce. take
one expression and verify that the result is or is not ‘None’.
* *note assertIs(): ccf. and *note assertIsNot(): cd0. take two
values and check whether the two values evaluate to the same object
or not. (Added by Michael Foord; bpo-2578(9).)
* *note assertIsInstance(): cd1. and *note assertNotIsInstance():
cd2. check whether the resulting object is an instance of a
particular class, or of one of a tuple of classes. (Added by Georg
Brandl; bpo-7031(10).)
* *note assertGreater(): cd3, *note assertGreaterEqual(): cd4, *note
assertLess(): cd5, and *note assertLessEqual(): cd6. compare two
quantities.
* *note assertMultiLineEqual(): cd7. compares two strings, and if
they’re not equal, displays a helpful comparison that highlights
the differences in the two strings. This comparison is now used by
default when Unicode strings are compared with *note assertEqual():
bb5.
* ‘assertRegexpMatches()’ and ‘assertNotRegexpMatches()’ checks
whether the first argument is a string matching or not matching the
regular expression provided as the second argument (bpo-8038(11)).
* ‘assertRaisesRegexp()’ checks whether a particular exception is
raised, and then also checks that the string representation of the
exception matches the provided regular expression.
* *note assertIn(): cd8. and *note assertNotIn(): cd9. tests whether
`first' is or is not in `second'.
* ‘assertItemsEqual()’ tests whether two provided sequences contain
the same elements.
* *note assertSetEqual(): cda. compares whether two sets are equal,
and only reports the differences between the sets in case of error.
* Similarly, *note assertListEqual(): cdb. and *note
assertTupleEqual(): cdc. compare the specified types and explain
any differences without necessarily printing their full values;
these methods are now used by default when comparing lists and
tuples using *note assertEqual(): bb5. More generally, *note
assertSequenceEqual(): cdd. compares two sequences and can
optionally check whether both sequences are of a particular type.
* *note assertDictEqual(): cde. compares two dictionaries and reports
the differences; it’s now used by default when you compare two
dictionaries using *note assertEqual(): bb5.
‘assertDictContainsSubset()’ checks whether all of the key/value
pairs in `first' are found in `second'.
* *note assertAlmostEqual(): bb7. and *note assertNotAlmostEqual():
bb8. test whether `first' and `second' are approximately equal.
This method can either round their difference to an
optionally-specified number of `places' (the default is 7) and
compare it to zero, or require the difference to be smaller than a
supplied `delta' value.
* *note loadTestsFromName(): cdf. properly honors the *note
suiteClass: ce0. attribute of the *note TestLoader: 782. (Fixed by
Mark Roddy; bpo-6866(12).)
* A new hook lets you extend the *note assertEqual(): bb5. method to
handle new data types. The *note addTypeEqualityFunc(): ce1.
method takes a type object and a function. The function will be
used when both of the objects being compared are of the specified
type. This function should compare the two objects and raise an
exception if they don’t match; it’s a good idea for the function to
provide additional information about why the two objects aren’t
matching, much as the new sequence comparison methods do.
*note unittest.main(): 902. now takes an optional ‘exit’ argument. If
false, *note main(): 902. doesn’t call *note sys.exit(): ce2, allowing
*note main(): 902. to be used from the interactive interpreter.
(Contributed by J. Pablo Fernández; bpo-3379(13).)
*note TestResult: abf. has new *note startTestRun(): ce3. and *note
stopTestRun(): ce4. methods that are called immediately before and after
a test run. (Contributed by Robert Collins; bpo-5728(14).)
With all these changes, the ‘unittest.py’ was becoming awkwardly large,
so the module was turned into a package and the code split into several
files (by Benjamin Peterson). This doesn’t affect how the module is
imported or used.
See also
........
‘http://www.voidspace.org.uk/python/articles/unittest2.shtml’
Describes the new features, how to use them, and the rationale for
various design decisions. (By Michael Foord.)
---------- Footnotes ----------
(1) http://pytest.org
(2) https://nose.readthedocs.io/
(3) https://bugs.python.org/issue6001
(4) https://bugs.python.org/issue8074
(5) https://bugs.python.org/issue1034053
(6) https://bugs.python.org/issue5663
(7) https://bugs.python.org/issue4444
(8) https://bugs.python.org/issue5679
(9) https://bugs.python.org/issue2578
(10) https://bugs.python.org/issue7031
(11) https://bugs.python.org/issue8038
(12) https://bugs.python.org/issue6866
(13) https://bugs.python.org/issue3379
(14) https://bugs.python.org/issue5728
File: python.info, Node: Updated module ElementTree 1 3, Prev: Updated module unittest, Up: New and Improved Modules
1.10.11.5 Updated module: ElementTree 1.3
.........................................
The version of the ElementTree library included with Python was updated
to version 1.3. Some of the new features are:
* The various parsing functions now take a `parser' keyword argument
giving an *note XMLParser: 252. instance that will be used. This
makes it possible to override the file’s internal encoding:
p = ET.XMLParser(encoding='utf-8')
t = ET.XML(""" """, parser=p)
Errors in parsing XML now raise a ‘ParseError’ exception, whose
instances have a ‘position’ attribute containing a (`line',
`column') tuple giving the location of the problem.
* ElementTree’s code for converting trees to a string has been
significantly reworked, making it roughly twice as fast in many
cases. The *note ElementTree.write(): 918. and ‘Element.write()’
methods now have a `method' parameter that can be “xml” (the
default), “html”, or “text”. HTML mode will output empty elements
as ‘ ’ instead of ‘ ’, and text mode will skip
over elements and only output the text chunks. If you set the
‘tag’ attribute of an element to ‘None’ but leave its children in
place, the element will be omitted when the tree is written out, so
you don’t need to do more extensive rearrangement to remove a
single element.
Namespace handling has also been improved. All ‘xmlns:’
declarations are now output on the root element, not scattered
throughout the resulting XML. You can set the default namespace for
a tree by setting the ‘default_namespace’ attribute and can
register new prefixes with *note register_namespace(): b50. In XML
mode, you can use the true/false `xml_declaration' parameter to
suppress the XML declaration.
* New *note Element: ac4. method: *note extend(): b51. appends the
items from a sequence to the element’s children. Elements
themselves behave like sequences, so it’s easy to move children
from one element to another:
from xml.etree import ElementTree as ET
t = ET.XML("""
- 1
- 2
- 3
""")
new = ET.XML(' ')
new.extend(t)
# Outputs - 1
...
print ET.tostring(new)
* New ‘Element’ method: *note iter(): ce7. yields the children of the
element as a generator. It’s also possible to write ‘for child in
elem:’ to loop over an element’s children. The existing method
‘getiterator()’ is now deprecated, as is ‘getchildren()’ which
constructs and returns a list of children.
* New ‘Element’ method: *note itertext(): b53. yields all chunks of
text that are descendants of the element. For example:
t = ET.XML("""
- 1
- 2
- 3
""")
# Outputs ['\n ', '1', ' ', '2', ' ', '3', '\n']
print list(t.itertext())
* Deprecated: using an element as a Boolean (i.e., ‘if elem:’) would
return true if the element had any children, or false if there were
no children. This behaviour is confusing – ‘None’ is false, but so
is a childless element? – so it will now trigger a *note
FutureWarning: 325. In your code, you should be explicit: write
‘len(elem) != 0’ if you’re interested in the number of children, or
‘elem is not None’.
Fredrik Lundh develops ElementTree and produced the 1.3 version; you can
read his article describing 1.3 at
‘http://effbot.org/zone/elementtree-13-intro.htm’. Florent Xicluna
updated the version included with Python, after discussions on
python-dev and in bpo-6472(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue6472
File: python.info, Node: Build and C API Changes<8>, Next: Other Changes and Fixes, Prev: New and Improved Modules, Up: What’s New in Python 2 7
1.10.12 Build and C API Changes
-------------------------------
Changes to Python’s build process and to the C API include:
* The latest release of the GNU Debugger, GDB 7, can be scripted
using Python(1). When you begin debugging an executable program P,
GDB will look for a file named ‘P-gdb.py’ and automatically read
it. Dave Malcolm contributed a ‘python-gdb.py’ that adds a number
of commands useful when debugging Python itself. For example,
‘py-up’ and ‘py-down’ go up or down one Python stack frame, which
usually corresponds to several C stack frames. ‘py-print’ prints
the value of a Python variable, and ‘py-bt’ prints the Python stack
trace. (Added as a result of bpo-8032(2).)
* If you use the ‘.gdbinit’ file provided with Python, the “pyo”
macro in the 2.7 version now works correctly when the thread being
debugged doesn’t hold the GIL; the macro now acquires it before
printing. (Contributed by Victor Stinner; bpo-3632(3).)
* *note Py_AddPendingCall(): ce9. is now thread-safe, letting any
worker thread submit notifications to the main Python thread. This
is particularly useful for asynchronous IO operations.
(Contributed by Kristján Valur Jónsson; bpo-4293(4).)
* New function: *note PyCode_NewEmpty(): cea. creates an empty code
object; only the filename, function name, and first line number are
required. This is useful for extension modules that are attempting
to construct a more useful traceback stack. Previously such
extensions needed to call *note PyCode_New(): 272, which had many
more arguments. (Added by Jeffrey Yasskin.)
* New function: *note PyErr_NewExceptionWithDoc(): bff. creates a new
exception class, just as the existing *note PyErr_NewException():
c00. does, but takes an extra ‘char *’ argument containing the
docstring for the new exception class. (Added by ‘lekma’ on the
Python bug tracker; bpo-7033(5).)
* New function: *note PyFrame_GetLineNumber(): ceb. takes a frame
object and returns the line number that the frame is currently
executing. Previously code would need to get the index of the
bytecode instruction currently executing, and then look up the line
number corresponding to that address. (Added by Jeffrey Yasskin.)
* New functions: *note PyLong_AsLongAndOverflow(): bfd. and *note
PyLong_AsLongLongAndOverflow(): bfc. approximates a Python long
integer as a C ‘long’ or ‘long long’. If the number is too large
to fit into the output type, an `overflow' flag is set and returned
to the caller. (Contributed by Case Van Horsen; bpo-7528(6) and
bpo-7767(7).)
* New function: stemming from the rewrite of string-to-float
conversion, a new *note PyOS_string_to_double(): c23. function was
added. The old ‘PyOS_ascii_strtod()’ and ‘PyOS_ascii_atof()’
functions are now deprecated.
* New function: *note PySys_SetArgvEx(): bfa. sets the value of
‘sys.argv’ and can optionally update ‘sys.path’ to include the
directory containing the script named by ‘sys.argv[0]’ depending on
the value of an `updatepath' parameter.
This function was added to close a security hole for applications
that embed Python. The old function, *note PySys_SetArgv(): cec,
would always update ‘sys.path’, and sometimes it would add the
current directory. This meant that, if you ran an application
embedding Python in a directory controlled by someone else,
attackers could put a Trojan-horse module in the directory (say, a
file named ‘os.py’) that your application would then import and
run.
If you maintain a C/C++ application that embeds Python, check
whether you’re calling *note PySys_SetArgv(): cec. and carefully
consider whether the application should be using *note
PySys_SetArgvEx(): bfa. with `updatepath' set to false.
Security issue reported as CVE-2008-5983(8); discussed in
bpo-5753(9), and fixed by Antoine Pitrou.
* New macros: the Python header files now define the following
macros: ‘Py_ISALNUM’, ‘Py_ISALPHA’, ‘Py_ISDIGIT’, ‘Py_ISLOWER’,
‘Py_ISSPACE’, ‘Py_ISUPPER’, ‘Py_ISXDIGIT’, ‘Py_TOLOWER’, and
‘Py_TOUPPER’. All of these functions are analogous to the C
standard macros for classifying characters, but ignore the current
locale setting, because in several places Python needs to analyze
characters in a locale-independent way. (Added by Eric Smith;
bpo-5793(10).)
* Removed function: ‘PyEval_CallObject’ is now only available as a
macro. A function version was being kept around to preserve ABI
linking compatibility, but that was in 1997; it can certainly be
deleted by now. (Removed by Antoine Pitrou; bpo-8276(11).)
* New format codes: the ‘PyFormat_FromString()’,
‘PyFormat_FromStringV()’, and *note PyErr_Format(): 7aa. functions
now accept ‘%lld’ and ‘%llu’ format codes for displaying C’s ‘long
long’ types. (Contributed by Mark Dickinson; bpo-7228(12).)
* The complicated interaction between threads and process forking has
been changed. Previously, the child process created by *note
os.fork(): 961. might fail because the child is created with only a
single thread running, the thread performing the *note os.fork():
961. If other threads were holding a lock, such as Python’s import
lock, when the fork was performed, the lock would still be marked
as “held” in the new process. But in the child process nothing
would ever release the lock, since the other threads weren’t
replicated, and the child process would no longer be able to
perform imports.
Python 2.7 acquires the import lock before performing an *note
os.fork(): 961, and will also clean up any locks created using the
*note threading: 109. module. C extension modules that have
internal locks, or that call ‘fork()’ themselves, will not benefit
from this clean-up.
(Fixed by Thomas Wouters; bpo-1590864(13).)
* The *note Py_Finalize(): ced. function now calls the internal
‘threading._shutdown()’ function; this prevents some exceptions
from being raised when an interpreter shuts down. (Patch by Adam
Olsen; bpo-1722344(14).)
* When using the *note PyMemberDef: 426. structure to define
attributes of a type, Python will no longer let you try to delete
or set a ‘T_STRING_INPLACE’ attribute.
* Global symbols defined by the *note ctypes: 2b. module are now
prefixed with ‘Py’, or with ‘_ctypes’. (Implemented by Thomas
Heller; bpo-3102(15).)
* New configure option: the ‘--with-system-expat’ switch allows
building the ‘pyexpat’ module to use the system Expat library.
(Contributed by Arfrever Frehtes Taifersar Arahesis; bpo-7609(16).)
* New configure option: the ‘--with-valgrind’ option will now disable
the pymalloc allocator, which is difficult for the Valgrind
memory-error detector to analyze correctly. Valgrind will
therefore be better at detecting memory leaks and overruns.
(Contributed by James Henstridge; bpo-2422(17).)
* New configure option: you can now supply an empty string to
‘--with-dbmliborder=’ in order to disable all of the various DBM
modules. (Added by Arfrever Frehtes Taifersar Arahesis;
bpo-6491(18).)
* The ‘configure’ script now checks for floating-point rounding bugs
on certain 32-bit Intel chips and defines a ‘X87_DOUBLE_ROUNDING’
preprocessor definition. No code currently uses this definition,
but it’s available if anyone wishes to use it. (Added by Mark
Dickinson; bpo-2937(19).)
‘configure’ also now sets a ‘LDCXXSHARED’ Makefile variable for
supporting C++ linking. (Contributed by Arfrever Frehtes Taifersar
Arahesis; bpo-1222585(20).)
* The build process now creates the necessary files for pkg-config
support. (Contributed by Clinton Roy; bpo-3585(21).)
* The build process now supports Subversion 1.7. (Contributed by
Arfrever Frehtes Taifersar Arahesis; bpo-6094(22).)
* Menu:
* Capsules::
* Port-Specific Changes; Windows: Port-Specific Changes Windows.
* Port-Specific Changes; Mac OS X: Port-Specific Changes Mac OS X.
* Port-Specific Changes; FreeBSD: Port-Specific Changes FreeBSD.
---------- Footnotes ----------
(1) https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html
(2) https://bugs.python.org/issue8032
(3) https://bugs.python.org/issue3632
(4) https://bugs.python.org/issue4293
(5) https://bugs.python.org/issue7033
(6) https://bugs.python.org/issue7528
(7) https://bugs.python.org/issue7767
(8) https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983
(9) https://bugs.python.org/issue5753
(10) https://bugs.python.org/issue5793
(11) https://bugs.python.org/issue8276
(12) https://bugs.python.org/issue7228
(13) https://bugs.python.org/issue1590864
(14) https://bugs.python.org/issue1722344
(15) https://bugs.python.org/issue3102
(16) https://bugs.python.org/issue7609
(17) https://bugs.python.org/issue2422
(18) https://bugs.python.org/issue6491
(19) https://bugs.python.org/issue2937
(20) https://bugs.python.org/issue1222585
(21) https://bugs.python.org/issue3585
(22) https://bugs.python.org/issue6094
File: python.info, Node: Capsules, Next: Port-Specific Changes Windows, Up: Build and C API Changes<8>
1.10.12.1 Capsules
..................
Python 3.1 adds a new C datatype, *note PyCapsule: c07, for providing a
C API to an extension module. A capsule is essentially the holder of a
C ‘void *’ pointer, and is made available as a module attribute; for
example, the *note socket: ef. module’s API is exposed as ‘socket.CAPI’,
and *note unicodedata: 11a. exposes ‘ucnhash_CAPI’. Other extensions
can import the module, access its dictionary to get the capsule object,
and then get the ‘void *’ pointer, which will usually point to an array
of pointers to the module’s various API functions.
There is an existing data type already used for this, ‘PyCObject’, but
it doesn’t provide type safety. Evil code written in pure Python could
cause a segmentation fault by taking a ‘PyCObject’ from module A and
somehow substituting it for the ‘PyCObject’ in module B. Capsules know
their own name, and getting the pointer requires providing the name:
void *vtable;
if (!PyCapsule_IsValid(capsule, "mymodule.CAPI") {
PyErr_SetString(PyExc_ValueError, "argument type invalid");
return NULL;
}
vtable = PyCapsule_GetPointer(capsule, "mymodule.CAPI");
You are assured that ‘vtable’ points to whatever you’re expecting. If a
different capsule was passed in, *note PyCapsule_IsValid(): cf0. would
detect the mismatched name and return false. Refer to *note Providing a
C API for an Extension Module: cf1. for more information on using these
objects.
Python 2.7 now uses capsules internally to provide various
extension-module APIs, but the ‘PyCObject_AsVoidPtr()’ was modified to
handle capsules, preserving compile-time compatibility with the
‘CObject’ interface. Use of ‘PyCObject_AsVoidPtr()’ will signal a *note
PendingDeprecationWarning: 279, which is silent by default.
Implemented in Python 3.1 and backported to 2.7 by Larry Hastings;
discussed in bpo-5630(1).
---------- Footnotes ----------
(1) https://bugs.python.org/issue5630
File: python.info, Node: Port-Specific Changes Windows, Next: Port-Specific Changes Mac OS X, Prev: Capsules, Up: Build and C API Changes<8>
1.10.12.2 Port-Specific Changes: Windows
........................................
* The *note msvcrt: b6. module now contains some constants from the
‘crtassem.h’ header file: ‘CRT_ASSEMBLY_VERSION’,
‘VC_ASSEMBLY_PUBLICKEYTOKEN’, and ‘LIBRARIES_ASSEMBLY_NAME_PREFIX’.
(Contributed by David Cournapeau; bpo-4365(1).)
* The ‘_winreg’ module for accessing the registry now implements the
‘CreateKeyEx()’ and ‘DeleteKeyEx()’ functions, extended versions of
previously-supported functions that take several extra arguments.
The ‘DisableReflectionKey()’, ‘EnableReflectionKey()’, and
‘QueryReflectionKey()’ were also tested and documented.
(Implemented by Brian Curtin: bpo-7347(2).)
* The new ‘_beginthreadex()’ API is used to start threads, and the
native thread-local storage functions are now used. (Contributed
by Kristján Valur Jónsson; bpo-3582(3).)
* The *note os.kill(): cf3. function now works on Windows. The
signal value can be the constants ‘CTRL_C_EVENT’,
‘CTRL_BREAK_EVENT’, or any integer. The first two constants will
send ‘Control-C’ and ‘Control-Break’ keystroke events to
subprocesses; any other value will use the ‘TerminateProcess()’
API. (Contributed by Miki Tebeka; bpo-1220212(4).)
* The *note os.listdir(): a41. function now correctly fails for an
empty path. (Fixed by Hirokazu Yamamoto; bpo-5913(5).)
* The ‘mimelib’ module will now read the MIME database from the
Windows registry when initializing. (Patch by Gabriel Genellina;
bpo-4969(6).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue4365
(2) https://bugs.python.org/issue7347
(3) https://bugs.python.org/issue3582
(4) https://bugs.python.org/issue1220212
(5) https://bugs.python.org/issue5913
(6) https://bugs.python.org/issue4969
File: python.info, Node: Port-Specific Changes Mac OS X, Next: Port-Specific Changes FreeBSD, Prev: Port-Specific Changes Windows, Up: Build and C API Changes<8>
1.10.12.3 Port-Specific Changes: Mac OS X
.........................................
* The path ‘/Library/Python/2.7/site-packages’ is now appended to
‘sys.path’, in order to share added packages between the system
installation and a user-installed copy of the same version.
(Changed by Ronald Oussoren; bpo-4865(1).)
Changed in version 2.7.13: As of 2.7.13, this change was
removed. ‘/Library/Python/2.7/site-packages’, the
site-packages directory used by the Apple-supplied system
Python 2.7 is no longer appended to ‘sys.path’ for
user-installed Pythons such as from the python.org installers.
As of macOS 10.12, Apple changed how the system site-packages
directory is configured, which could cause installation of pip
components, like setuptools, to fail. Packages installed for
the system Python will no longer be shared with user-installed
Pythons. (bpo-28440(2))
---------- Footnotes ----------
(1) https://bugs.python.org/issue4865
(2) https://bugs.python.org/issue28440
File: python.info, Node: Port-Specific Changes FreeBSD, Prev: Port-Specific Changes Mac OS X, Up: Build and C API Changes<8>
1.10.12.4 Port-Specific Changes: FreeBSD
........................................
* FreeBSD 7.1’s ‘SO_SETFIB’ constant, used with
‘getsockopt()’/‘setsockopt()’ to select an alternate routing table,
is now available in the *note socket: ef. module. (Added by Kyle
VanderBeek; bpo-8235(1).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue8235
File: python.info, Node: Other Changes and Fixes, Next: Porting to Python 2 7, Prev: Build and C API Changes<8>, Up: What’s New in Python 2 7
1.10.13 Other Changes and Fixes
-------------------------------
* Two benchmark scripts, ‘iobench’ and ‘ccbench’, were added to the
‘Tools’ directory. ‘iobench’ measures the speed of the built-in
file I/O objects returned by *note open(): 4f0. while performing
various operations, and ‘ccbench’ is a concurrency benchmark that
tries to measure computing throughput, thread switching latency,
and IO processing bandwidth when performing several tasks using a
varying number of threads.
* The ‘Tools/i18n/msgfmt.py’ script now understands plural forms in
‘.po’ files. (Fixed by Martin von Löwis; bpo-5464(1).)
* When importing a module from a ‘.pyc’ or ‘.pyo’ file with an
existing ‘.py’ counterpart, the ‘co_filename’ attributes of the
resulting code objects are overwritten when the original filename
is obsolete. This can happen if the file has been renamed, moved,
or is accessed through different paths. (Patch by Ziga Seilnacht
and Jean-Paul Calderone; bpo-1180193(2).)
* The ‘regrtest.py’ script now takes a ‘--randseed=’ switch that
takes an integer that will be used as the random seed for the ‘-r’
option that executes tests in random order. The ‘-r’ option also
reports the seed that was used (Added by Collin Winter.)
* Another ‘regrtest.py’ switch is ‘-j’, which takes an integer
specifying how many tests run in parallel. This allows reducing
the total runtime on multi-core machines. This option is
compatible with several other options, including the ‘-R’ switch
which is known to produce long runtimes. (Added by Antoine Pitrou,
bpo-6152(3).) This can also be used with a new ‘-F’ switch that
runs selected tests in a loop until they fail. (Added by Antoine
Pitrou; bpo-7312(4).)
* When executed as a script, the ‘py_compile.py’ module now accepts
‘'-'’ as an argument, which will read standard input for the list
of filenames to be compiled. (Contributed by Piotr Ożarowski;
bpo-8233(5).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue5464
(2) https://bugs.python.org/issue1180193
(3) https://bugs.python.org/issue6152
(4) https://bugs.python.org/issue7312
(5) https://bugs.python.org/issue8233
File: python.info, Node: Porting to Python 2 7, Next: New Features Added to Python 2 7 Maintenance Releases, Prev: Other Changes and Fixes, Up: What’s New in Python 2 7
1.10.14 Porting to Python 2.7
-----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code:
* The *note range(): 9be. function processes its arguments more
consistently; it will now call *note __int__(): 18b. on non-float,
non-integer arguments that are supplied to it. (Fixed by Alexander
Belopolsky; bpo-1533(1).)
* The string *note format(): 4db. method changed the default
precision used for floating-point and complex numbers from 6
decimal places to 12, which matches the precision used by *note
str(): 330. (Changed by Eric Smith; bpo-5920(2).)
* Because of an optimization for the *note with: 6e9. statement, the
special methods *note __enter__(): c95. and *note __exit__(): c96.
must belong to the object’s type, and cannot be directly attached
to the object’s instance. This affects new-style classes (derived
from *note object: 2b0.) and C extension types. (bpo-6101(3).)
* Due to a bug in Python 2.6, the `exc_value' parameter to *note
__exit__(): c96. methods was often the string representation of the
exception, not an instance. This was fixed in 2.7, so `exc_value'
will be an instance as expected. (Fixed by Florent Xicluna;
bpo-7853(4).)
* When a restricted set of attributes were set using ‘__slots__’,
deleting an unset attribute would not raise *note AttributeError:
39b. as you would expect. Fixed by Benjamin Peterson;
bpo-7604(5).)
In the standard library:
* Operations with *note datetime: 194. instances that resulted in a
year falling outside the supported range didn’t always raise *note
OverflowError: 960. Such errors are now checked more carefully and
will now raise the exception. (Reported by Mark Leander, patch by
Anand B. Pillai and Alexander Belopolsky; bpo-7150(6).)
* When using *note Decimal: 188. instances with a string’s *note
format(): 4db. method, the default alignment was previously
left-alignment. This has been changed to right-alignment, which
might change the output of your programs. (Changed by Mark
Dickinson; bpo-6857(7).)
Comparisons involving a signaling NaN value (or ‘sNAN’) now signal
*note InvalidOperation: 9eb. instead of silently returning a true
or false value depending on the comparison operator. Quiet NaN
values (or ‘NaN’) are now hashable. (Fixed by Mark Dickinson;
bpo-7279(8).)
* The ElementTree library, ‘xml.etree’, no longer escapes ampersands
and angle brackets when outputting an XML processing instruction
(which looks like ‘’) or comment
(which looks like ‘’). (Patch by Neil Muller;
bpo-2746(9).)
* The ‘readline()’ method of ‘StringIO’ objects now does nothing when
a negative length is requested, as other file-like objects do.
(bpo-7348(10)).
* The *note syslog: ff. module will now use the value of
‘sys.argv[0]’ as the identifier instead of the previous default
value of ‘'python'’. (Changed by Sean Reifschneider;
bpo-8451(11).)
* The *note tarfile: 101. module’s default error handling has
changed, to no longer suppress fatal errors. The default error
level was previously 0, which meant that errors would only result
in a message being written to the debug log, but because the debug
log is not activated by default, these errors go unnoticed. The
default error level is now 1, which raises an exception if there’s
an error. (Changed by Lars Gustäbel; bpo-7357(12).)
* The ‘urlparse’ module’s ‘urlsplit()’ now handles unknown URL
schemes in a fashion compliant with RFC 3986(13): if the URL is of
the form ‘"://..."’, the text before the ‘://’ is
treated as the scheme, even if it’s a made-up scheme that the
module doesn’t know about. This change may break code that worked
around the old behaviour. For example, Python 2.6.4 or 2.5 will
return the following:
>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', '', '//host/filename?query', '', '')
Python 2.7 (and Python 2.6.5) will return:
>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', 'host', '/filename?query', '', '')
(Python 2.7 actually produces slightly different output, since it
returns a named tuple instead of a standard tuple.)
For C extensions:
* C extensions that use integer format codes with the ‘PyArg_Parse*’
family of functions will now raise a *note TypeError: 192.
exception instead of triggering a *note DeprecationWarning: 278.
(bpo-5080(14)).
* Use the new *note PyOS_string_to_double(): c23. function instead of
the old ‘PyOS_ascii_strtod()’ and ‘PyOS_ascii_atof()’ functions,
which are now deprecated.
For applications that embed Python:
* The *note PySys_SetArgvEx(): bfa. function was added, letting
applications close a security hole when the existing *note
PySys_SetArgv(): cec. function was used. Check whether you’re
calling *note PySys_SetArgv(): cec. and carefully consider whether
the application should be using *note PySys_SetArgvEx(): bfa. with
`updatepath' set to false.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1533
(2) https://bugs.python.org/issue5920
(3) https://bugs.python.org/issue6101
(4) https://bugs.python.org/issue7853
(5) https://bugs.python.org/issue7604
(6) https://bugs.python.org/issue7150
(7) https://bugs.python.org/issue6857
(8) https://bugs.python.org/issue7279
(9) https://bugs.python.org/issue2746
(10) https://bugs.python.org/issue7348
(11) https://bugs.python.org/issue8451
(12) https://bugs.python.org/issue7357
(13) https://tools.ietf.org/html/rfc3986.html
(14) https://bugs.python.org/issue5080
File: python.info, Node: New Features Added to Python 2 7 Maintenance Releases, Next: Acknowledgements, Prev: Porting to Python 2 7, Up: What’s New in Python 2 7
1.10.15 New Features Added to Python 2.7 Maintenance Releases
-------------------------------------------------------------
New features may be added to Python 2.7 maintenance releases when the
situation genuinely calls for it. Any such additions must go through
the Python Enhancement Proposal process, and make a compelling case for
why they can’t be adequately addressed by either adding the new feature
solely to Python 3, or else by publishing it on the Python Package
Index.
In addition to the specific proposals listed below, there is a general
exemption allowing new ‘-3’ warnings to be added in any Python 2.7
maintenance release.
* Menu:
* Two new environment variables for debug mode::
* PEP 434; IDLE Enhancement Exception for All Branches: PEP 434 IDLE Enhancement Exception for All Branches.
* PEP 466; Network Security Enhancements for Python 2.7: PEP 466 Network Security Enhancements for Python 2 7.
* PEP 477; Backport ensurepip (PEP 453) to Python 2.7: PEP 477 Backport ensurepip PEP 453 to Python 2 7.
* PEP 476; Enabling certificate verification by default for stdlib http clients: PEP 476 Enabling certificate verification by default for stdlib http clients<2>.
* PEP 493; HTTPS verification migration tools for Python 2.7: PEP 493 HTTPS verification migration tools for Python 2 7.
* New make regen-all build target: New make regen-all build target<3>.
* Removal of make touch build target: Removal of make touch build target<3>.
File: python.info, Node: Two new environment variables for debug mode, Next: PEP 434 IDLE Enhancement Exception for All Branches, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.1 Two new environment variables for debug mode
......................................................
In debug mode, the ‘[xxx refs]’ statistic is not written by default, the
‘PYTHONSHOWREFCOUNT’ environment variable now must also be set.
(Contributed by Victor Stinner; bpo-31733(1).)
When Python is compiled with ‘COUNT_ALLOC’ defined, allocation counts
are no longer dumped by default anymore: the ‘PYTHONSHOWALLOCCOUNT’
environment variable must now also be set. Moreover, allocation counts
are now dumped into stderr, rather than stdout. (Contributed by Victor
Stinner; bpo-31692(2).)
New in version 2.7.15.
---------- Footnotes ----------
(1) https://bugs.python.org/issue31733
(2) https://bugs.python.org/issue31692
File: python.info, Node: PEP 434 IDLE Enhancement Exception for All Branches, Next: PEP 466 Network Security Enhancements for Python 2 7, Prev: Two new environment variables for debug mode, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.2 PEP 434: IDLE Enhancement Exception for All Branches
..............................................................
PEP 434(1) describes a general exemption for changes made to the IDLE
development environment shipped along with Python. This exemption makes
it possible for the IDLE developers to provide a more consistent user
experience across all supported versions of Python 2 and 3.
For details of any IDLE changes, refer to the NEWS file for the specific
release.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0434
File: python.info, Node: PEP 466 Network Security Enhancements for Python 2 7, Next: PEP 477 Backport ensurepip PEP 453 to Python 2 7, Prev: PEP 434 IDLE Enhancement Exception for All Branches, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.3 PEP 466: Network Security Enhancements for Python 2.7
...............................................................
PEP 466(1) describes a number of network security enhancement proposals
that have been approved for inclusion in Python 2.7 maintenance
releases, with the first of those changes appearing in the Python 2.7.7
release.
PEP 466(2) related features added in Python 2.7.7:
* *note hmac.compare_digest(): a0c. was backported from Python 3 to
make a timing attack resistant comparison operation available to
Python 2 applications. (Contributed by Alex Gaynor; bpo-21306(3).)
* OpenSSL 1.0.1g was upgraded in the official Windows installers
published on python.org. (Contributed by Zachary Ware;
bpo-21462(4).)
PEP 466(5) related features added in Python 2.7.8:
* *note hashlib.pbkdf2_hmac(): 7e6. was backported from Python 3 to
make a hashing algorithm suitable for secure password storage
broadly available to Python 2 applications. (Contributed by Alex
Gaynor; bpo-21304(6).)
* OpenSSL 1.0.1h was upgraded for the official Windows installers
published on python.org. (contributed by Zachary Ware in
bpo-21671(7) for CVE-2014-0224)
PEP 466(8) related features added in Python 2.7.9:
* Most of Python 3.4’s *note ssl: f3. module was backported. This
means *note ssl: f3. now supports Server Name Indication, TLS1.x
settings, access to the platform certificate store, the *note
SSLContext: 3e4. class, and other features. (Contributed by Alex
Gaynor and David Reid; bpo-21308(9).)
Refer to the “Version added: 2.7.9” notes in the module
documentation for specific details.
* *note os.urandom(): 4d1. was changed to cache a file descriptor to
‘/dev/urandom’ instead of reopening ‘/dev/urandom’ on every call.
(Contributed by Alex Gaynor; bpo-21305(10).)
* *note hashlib.algorithms_guaranteed: cfc. and *note
hashlib.algorithms_available: cfd. were backported from Python 3 to
make it easier for Python 2 applications to select the strongest
available hash algorithm. (Contributed by Alex Gaynor in
bpo-21307(11))
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0466
(2) https://www.python.org/dev/peps/pep-0466
(3) https://bugs.python.org/issue21306
(4) https://bugs.python.org/issue21462
(5) https://www.python.org/dev/peps/pep-0466
(6) https://bugs.python.org/issue21304
(7) https://bugs.python.org/issue21671
(8) https://www.python.org/dev/peps/pep-0466
(9) https://bugs.python.org/issue21308
(10) https://bugs.python.org/issue21305
(11) https://bugs.python.org/issue21307
File: python.info, Node: PEP 477 Backport ensurepip PEP 453 to Python 2 7, Next: PEP 476 Enabling certificate verification by default for stdlib http clients<2>, Prev: PEP 466 Network Security Enhancements for Python 2 7, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.4 PEP 477: Backport ensurepip (PEP 453) to Python 2.7
.............................................................
PEP 477(1) approves the inclusion of the PEP 453(2) ensurepip module and
the improved documentation that was enabled by it in the Python 2.7
maintenance releases, appearing first in the Python 2.7.9 release.
* Menu:
* Bootstrapping pip By Default: Bootstrapping pip By Default<2>.
* Documentation Changes: Documentation Changes<2>.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0477
(2) https://www.python.org/dev/peps/pep-0453
File: python.info, Node: Bootstrapping pip By Default<2>, Next: Documentation Changes<2>, Up: PEP 477 Backport ensurepip PEP 453 to Python 2 7
1.10.15.5 Bootstrapping pip By Default
......................................
The new *note ensurepip: 7a. module (defined in PEP 453(1)) provides a
standard cross-platform mechanism to bootstrap the pip installer into
Python installations. The version of ‘pip’ included with Python 2.7.9
is ‘pip’ 1.5.6, and future 2.7.x maintenance releases will update the
bundled version to the latest version of ‘pip’ that is available at the
time of creating the release candidate.
By default, the commands ‘pip’, ‘pipX’ and ‘pipX.Y’ will be installed on
all platforms (where X.Y stands for the version of the Python
installation), along with the ‘pip’ Python package and its dependencies.
For CPython *note source builds on POSIX systems: 7f3, the ‘make
install’ and ‘make altinstall’ commands do not bootstrap ‘pip’ by
default. This behaviour can be controlled through configure options,
and overridden through Makefile options.
On Windows and Mac OS X, the CPython installers now default to
installing ‘pip’ along with CPython itself (users may opt out of
installing it during the installation process). Window users will need
to opt in to the automatic ‘PATH’ modifications to have ‘pip’ available
from the command line by default, otherwise it can still be accessed
through the Python launcher for Windows as ‘py -m pip’.
As discussed in the PEP(2), platform packagers may choose not to install
these commands by default, as long as, when invoked, they provide clear
and simple directions on how to install them on that platform (usually
using the system package manager).
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0453
(2)
https://www.python.org/dev/peps/pep-0477/#disabling-ensurepip-by-downstream-distributors
File: python.info, Node: Documentation Changes<2>, Prev: Bootstrapping pip By Default<2>, Up: PEP 477 Backport ensurepip PEP 453 to Python 2 7
1.10.15.6 Documentation Changes
...............................
As part of this change, the *note Installing Python Modules: 7f5. and
*note Distributing Python Modules: 7f6. sections of the documentation
have been completely redesigned as short getting started and FAQ
documents. Most packaging documentation has now been moved out to the
Python Packaging Authority maintained Python Packaging User Guide(1) and
the documentation of the individual projects.
However, as this migration is currently still incomplete, the legacy
versions of those guides remaining available as *note Installing Python
Modules (Legacy version): 7f7. and *note Distributing Python Modules
(Legacy version): 7f8.
See also
........
PEP 453(2) – Explicit bootstrapping of pip in Python installations
PEP written by Donald Stufft and Nick Coghlan, implemented by
Donald Stufft, Nick Coghlan, Martin von Löwis and Ned Deily.
---------- Footnotes ----------
(1) http://packaging.python.org
(2) https://www.python.org/dev/peps/pep-0453
File: python.info, Node: PEP 476 Enabling certificate verification by default for stdlib http clients<2>, Next: PEP 493 HTTPS verification migration tools for Python 2 7, Prev: PEP 477 Backport ensurepip PEP 453 to Python 2 7, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.7 PEP 476: Enabling certificate verification by default for stdlib http clients
.......................................................................................
PEP 476(1) updated ‘httplib’ and modules which use it, such as ‘urllib2’
and ‘xmlrpclib’, to now verify that the server presents a certificate
which is signed by a Certificate Authority in the platform trust store
and whose hostname matches the hostname being requested by default,
significantly improving security for many applications. This change was
made in the Python 2.7.9 release.
For applications which require the old previous behavior, they can pass
an alternate context:
import urllib2
import ssl
# This disables all verification
context = ssl._create_unverified_context()
# This allows using a specific certificate for the host, which doesn't need
# to be in the trust store
context = ssl.create_default_context(cafile="/path/to/file.crt")
urllib2.urlopen("https://invalid-cert", context=context)
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0476
File: python.info, Node: PEP 493 HTTPS verification migration tools for Python 2 7, Next: New make regen-all build target<3>, Prev: PEP 476 Enabling certificate verification by default for stdlib http clients<2>, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.8 PEP 493: HTTPS verification migration tools for Python 2.7
....................................................................
PEP 493(1) provides additional migration tools to support a more
incremental infrastructure upgrade process for environments containing
applications and services relying on the historically permissive
processing of server certificates when establishing client HTTPS
connections. These additions were made in the Python 2.7.12 release.
These tools are intended for use in cases where affected applications
and services can’t be modified to explicitly pass a more permissive SSL
context when establishing the connection.
For applications and services which can’t be modified at all, the new
‘PYTHONHTTPSVERIFY’ environment variable may be set to ‘0’ to revert an
entire Python process back to the default permissive behaviour of Python
2.7.8 and earlier.
For cases where the connection establishment code can’t be modified, but
the overall application can be, the new
‘ssl._https_verify_certificates()’ function can be used to adjust the
default behaviour at runtime.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0493
File: python.info, Node: New make regen-all build target<3>, Next: Removal of make touch build target<3>, Prev: PEP 493 HTTPS verification migration tools for Python 2 7, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.9 New ‘make regen-all’ build target
...........................................
To simplify cross-compilation, and to ensure that CPython can reliably
be compiled without requiring an existing version of Python to already
be available, the autotools-based build system no longer attempts to
implicitly recompile generated files based on file modification times.
Instead, a new ‘make regen-all’ command has been added to force
regeneration of these files when desired (e.g. after an initial version
of Python has already been built based on the pregenerated versions).
More selective regeneration targets are also defined - see
Makefile.pre.in(1) for details.
(Contributed by Victor Stinner in bpo-23404(2).)
New in version 2.7.14.
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Makefile.pre.in
(2) https://bugs.python.org/issue23404
File: python.info, Node: Removal of make touch build target<3>, Prev: New make regen-all build target<3>, Up: New Features Added to Python 2 7 Maintenance Releases
1.10.15.10 Removal of ‘make touch’ build target
...............................................
The ‘make touch’ build target previously used to request implicit
regeneration of generated files by updating their modification times has
been removed.
It has been replaced by the new ‘make regen-all’ target.
(Contributed by Victor Stinner in bpo-23404(1).)
Changed in version 2.7.14.
---------- Footnotes ----------
(1) https://bugs.python.org/issue23404
File: python.info, Node: Acknowledgements, Prev: New Features Added to Python 2 7 Maintenance Releases, Up: What’s New in Python 2 7
1.10.16 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Nick Coghlan, Philip Jenvey, Ryan Lovett, R. David Murray, Hugh
Secker-Walker.
File: python.info, Node: What’s New in Python 2 6, Next: What’s New in Python 2 5, Prev: What’s New in Python 2 7, Up: What’s New in Python
1.11 What’s New in Python 2.6
=============================
Author: A.M. Kuchling (amk at amk.ca)
This article explains the new features in Python 2.6, released on
October 1 2008. The release schedule is described in PEP 361(1).
The major theme of Python 2.6 is preparing the migration path to Python
3.0, a major redesign of the language. Whenever possible, Python 2.6
incorporates new features and syntax from 3.0 while remaining compatible
with existing code by not removing older features or syntax. When it’s
not possible to do that, Python 2.6 tries to do what it can, adding
compatibility functions in a ‘future_builtins’ module and a ‘-3’ switch
to warn about usages that will become unsupported in 3.0.
Some significant new packages have been added to the standard library,
such as the *note multiprocessing: b7. and *note json: a4. modules, but
there aren’t many new features that aren’t related to Python 3.0 in some
way.
Python 2.6 also sees a number of improvements and bugfixes throughout
the source. A search through the change logs finds there were 259
patches applied and 612 bugs fixed between Python 2.5 and 2.6. Both
figures are likely to be underestimates.
This article doesn’t attempt to provide a complete specification of the
new features, but instead provides a convenient overview. For full
details, you should refer to the documentation for Python 2.6. If you
want to understand the rationale for the design and implementation,
refer to the PEP for a particular new feature. Whenever possible,
“What’s New in Python” links to the bug/patch item for each change.
* Menu:
* Python 3.0: Python 3 0.
* Changes to the Development Process::
* PEP 343; The ‘with’ statement: PEP 343 The ‘with’ statement.
* PEP 366; Explicit Relative Imports From a Main Module: PEP 366 Explicit Relative Imports From a Main Module.
* PEP 370; Per-user site-packages Directory: PEP 370 Per-user site-packages Directory.
* PEP 371; The multiprocessing Package: PEP 371 The multiprocessing Package.
* PEP 3101; Advanced String Formatting: PEP 3101 Advanced String Formatting.
* PEP 3105; print As a Function: PEP 3105 print As a Function.
* PEP 3110; Exception-Handling Changes: PEP 3110 Exception-Handling Changes.
* PEP 3112; Byte Literals: PEP 3112 Byte Literals.
* PEP 3116; New I/O Library: PEP 3116 New I/O Library.
* PEP 3118; Revised Buffer Protocol: PEP 3118 Revised Buffer Protocol.
* PEP 3119; Abstract Base Classes: PEP 3119 Abstract Base Classes.
* PEP 3127; Integer Literal Support and Syntax: PEP 3127 Integer Literal Support and Syntax.
* PEP 3129; Class Decorators: PEP 3129 Class Decorators.
* PEP 3141; A Type Hierarchy for Numbers: PEP 3141 A Type Hierarchy for Numbers.
* Other Language Changes: Other Language Changes<10>.
* New and Improved Modules: New and Improved Modules<2>.
* Deprecations and Removals::
* Build and C API Changes: Build and C API Changes<9>.
* Porting to Python 2.6: Porting to Python 2 6.
* Acknowledgements: Acknowledgements<2>.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0361
File: python.info, Node: Python 3 0, Next: Changes to the Development Process, Up: What’s New in Python 2 6
1.11.1 Python 3.0
-----------------
The development cycle for Python versions 2.6 and 3.0 was synchronized,
with the alpha and beta releases for both versions being made on the
same days. The development of 3.0 has influenced many features in 2.6.
Python 3.0 is a far-ranging redesign of Python that breaks compatibility
with the 2.x series. This means that existing Python code will need
some conversion in order to run on Python 3.0. However, not all the
changes in 3.0 necessarily break compatibility. In cases where new
features won’t cause existing code to break, they’ve been backported to
2.6 and are described in this document in the appropriate place. Some
of the 3.0-derived features are:
* A *note __complex__(): 18d. method for converting objects to a
complex number.
* Alternate syntax for catching exceptions: ‘except TypeError as
exc’.
* The addition of *note functools.reduce(): c6f. as a synonym for the
built-in ‘reduce()’ function.
Python 3.0 adds several new built-in functions and changes the semantics
of some existing builtins. Functions that are new in 3.0 such as *note
bin(): c40. have simply been added to Python 2.6, but existing builtins
haven’t been changed; instead, the ‘future_builtins’ module has versions
with the new 3.0 semantics. Code written to be compatible with 3.0 can
do ‘from future_builtins import hex, map’ as necessary.
A new command-line switch, ‘-3’, enables warnings about features that
will be removed in Python 3.0. You can run code with this switch to see
how much work will be necessary to port code to 3.0. The value of this
switch is available to Python code as the boolean variable
‘sys.py3kwarning’, and to C extension code as ‘Py_Py3kWarningFlag’.
See also
........
The 3xxx series of PEPs, which contains proposals for Python 3.0. PEP
3000(1) describes the development process for Python 3.0. Start with
PEP 3100(2) that describes the general goals for Python 3.0, and then
explore the higher-numbered PEPS that propose specific features.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3000
(2) https://www.python.org/dev/peps/pep-3100
File: python.info, Node: Changes to the Development Process, Next: PEP 343 The ‘with’ statement, Prev: Python 3 0, Up: What’s New in Python 2 6
1.11.2 Changes to the Development Process
-----------------------------------------
While 2.6 was being developed, the Python development process underwent
two significant changes: we switched from SourceForge’s issue tracker to
a customized Roundup installation, and the documentation was converted
from LaTeX to reStructuredText.
* Menu:
* New Issue Tracker; Roundup: New Issue Tracker Roundup.
* New Documentation Format; reStructuredText Using Sphinx: New Documentation Format reStructuredText Using Sphinx.
File: python.info, Node: New Issue Tracker Roundup, Next: New Documentation Format reStructuredText Using Sphinx, Up: Changes to the Development Process
1.11.2.1 New Issue Tracker: Roundup
...................................
For a long time, the Python developers had been growing increasingly
annoyed by SourceForge’s bug tracker. SourceForge’s hosted solution
doesn’t permit much customization; for example, it wasn’t possible to
customize the life cycle of issues.
The infrastructure committee of the Python Software Foundation therefore
posted a call for issue trackers, asking volunteers to set up different
products and import some of the bugs and patches from SourceForge. Four
different trackers were examined: Jira(1), Launchpad(2), Roundup(3), and
Trac(4). The committee eventually settled on Jira and Roundup as the
two candidates. Jira is a commercial product that offers no-cost hosted
instances to free-software projects; Roundup is an open-source project
that requires volunteers to administer it and a server to host it.
After posting a call for volunteers, a new Roundup installation was set
up at ‘https://bugs.python.org’. One installation of Roundup can host
multiple trackers, and this server now also hosts issue trackers for
Jython and for the Python web site. It will surely find other uses in
the future. Where possible, this edition of “What’s New in Python”
links to the bug/patch item for each change.
Hosting of the Python bug tracker is kindly provided by Upfront
Systems(5) of Stellenbosch, South Africa. Martin von Löwis put a lot of
effort into importing existing bugs and patches from SourceForge; his
scripts for this import operation are at
‘http://svn.python.org/view/tracker/importer/’ and may be useful to
other projects wishing to move from SourceForge to Roundup.
See also
........
‘https://bugs.python.org’
The Python bug tracker.
‘http://bugs.jython.org’:
The Jython bug tracker.
‘http://roundup.sourceforge.net/’
Roundup downloads and documentation.
‘http://svn.python.org/view/tracker/importer/’
Martin von Löwis’s conversion scripts.
---------- Footnotes ----------
(1) https://www.atlassian.com/software/jira/
(2) https://launchpad.net/
(3) http://roundup.sourceforge.net/
(4) https://trac.edgewall.org/
(5) http://www.upfrontsoftware.co.za
File: python.info, Node: New Documentation Format reStructuredText Using Sphinx, Prev: New Issue Tracker Roundup, Up: Changes to the Development Process
1.11.2.2 New Documentation Format: reStructuredText Using Sphinx
................................................................
The Python documentation was written using LaTeX since the project
started around 1989. In the 1980s and early 1990s, most documentation
was printed out for later study, not viewed online. LaTeX was widely
used because it provided attractive printed output while remaining
straightforward to write once the basic rules of the markup were
learned.
Today LaTeX is still used for writing publications destined for
printing, but the landscape for programming tools has shifted. We no
longer print out reams of documentation; instead, we browse through it
online and HTML has become the most important format to support.
Unfortunately, converting LaTeX to HTML is fairly complicated and Fred
L. Drake Jr., the long-time Python documentation editor, spent a lot of
time maintaining the conversion process. Occasionally people would
suggest converting the documentation into SGML and later XML, but
performing a good conversion is a major task and no one ever committed
the time required to finish the job.
During the 2.6 development cycle, Georg Brandl put a lot of effort into
building a new toolchain for processing the documentation. The
resulting package is called Sphinx, and is available from
‘http://sphinx-doc.org/’.
Sphinx concentrates on HTML output, producing attractively styled and
modern HTML; printed output is still supported through conversion to
LaTeX. The input format is reStructuredText, a markup syntax supporting
custom extensions and directives that is commonly used in the Python
community.
Sphinx is a standalone package that can be used for writing, and almost
two dozen other projects (listed on the Sphinx web site(1)) have adopted
Sphinx as their documentation tool.
See also
........
Documenting Python(2)
Describes how to write for Python’s documentation.
Sphinx(3)
Documentation and code for the Sphinx toolchain.
Docutils(4)
The underlying reStructuredText parser and toolset.
---------- Footnotes ----------
(1) https://www.sphinx-doc.org/en/master/examples.html
(2) https://devguide.python.org/documenting/
(3) http://sphinx-doc.org/
(4) http://docutils.sourceforge.net
File: python.info, Node: PEP 343 The ‘with’ statement, Next: PEP 366 Explicit Relative Imports From a Main Module, Prev: Changes to the Development Process, Up: What’s New in Python 2 6
1.11.3 PEP 343: The ‘with’ statement
------------------------------------
The previous version, Python 2.5, added the ‘*note with: 6e9.’ statement
as an optional feature, to be enabled by a ‘from __future__ import
with_statement’ directive. In 2.6 the statement no longer needs to be
specially enabled; this means that ‘with’ is now always a keyword. The
rest of this section is a copy of the corresponding section from the
“What’s New in Python 2.5” document; if you’re familiar with the
‘‘with’’ statement from Python 2.5, you can skip this section.
The ‘*note with: 6e9.’ statement clarifies code that previously would
use ‘try...finally’ blocks to ensure that clean-up code is executed. In
this section, I’ll discuss the statement as it will commonly be used.
In the next section, I’ll examine the implementation details and show
how to write objects for use with this statement.
The ‘*note with: 6e9.’ statement is a control-flow structure whose basic
structure is:
with expression [as variable]:
with-block
The expression is evaluated, and it should result in an object that
supports the context management protocol (that is, has *note
__enter__(): c95. and *note __exit__(): c96. methods).
The object’s *note __enter__(): c95. is called before `with-block' is
executed and therefore can run set-up code. It also may return a value
that is bound to the name `variable', if given. (Note carefully that
`variable' is `not' assigned the result of `expression'.)
After execution of the `with-block' is finished, the object’s *note
__exit__(): c96. method is called, even if the block raised an
exception, and can therefore run clean-up code.
Some standard Python objects now support the context management protocol
and can be used with the ‘*note with: 6e9.’ statement. File objects are
one example:
with open('/etc/passwd', 'r') as f:
for line in f:
print line
... more processing code ...
After this statement has executed, the file object in `f' will have been
automatically closed, even if the *note for: c30. loop raised an
exception part-way through the block.
Note: In this case, `f' is the same object created by *note open():
4f0, because ‘file.__enter__()’ returns `self'.
The *note threading: 109. module’s locks and condition variables also
support the ‘*note with: 6e9.’ statement:
lock = threading.Lock()
with lock:
# Critical section of code
...
The lock is acquired before the block is executed and always released
once the block is complete.
The ‘localcontext()’ function in the *note decimal: 36. module makes it
easy to save and restore the current decimal context, which encapsulates
the desired precision and rounding characteristics for computations:
from decimal import Decimal, Context, localcontext
# Displays with default precision of 28 digits
v = Decimal('578')
print v.sqrt()
with localcontext(Context(prec=16)):
# All code in this block uses a precision of 16 digits.
# The original context is restored on exiting the block.
print v.sqrt()
* Menu:
* Writing Context Managers::
* The contextlib module::
File: python.info, Node: Writing Context Managers, Next: The contextlib module, Up: PEP 343 The ‘with’ statement
1.11.3.1 Writing Context Managers
.................................
Under the hood, the ‘*note with: 6e9.’ statement is fairly complicated.
Most people will only use ‘‘with’’ in company with existing objects and
don’t need to know these details, so you can skip the rest of this
section if you like. Authors of new objects will need to understand the
details of the underlying implementation and should keep reading.
A high-level explanation of the context management protocol is:
* The expression is evaluated and should result in an object called a
“context manager”. The context manager must have *note
__enter__(): c95. and *note __exit__(): c96. methods.
* The context manager’s *note __enter__(): c95. method is called.
The value returned is assigned to `VAR'. If no ‘as VAR’ clause is
present, the value is simply discarded.
* The code in `BLOCK' is executed.
* If `BLOCK' raises an exception, the context manager’s *note
__exit__(): c96. method is called with three arguments, the
exception details (‘type, value, traceback’, the same values
returned by *note sys.exc_info(): c5f, which can also be ‘None’ if
no exception occurred). The method’s return value controls whether
an exception is re-raised: any false value re-raises the exception,
and ‘True’ will result in suppressing it. You’ll only rarely want
to suppress the exception, because if you do the author of the code
containing the ‘*note with: 6e9.’ statement will never realize
anything went wrong.
* If `BLOCK' didn’t raise an exception, the *note __exit__(): c96.
method is still called, but `type', `value', and `traceback' are
all ‘None’.
Let’s think through an example. I won’t present detailed code but will
only sketch the methods necessary for a database that supports
transactions.
(For people unfamiliar with database terminology: a set of changes to
the database are grouped into a transaction. Transactions can be either
committed, meaning that all the changes are written into the database,
or rolled back, meaning that the changes are all discarded and the
database is unchanged. See any database textbook for more information.)
Let’s assume there’s an object representing a database connection. Our
goal will be to let the user write code like this:
db_connection = DatabaseConnection()
with db_connection as cursor:
cursor.execute('insert into ...')
cursor.execute('delete from ...')
# ... more operations ...
The transaction should be committed if the code in the block runs
flawlessly or rolled back if there’s an exception. Here’s the basic
interface for ‘DatabaseConnection’ that I’ll assume:
class DatabaseConnection:
# Database interface
def cursor(self):
"Returns a cursor object and starts a new transaction"
def commit(self):
"Commits current transaction"
def rollback(self):
"Rolls back current transaction"
The *note __enter__(): c95. method is pretty easy, having only to start
a new transaction. For this application the resulting cursor object
would be a useful result, so the method will return it. The user can
then add ‘as cursor’ to their ‘*note with: 6e9.’ statement to bind the
cursor to a variable name.
class DatabaseConnection:
...
def __enter__(self):
# Code to start a new transaction
cursor = self.cursor()
return cursor
The *note __exit__(): c96. method is the most complicated because it’s
where most of the work has to be done. The method has to check if an
exception occurred. If there was no exception, the transaction is
committed. The transaction is rolled back if there was an exception.
In the code below, execution will just fall off the end of the function,
returning the default value of ‘None’. ‘None’ is false, so the
exception will be re-raised automatically. If you wished, you could be
more explicit and add a *note return: 190. statement at the marked
location.
class DatabaseConnection:
...
def __exit__(self, type, value, tb):
if tb is None:
# No exception, so commit
self.commit()
else:
# Exception occurred, so rollback.
self.rollback()
# return False
File: python.info, Node: The contextlib module, Prev: Writing Context Managers, Up: PEP 343 The ‘with’ statement
1.11.3.2 The contextlib module
..............................
The *note contextlib: 24. module provides some functions and a decorator
that are useful when writing objects for use with the ‘*note with: 6e9.’
statement.
The decorator is called ‘contextmanager()’, and lets you write a single
generator function instead of defining a new class. The generator
should yield exactly one value. The code up to the *note yield: 18f.
will be executed as the *note __enter__(): c95. method, and the value
yielded will be the method’s return value that will get bound to the
variable in the ‘*note with: 6e9.’ statement’s ‘as’ clause, if any. The
code after the ‘yield’ will be executed in the *note __exit__(): c96.
method. Any exception raised in the block will be raised by the ‘yield’
statement.
Using this decorator, our database example from the previous section
could be written as:
from contextlib import contextmanager
@contextmanager
def db_transaction(connection):
cursor = connection.cursor()
try:
yield cursor
except:
connection.rollback()
raise
else:
connection.commit()
db = DatabaseConnection()
with db_transaction(db) as cursor:
...
The *note contextlib: 24. module also has a ‘nested(mgr1, mgr2, ...)’
function that combines a number of context managers so you don’t need to
write nested ‘*note with: 6e9.’ statements. In this example, the single
‘‘with’’ statement both starts a database transaction and acquires a
thread lock:
lock = threading.Lock()
with nested (db_transaction(db), lock) as (cursor, locked):
...
Finally, the ‘closing()’ function returns its argument so that it can be
bound to a variable, and calls the argument’s ‘.close()’ method at the
end of the block.
import urllib, sys
from contextlib import closing
with closing(urllib.urlopen('http://www.yahoo.com')) as f:
for line in f:
sys.stdout.write(line)
See also
........
PEP 343(1) - The “with” statement
PEP written by Guido van Rossum and Nick Coghlan; implemented by
Mike Bland, Guido van Rossum, and Neal Norwitz. The PEP shows the
code generated for a ‘*note with: 6e9.’ statement, which can be
helpful in learning how the statement works.
The documentation for the *note contextlib: 24. module.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0343
File: python.info, Node: PEP 366 Explicit Relative Imports From a Main Module, Next: PEP 370 Per-user site-packages Directory, Prev: PEP 343 The ‘with’ statement, Up: What’s New in Python 2 6
1.11.4 PEP 366: Explicit Relative Imports From a Main Module
------------------------------------------------------------
Python’s *note -m: 337. switch allows running a module as a script.
When you ran a module that was located inside a package, relative
imports didn’t work correctly.
The fix for Python 2.6 adds a *note __package__: 955. attribute to
modules. When this attribute is present, relative imports will be
relative to the value of this attribute instead of the *note __name__:
d12. attribute.
PEP 302-style importers can then set *note __package__: 955. as
necessary. The *note runpy: e2. module that implements the *note -m:
337. switch now does this, so relative imports will now work correctly
in scripts running from inside a package.
File: python.info, Node: PEP 370 Per-user site-packages Directory, Next: PEP 371 The multiprocessing Package, Prev: PEP 366 Explicit Relative Imports From a Main Module, Up: What’s New in Python 2 6
1.11.5 PEP 370: Per-user ‘site-packages’ Directory
--------------------------------------------------
When you run Python, the module search path ‘sys.path’ usually includes
a directory whose path ends in ‘"site-packages"’. This directory is
intended to hold locally-installed packages available to all users using
a machine or a particular site installation.
Python 2.6 introduces a convention for user-specific site directories.
The directory varies depending on the platform:
* Unix and Mac OS X: ‘~/.local/’
* Windows: ‘%APPDATA%/Python’
Within this directory, there will be version-specific subdirectories,
such as ‘lib/python2.6/site-packages’ on Unix/Mac OS and
‘Python26/site-packages’ on Windows.
If you don’t like the default directory, it can be overridden by an
environment variable. *note PYTHONUSERBASE: d14. sets the root
directory used for all Python versions supporting this feature. On
Windows, the directory for application-specific data can be changed by
setting the ‘APPDATA’ environment variable. You can also modify the
‘site.py’ file for your Python installation.
The feature can be disabled entirely by running Python with the *note
-s: d15. option or setting the *note PYTHONNOUSERSITE: d16. environment
variable.
See also
........
PEP 370(1) - Per-user ‘site-packages’ Directory
PEP written and implemented by Christian Heimes.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0370
File: python.info, Node: PEP 371 The multiprocessing Package, Next: PEP 3101 Advanced String Formatting, Prev: PEP 370 Per-user site-packages Directory, Up: What’s New in Python 2 6
1.11.6 PEP 371: The ‘multiprocessing’ Package
---------------------------------------------
The new *note multiprocessing: b7. package lets Python programs create
new processes that will perform a computation and return a result to the
parent. The parent and child processes can communicate using queues and
pipes, synchronize their operations using locks and semaphores, and can
share simple arrays of data.
The *note multiprocessing: b7. module started out as an exact emulation
of the *note threading: 109. module using processes instead of threads.
That goal was discarded along the path to Python 2.6, but the general
approach of the module is still similar. The fundamental class is the
‘Process’, which is passed a callable object and a collection of
arguments. The ‘start()’ method sets the callable running in a
subprocess, after which you can call the ‘is_alive()’ method to check
whether the subprocess is still running and the ‘join()’ method to wait
for the process to exit.
Here’s a simple example where the subprocess will calculate a factorial.
The function doing the calculation is written strangely so that it takes
significantly longer when the input argument is a multiple of 4.
import time
from multiprocessing import Process, Queue
def factorial(queue, N):
"Compute a factorial."
# If N is a multiple of 4, this function will take much longer.
if (N % 4) == 0:
time.sleep(.05 * N/4)
# Calculate the result
fact = 1L
for i in range(1, N+1):
fact = fact * i
# Put the result on the queue
queue.put(fact)
if __name__ == '__main__':
queue = Queue()
N = 5
p = Process(target=factorial, args=(queue, N))
p.start()
p.join()
result = queue.get()
print 'Factorial', N, '=', result
A *note Queue: d18. is used to communicate the result of the factorial.
The *note Queue: d18. object is stored in a global variable. The child
process will use the value of the variable when the child was created;
because it’s a *note Queue: d18, parent and child can use the object to
communicate. (If the parent were to change the value of the global
variable, the child’s value would be unaffected, and vice versa.)
Two other classes, ‘Pool’ and ‘Manager’, provide higher-level
interfaces. ‘Pool’ will create a fixed number of worker processes, and
requests can then be distributed to the workers by calling ‘apply()’ or
‘apply_async()’ to add a single request, and *note map(): c2d. or
‘map_async()’ to add a number of requests. The following code uses a
‘Pool’ to spread requests across 5 worker processes and retrieve a list
of results:
from multiprocessing import Pool
def factorial(N, dictionary):
"Compute a factorial."
...
p = Pool(5)
result = p.map(factorial, range(1, 1000, 10))
for v in result:
print v
This produces the following output:
1
39916800
51090942171709440000
8222838654177922817725562880000000
33452526613163807108170062053440751665152000000000
...
The other high-level interface, the ‘Manager’ class, creates a separate
server process that can hold master copies of Python data structures.
Other processes can then access and modify these data structures using
proxy objects. The following example creates a shared dictionary by
calling the *note dict(): 1b8. method; the worker processes then insert
values into the dictionary. (Locking is not done for you automatically,
which doesn’t matter in this example. ‘Manager’’s methods also include
‘Lock()’, ‘RLock()’, and ‘Semaphore()’ to create shared locks.)
import time
from multiprocessing import Pool, Manager
def factorial(N, dictionary):
"Compute a factorial."
# Calculate the result
fact = 1L
for i in range(1, N+1):
fact = fact * i
# Store result in dictionary
dictionary[N] = fact
if __name__ == '__main__':
p = Pool(5)
mgr = Manager()
d = mgr.dict() # Create shared dictionary
# Run tasks using the pool
for N in range(1, 1000, 10):
p.apply_async(factorial, (N, d))
# Mark pool as closed -- no more tasks can be added.
p.close()
# Wait for tasks to exit
p.join()
# Output results
for k, v in sorted(d.items()):
print k, v
This will produce the output:
1 1
11 39916800
21 51090942171709440000
31 8222838654177922817725562880000000
41 33452526613163807108170062053440751665152000000000
51 15511187532873822802242430164693032110632597200169861120000...
See also
........
The documentation for the *note multiprocessing: b7. module.
PEP 371(1) - Addition of the multiprocessing package
PEP written by Jesse Noller and Richard Oudkerk; implemented by
Richard Oudkerk and Jesse Noller.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0371
File: python.info, Node: PEP 3101 Advanced String Formatting, Next: PEP 3105 print As a Function, Prev: PEP 371 The multiprocessing Package, Up: What’s New in Python 2 6
1.11.7 PEP 3101: Advanced String Formatting
-------------------------------------------
In Python 3.0, the ‘%’ operator is supplemented by a more powerful
string formatting method, *note format(): 4db. Support for the *note
str.format(): 4da. method has been backported to Python 2.6.
In 2.6, both 8-bit and Unicode strings have a ‘.format()’ method that
treats the string as a template and takes the arguments to be formatted.
The formatting template uses curly brackets (‘{’, ‘}’) as special
characters:
>>> # Substitute positional argument 0 into the string.
>>> "User ID: {0}".format("root")
'User ID: root'
>>> # Use the named keyword arguments
>>> "User ID: {uid} Last seen: {last_login}".format(
... uid="root",
... last_login = "5 Mar 2008 07:20")
'User ID: root Last seen: 5 Mar 2008 07:20'
Curly brackets can be escaped by doubling them:
>>> "Empty dict: {{}}".format()
"Empty dict: {}"
Field names can be integers indicating positional arguments, such as
‘{0}’, ‘{1}’, etc. or names of keyword arguments. You can also supply
compound field names that read attributes or access dictionary keys:
>>> import sys
>>> print 'Platform: {0.platform}\nPython version: {0.version}'.format(sys)
Platform: darwin
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
>>> import mimetypes
>>> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map)
'Content-type: video/mp4'
Note that when using dictionary-style notation such as ‘[.mp4]’, you
don’t need to put any quotation marks around the string; it will look up
the value using ‘.mp4’ as the key. Strings beginning with a number will
be converted to an integer. You can’t write more complicated
expressions inside a format string.
So far we’ve shown how to specify which field to substitute into the
resulting string. The precise formatting used is also controllable by
adding a colon followed by a format specifier. For example:
>>> # Field 0: left justify, pad to 15 characters
>>> # Field 1: right justify, pad to 6 characters
>>> fmt = '{0:15} ${1:>6}'
>>> fmt.format('Registration', 35)
'Registration $ 35'
>>> fmt.format('Tutorial', 50)
'Tutorial $ 50'
>>> fmt.format('Banquet', 125)
'Banquet $ 125'
Format specifiers can reference other fields through nesting:
>>> fmt = '{0:{1}}'
>>> width = 15
>>> fmt.format('Invoice #1234', width)
'Invoice #1234 '
>>> width = 35
>>> fmt.format('Invoice #1234', width)
'Invoice #1234 '
The alignment of a field within the desired width can be specified:
Character Effect
----------------------------------------------------------------------
< (default) Left-align
> Right-align
^ Center
= (For numeric types only) Pad after the sign.
Format specifiers can also include a presentation type, which controls
how the value is formatted. For example, floating-point numbers can be
formatted as a general number or in exponential notation:
>>> '{0:g}'.format(3.75)
'3.75'
>>> '{0:e}'.format(3.75)
'3.750000e+00'
A variety of presentation types are available. Consult the 2.6
documentation for a *note complete list: d1a.; here’s a sample:
‘b’ Binary. Outputs the number in base 2.
‘c’ Character. Converts the integer to the corresponding Unicode character
before printing.
‘d’ Decimal Integer. Outputs the number in base 10.
‘o’ Octal format. Outputs the number in base 8.
‘x’ Hex format. Outputs the number in base 16, using lower-case letters for
the digits above 9.
‘e’ Exponent notation. Prints the number in scientific notation using the
letter ‘e’ to indicate the exponent.
‘g’ General format. This prints the number as a fixed-point number, unless
the number is too large, in which case it switches to ‘e’ exponent
notation.
‘n’ Number. This is the same as ‘g’ (for floats) or ‘d’ (for integers),
except that it uses the current locale setting to insert the appropriate
number separator characters.
‘%’ Percentage. Multiplies the number by 100 and displays in fixed (‘f’)
format, followed by a percent sign.
Classes and types can define a *note __format__(): 94e. method to
control how they’re formatted. It receives a single argument, the
format specifier:
def __format__(self, format_spec):
if isinstance(format_spec, unicode):
return unicode(str(self))
else:
return str(self)
There’s also a *note format(): 4db. builtin that will format a single
value. It calls the type’s *note __format__(): 94e. method with the
provided specifier:
>>> format(75.6564, '.2f')
'75.66'
See also
........
*note Format String Syntax: d1a.
The reference documentation for format fields.
PEP 3101(1) - Advanced String Formatting
PEP written by Talin. Implemented by Eric Smith.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3101
File: python.info, Node: PEP 3105 print As a Function, Next: PEP 3110 Exception-Handling Changes, Prev: PEP 3101 Advanced String Formatting, Up: What’s New in Python 2 6
1.11.8 PEP 3105: ‘print’ As a Function
--------------------------------------
The ‘print’ statement becomes the *note print(): 886. function in Python
3.0. Making *note print(): 886. a function makes it possible to replace
the function by doing ‘def print(...)’ or importing a new function from
somewhere else.
Python 2.6 has a ‘__future__’ import that removes ‘print’ as language
syntax, letting you use the functional form instead. For example:
>>> from __future__ import print_function
>>> print('# of entries', len(dictionary), file=sys.stderr)
The signature of the new function is:
def print(*args, sep=' ', end='\n', file=None)
The parameters are:
* `args': positional arguments whose values will be printed out.
* `sep': the separator, which will be printed between arguments.
* `end': the ending text, which will be printed after all of the
arguments have been output.
* `file': the file object to which the output will be sent.
See also
........
PEP 3105(1) - Make print a function
PEP written by Georg Brandl.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3105
File: python.info, Node: PEP 3110 Exception-Handling Changes, Next: PEP 3112 Byte Literals, Prev: PEP 3105 print As a Function, Up: What’s New in Python 2 6
1.11.9 PEP 3110: Exception-Handling Changes
-------------------------------------------
One error that Python programmers occasionally make is writing the
following code:
try:
...
except TypeError, ValueError: # Wrong!
...
The author is probably trying to catch both *note TypeError: 192. and
*note ValueError: 1fb. exceptions, but this code actually does something
different: it will catch *note TypeError: 192. and bind the resulting
exception object to the local name ‘"ValueError"’. The *note
ValueError: 1fb. exception will not be caught at all. The correct code
specifies a tuple of exceptions:
try:
...
except (TypeError, ValueError):
...
This error happens because the use of the comma here is ambiguous: does
it indicate two different nodes in the parse tree, or a single node
that’s a tuple?
Python 3.0 makes this unambiguous by replacing the comma with the word
“as”. To catch an exception and store the exception object in the
variable ‘exc’, you must write:
try:
...
except TypeError as exc:
...
Python 3.0 will only support the use of “as”, and therefore interprets
the first example as catching two different exceptions. Python 2.6
supports both the comma and “as”, so existing code will continue to
work. We therefore suggest using “as” when writing new Python code that
will only be executed with 2.6.
See also
........
PEP 3110(1) - Catching Exceptions in Python 3000
PEP written and implemented by Collin Winter.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3110
File: python.info, Node: PEP 3112 Byte Literals, Next: PEP 3116 New I/O Library, Prev: PEP 3110 Exception-Handling Changes, Up: What’s New in Python 2 6
1.11.10 PEP 3112: Byte Literals
-------------------------------
Python 3.0 adopts Unicode as the language’s fundamental string type and
denotes 8-bit literals differently, either as ‘b'string'’ or using a
*note bytes: 331. constructor. For future compatibility, Python 2.6
adds *note bytes: 331. as a synonym for the *note str: 330. type, and it
also supports the ‘b''’ notation.
The 2.6 *note str: 330. differs from 3.0’s *note bytes: 331. type in
various ways; most notably, the constructor is completely different. In
3.0, ‘bytes([65, 66, 67])’ is 3 elements long, containing the bytes
representing ‘ABC’; in 2.6, ‘bytes([65, 66, 67])’ returns the 12-byte
string representing the *note str(): 330. of the list.
The primary use of *note bytes: 331. in 2.6 will be to write tests of
object type such as ‘isinstance(x, bytes)’. This will help the 2to3
converter, which can’t tell whether 2.x code intends strings to contain
either characters or 8-bit bytes; you can now use either *note bytes:
331. or *note str: 330. to represent your intention exactly, and the
resulting code will also be correct in Python 3.0.
There’s also a ‘__future__’ import that causes all string literals to
become Unicode strings. This means that ‘\u’ escape sequences can be
used to include Unicode characters:
from __future__ import unicode_literals
s = ('\u751f\u3080\u304e\u3000\u751f\u3054'
'\u3081\u3000\u751f\u305f\u307e\u3054')
print len(s) # 12 Unicode characters
At the C level, Python 3.0 will rename the existing 8-bit string type,
called ‘PyStringObject’ in Python 2.x, to *note PyBytesObject: d1e.
Python 2.6 uses ‘#define’ to support using the names *note
PyBytesObject(): d1e, *note PyBytes_Check(): d1f, *note
PyBytes_FromStringAndSize(): d20, and all the other functions and macros
used with strings.
Instances of the *note bytes: 331. type are immutable just as strings
are. A new *note bytearray: 332. type stores a mutable sequence of
bytes:
>>> bytearray([65, 66, 67])
bytearray(b'ABC')
>>> b = bytearray(u'\u21ef\u3244', 'utf-8')
>>> b
bytearray(b'\xe2\x87\xaf\xe3\x89\x84')
>>> b[0] = '\xe3'
>>> b
bytearray(b'\xe3\x87\xaf\xe3\x89\x84')
>>> unicode(str(b), 'utf-8')
u'\u31ef \u3244'
Byte arrays support most of the methods of string types, such as
‘startswith()’/‘endswith()’, ‘find()’/‘rfind()’, and some of the methods
of lists, such as ‘append()’, ‘pop()’, and ‘reverse()’.
>>> b = bytearray('ABC')
>>> b.append('d')
>>> b.append(ord('e'))
>>> b
bytearray(b'ABCde')
There’s also a corresponding C API, with *note PyByteArray_FromObject():
d21, *note PyByteArray_FromStringAndSize(): d22, and various other
functions.
See also
........
PEP 3112(1) - Bytes literals in Python 3000
PEP written by Jason Orendorff; backported to 2.6 by Christian
Heimes.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3112
File: python.info, Node: PEP 3116 New I/O Library, Next: PEP 3118 Revised Buffer Protocol, Prev: PEP 3112 Byte Literals, Up: What’s New in Python 2 6
1.11.11 PEP 3116: New I/O Library
---------------------------------
Python’s built-in file objects support a number of methods, but
file-like objects don’t necessarily support all of them. Objects that
imitate files usually support ‘read()’ and ‘write()’, but they may not
support *note readline(): de, for example. Python 3.0 introduces a
layered I/O library in the *note io: a1. module that separates buffering
and text-handling features from the fundamental read and write
operations.
There are three levels of abstract base classes provided by the *note
io: a1. module:
* ‘RawIOBase’ defines raw I/O operations: ‘read()’, ‘readinto()’,
‘write()’, ‘seek()’, ‘tell()’, ‘truncate()’, and ‘close()’. Most
of the methods of this class will often map to a single system
call. There are also ‘readable()’, ‘writable()’, and ‘seekable()’
methods for determining what operations a given object will allow.
Python 3.0 has concrete implementations of this class for files and
sockets, but Python 2.6 hasn’t restructured its file and socket
objects in this way.
* ‘BufferedIOBase’ is an abstract base class that buffers data in
memory to reduce the number of system calls used, making I/O
processing more efficient. It supports all of the methods of
‘RawIOBase’, and adds a ‘raw’ attribute holding the underlying raw
object.
There are five concrete classes implementing this ABC.
‘BufferedWriter’ and ‘BufferedReader’ are for objects that support
write-only or read-only usage that have a ‘seek()’ method for
random access. ‘BufferedRandom’ objects support read and write
access upon the same underlying stream, and ‘BufferedRWPair’ is for
objects such as TTYs that have both read and write operations
acting upon unconnected streams of data. The ‘BytesIO’ class
supports reading, writing, and seeking over an in-memory buffer.
* ‘TextIOBase’: Provides functions for reading and writing strings
(remember, strings will be Unicode in Python 3.0), and supporting
*note universal newlines: 5f4. ‘TextIOBase’ defines the *note
readline(): de. method and supports iteration upon objects.
There are two concrete implementations. ‘TextIOWrapper’ wraps a
buffered I/O object, supporting all of the methods for text I/O and
adding a ‘buffer’ attribute for access to the underlying object.
‘StringIO’ simply buffers everything in memory without ever writing
anything to disk.
(In Python 2.6, *note io.StringIO: 82d. is implemented in pure
Python, so it’s pretty slow. You should therefore stick with the
existing ‘StringIO’ module or ‘cStringIO’ for now. At some point
Python 3.0’s *note io: a1. module will be rewritten into C for
speed, and perhaps the C implementation will be backported to the
2.x releases.)
In Python 2.6, the underlying implementations haven’t been restructured
to build on top of the *note io: a1. module’s classes. The module is
being provided to make it easier to write code that’s forward-compatible
with 3.0, and to save developers the effort of writing their own
implementations of buffering and text I/O.
See also
........
PEP 3116(1) - New I/O
PEP written by Daniel Stutzbach, Mike Verdone, and Guido van
Rossum. Code by Guido van Rossum, Georg Brandl, Walter Doerwald,
Jeremy Hylton, Martin von Löwis, Tony Lownds, and others.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3116
File: python.info, Node: PEP 3118 Revised Buffer Protocol, Next: PEP 3119 Abstract Base Classes, Prev: PEP 3116 New I/O Library, Up: What’s New in Python 2 6
1.11.12 PEP 3118: Revised Buffer Protocol
-----------------------------------------
The buffer protocol is a C-level API that lets Python types exchange
pointers into their internal representations. A memory-mapped file can
be viewed as a buffer of characters, for example, and this lets another
module such as *note re: dd. treat memory-mapped files as a string of
characters to be searched.
The primary users of the buffer protocol are numeric-processing packages
such as NumPy, which expose the internal representation of arrays so
that callers can write data directly into an array instead of going
through a slower API. This PEP updates the buffer protocol in light of
experience from NumPy development, adding a number of new features such
as indicating the shape of an array or locking a memory region.
The most important new C API function is ‘PyObject_GetBuffer(PyObject
*obj, Py_buffer *view, int flags)’, which takes an object and a set of
flags, and fills in the ‘Py_buffer’ structure with information about the
object’s memory representation. Objects can use this operation to lock
memory in place while an external caller could be modifying the
contents, so there’s a corresponding ‘PyBuffer_Release(Py_buffer *view)’
to indicate that the external caller is done.
The `flags' argument to *note PyObject_GetBuffer(): d25. specifies
constraints upon the memory returned. Some examples are:
* ‘PyBUF_WRITABLE’ indicates that the memory must be writable.
* ‘PyBUF_LOCK’ requests a read-only or exclusive lock on the
memory.
* ‘PyBUF_C_CONTIGUOUS’ and ‘PyBUF_F_CONTIGUOUS’ requests a
C-contiguous (last dimension varies the fastest) or
Fortran-contiguous (first dimension varies the fastest) array
layout.
Two new argument codes for *note PyArg_ParseTuple(): 26a, ‘s*’ and ‘z*’,
return locked buffer objects for a parameter.
See also
........
PEP 3118(1) - Revising the buffer protocol
PEP written by Travis Oliphant and Carl Banks; implemented by
Travis Oliphant.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3118
File: python.info, Node: PEP 3119 Abstract Base Classes, Next: PEP 3127 Integer Literal Support and Syntax, Prev: PEP 3118 Revised Buffer Protocol, Up: What’s New in Python 2 6
1.11.13 PEP 3119: Abstract Base Classes
---------------------------------------
Some object-oriented languages such as Java support interfaces,
declaring that a class has a given set of methods or supports a given
access protocol. Abstract Base Classes (or ABCs) are an equivalent
feature for Python. The ABC support consists of an *note abc: 4. module
containing a metaclass called ‘ABCMeta’, special handling of this
metaclass by the *note isinstance(): 44f. and *note issubclass(): 450.
builtins, and a collection of basic ABCs that the Python developers
think will be widely useful. Future versions of Python will probably
add more ABCs.
Let’s say you have a particular class and wish to know whether it
supports dictionary-style access. The phrase “dictionary-style” is
vague, however. It probably means that accessing items with ‘obj[1]’
works. Does it imply that setting items with ‘obj[2] = value’ works?
Or that the object will have ‘keys()’, ‘values()’, and ‘items()’
methods? What about the iterative variants such as ‘iterkeys()’? *note
copy(): 26. and ‘update()’? Iterating over the object with *note
iter(): d27.?
The Python 2.6 *note collections: 1e. module includes a number of
different ABCs that represent these distinctions. ‘Iterable’ indicates
that a class defines *note __iter__(): 50f, and ‘Container’ means the
class defines a *note __contains__(): d28. method and therefore supports
‘x in y’ expressions. The basic dictionary interface of getting items,
setting items, and ‘keys()’, ‘values()’, and ‘items()’, is defined by
the ‘MutableMapping’ ABC.
You can derive your own classes from a particular ABC to indicate they
support that ABC’s interface:
import collections
class Storage(collections.MutableMapping):
...
Alternatively, you could write the class without deriving from the
desired ABC and instead register the class by calling the ABC’s
‘register()’ method:
import collections
class Storage:
...
collections.MutableMapping.register(Storage)
For classes that you write, deriving from the ABC is probably clearer.
The ‘register()’ method is useful when you’ve written a new ABC that can
describe an existing type or class, or if you want to declare that some
third-party class implements an ABC. For example, if you defined a
‘PrintableType’ ABC, it’s legal to do:
# Register Python's types
PrintableType.register(int)
PrintableType.register(float)
PrintableType.register(str)
Classes should obey the semantics specified by an ABC, but Python can’t
check this; it’s up to the class author to understand the ABC’s
requirements and to implement the code accordingly.
To check whether an object supports a particular interface, you can now
write:
def func(d):
if not isinstance(d, collections.MutableMapping):
raise ValueError("Mapping object expected, not %r" % d)
Don’t feel that you must now begin writing lots of checks as in the
above example. Python has a strong tradition of duck-typing, where
explicit type-checking is never done and code simply calls methods on an
object, trusting that those methods will be there and raising an
exception if they aren’t. Be judicious in checking for ABCs and only do
it where it’s absolutely necessary.
You can write your own ABCs by using ‘abc.ABCMeta’ as the metaclass in a
class definition:
from abc import ABCMeta, abstractmethod
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
class Square(Drawable):
def draw(self, x, y, scale):
...
In the ‘Drawable’ ABC above, the ‘draw_doubled()’ method renders the
object at twice its size and can be implemented in terms of other
methods described in ‘Drawable’. Classes implementing this ABC
therefore don’t need to provide their own implementation of
‘draw_doubled()’, though they can do so. An implementation of ‘draw()’
is necessary, though; the ABC can’t provide a useful generic
implementation.
You can apply the ‘@abstractmethod’ decorator to methods such as
‘draw()’ that must be implemented; Python will then raise an exception
for classes that don’t define the method. Note that the exception is
only raised when you actually try to create an instance of a subclass
lacking the method:
>>> class Circle(Drawable):
... pass
...
>>> c = Circle()
Traceback (most recent call last):
File "", line 1, in
TypeError: Can't instantiate abstract class Circle with abstract methods draw
>>>
Abstract data attributes can be declared using the ‘@abstractproperty’
decorator:
from abc import abstractproperty
...
@abstractproperty
def readonly(self):
return self._x
Subclasses must then define a ‘readonly()’ property.
See also
........
PEP 3119(1) - Introducing Abstract Base Classes
PEP written by Guido van Rossum and Talin. Implemented by Guido
van Rossum. Backported to 2.6 by Benjamin Aranguren, with Alex
Martelli.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3119
File: python.info, Node: PEP 3127 Integer Literal Support and Syntax, Next: PEP 3129 Class Decorators, Prev: PEP 3119 Abstract Base Classes, Up: What’s New in Python 2 6
1.11.14 PEP 3127: Integer Literal Support and Syntax
----------------------------------------------------
Python 3.0 changes the syntax for octal (base-8) integer literals,
prefixing them with “0o” or “0O” instead of a leading zero, and adds
support for binary (base-2) integer literals, signalled by a “0b” or
“0B” prefix.
Python 2.6 doesn’t drop support for a leading 0 signalling an octal
number, but it does add support for “0o” and “0b”:
>>> 0o21, 2*8 + 1
(17, 17)
>>> 0b101111
47
The *note oct(): c65. builtin still returns numbers prefixed with a
leading zero, and a new *note bin(): c40. builtin returns the binary
representation for a number:
>>> oct(42)
'052'
>>> future_builtins.oct(42)
'0o52'
>>> bin(173)
'0b10101101'
The *note int(): 184. and ‘long()’ builtins will now accept the “0o” and
“0b” prefixes when base-8 or base-2 are requested, or when the `base'
argument is zero (signalling that the base used should be determined
from the string):
>>> int ('0o52', 0)
42
>>> int('1101', 2)
13
>>> int('0b1101', 2)
13
>>> int('0b1101', 0)
13
See also
........
PEP 3127(1) - Integer Literal Support and Syntax
PEP written by Patrick Maupin; backported to 2.6 by Eric Smith.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3127
File: python.info, Node: PEP 3129 Class Decorators, Next: PEP 3141 A Type Hierarchy for Numbers, Prev: PEP 3127 Integer Literal Support and Syntax, Up: What’s New in Python 2 6
1.11.15 PEP 3129: Class Decorators
----------------------------------
Decorators have been extended from functions to classes. It’s now legal
to write:
@foo
@bar
class A:
pass
This is equivalent to:
class A:
pass
A = foo(bar(A))
See also
........
PEP 3129(1) - Class Decorators
PEP written by Collin Winter.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3129
File: python.info, Node: PEP 3141 A Type Hierarchy for Numbers, Next: Other Language Changes<10>, Prev: PEP 3129 Class Decorators, Up: What’s New in Python 2 6
1.11.16 PEP 3141: A Type Hierarchy for Numbers
----------------------------------------------
Python 3.0 adds several abstract base classes for numeric types inspired
by Scheme’s numeric tower. These classes were backported to 2.6 as the
*note numbers: c1. module.
The most general ABC is ‘Number’. It defines no operations at all, and
only exists to allow checking if an object is a number by doing
‘isinstance(obj, Number)’.
‘Complex’ is a subclass of ‘Number’. Complex numbers can undergo the
basic operations of addition, subtraction, multiplication, division, and
exponentiation, and you can retrieve the real and imaginary parts and
obtain a number’s conjugate. Python’s built-in complex type is an
implementation of ‘Complex’.
‘Real’ further derives from ‘Complex’, and adds operations that only
work on real numbers: ‘floor()’, ‘trunc()’, rounding, taking the
remainder mod N, floor division, and comparisons.
‘Rational’ numbers derive from ‘Real’, have ‘numerator’ and
‘denominator’ properties, and can be converted to floats. Python 2.6
adds a simple rational-number class, ‘Fraction’, in the *note fractions:
83. module. (It’s called ‘Fraction’ instead of ‘Rational’ to avoid a
name clash with *note numbers.Rational: c58.)
‘Integral’ numbers derive from ‘Rational’, and can be shifted left and
right with ‘<<’ and ‘>>’, combined using bitwise operations such as ‘&’
and ‘|’, and can be used as array indexes and slice boundaries.
In Python 3.0, the PEP slightly redefines the existing builtins *note
round(): c6d, *note math.floor(): d2c, *note math.ceil(): d2d, and adds
a new one, *note math.trunc(): d2e, that’s been backported to Python
2.6. *note math.trunc(): d2e. rounds toward zero, returning the closest
‘Integral’ that’s between the function’s argument and zero.
See also
........
PEP 3141(1) - A Type Hierarchy for Numbers
PEP written by Jeffrey Yasskin.
Scheme’s numerical tower(2), from the Guile manual.
Scheme’s number datatypes(3) from the R5RS Scheme specification.
* Menu:
* The fractions Module::
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3141
(2)
https://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower
(3)
http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2
File: python.info, Node: The fractions Module, Up: PEP 3141 A Type Hierarchy for Numbers
1.11.16.1 The ‘fractions’ Module
................................
To fill out the hierarchy of numeric types, the *note fractions: 83.
module provides a rational-number class. Rational numbers store their
values as a numerator and denominator forming a fraction, and can
exactly represent numbers such as ‘2/3’ that floating-point numbers can
only approximate.
The ‘Fraction’ constructor takes two ‘Integral’ values that will be the
numerator and denominator of the resulting fraction.
>>> from fractions import Fraction
>>> a = Fraction(2, 3)
>>> b = Fraction(2, 5)
>>> float(a), float(b)
(0.66666666666666663, 0.40000000000000002)
>>> a+b
Fraction(16, 15)
>>> a/b
Fraction(5, 3)
For converting floating-point numbers to rationals, the float type now
has an ‘as_integer_ratio()’ method that returns the numerator and
denominator for a fraction that evaluates to the same floating-point
value:
>>> (2.5) .as_integer_ratio()
(5, 2)
>>> (3.1415) .as_integer_ratio()
(7074029114692207L, 2251799813685248L)
>>> (1./3) .as_integer_ratio()
(6004799503160661L, 18014398509481984L)
Note that values that can only be approximated by floating-point
numbers, such as 1./3, are not simplified to the number being
approximated; the fraction attempts to match the floating-point value
`exactly'.
The *note fractions: 83. module is based upon an implementation by
Sjoerd Mullender that was in Python’s ‘Demo/classes/’ directory for a
long time. This implementation was significantly updated by Jeffrey
Yasskin.
File: python.info, Node: Other Language Changes<10>, Next: New and Improved Modules<2>, Prev: PEP 3141 A Type Hierarchy for Numbers, Up: What’s New in Python 2 6
1.11.17 Other Language Changes
------------------------------
Some smaller changes made to the core Python language are:
* Directories and zip archives containing a ‘__main__.py’ file can
now be executed directly by passing their name to the interpreter.
The directory or zip archive is automatically inserted as the first
entry in sys.path. (Suggestion and initial patch by Andy Chu,
subsequently revised by Phillip J. Eby and Nick Coghlan;
bpo-1739468(1).)
* The *note hasattr(): 447. function was catching and ignoring all
errors, under the assumption that they meant a *note __getattr__():
31a. method was failing somehow and the return value of *note
hasattr(): 447. would therefore be ‘False’. This logic shouldn’t
be applied to *note KeyboardInterrupt: 197. and *note SystemExit:
5fc, however; Python 2.6 will no longer discard such exceptions
when *note hasattr(): 447. encounters them. (Fixed by Benjamin
Peterson; bpo-2196(2).)
* When calling a function using the ‘**’ syntax to provide keyword
arguments, you are no longer required to use a Python dictionary;
any mapping will now work:
>>> def f(**kw):
... print sorted(kw)
...
>>> ud=UserDict.UserDict()
>>> ud['a'] = 1
>>> ud['b'] = 'string'
>>> f(**ud)
['a', 'b']
(Contributed by Alexander Belopolsky; bpo-1686487(3).)
It’s also become legal to provide keyword arguments after a ‘*args’
argument to a function call.
>>> def f(*args, **kw):
... print args, kw
...
>>> f(1,2,3, *(4,5,6), keyword=13)
(1, 2, 3, 4, 5, 6) {'keyword': 13}
Previously this would have been a syntax error. (Contributed by
Amaury Forgeot d’Arc; bpo-3473(4).)
* A new builtin, ‘next(iterator, [default])’ returns the next item
from the specified iterator. If the `default' argument is
supplied, it will be returned if `iterator' has been exhausted;
otherwise, the *note StopIteration: 486. exception will be raised.
(Backported in bpo-2719(5).)
* Tuples now have ‘index()’ and ‘count()’ methods matching the list
type’s ‘index()’ and ‘count()’ methods:
>>> t = (0,1,2,3,4,0,1,2)
>>> t.index(3)
3
>>> t.count(0)
2
(Contributed by Raymond Hettinger)
* The built-in types now have improved support for extended slicing
syntax, accepting various combinations of ‘(start, stop, step)’.
Previously, the support was partial and certain corner cases
wouldn’t work. (Implemented by Thomas Wouters.)
* Properties now have three attributes, ‘getter’, ‘setter’ and
‘deleter’, that are decorators providing useful shortcuts for
adding a getter, setter or deleter function to an existing
property. You would use them like this:
class C(object):
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
class D(C):
@C.x.getter
def x(self):
return self._x * 2
@x.setter
def x(self, value):
self._x = value / 2
* Several methods of the built-in set types now accept multiple
iterables: ‘intersection()’, ‘intersection_update()’, ‘union()’,
‘update()’, ‘difference()’ and ‘difference_update()’.
>>> s=set('1234567890')
>>> s.intersection('abc123', 'cdf246') # Intersection between all inputs
set(['2'])
>>> s.difference('246', '789')
set(['1', '0', '3', '5'])
(Contributed by Raymond Hettinger.)
* Many floating-point features were added. The *note float(): 187.
function will now turn the string ‘nan’ into an IEEE 754 Not A
Number value, and ‘+inf’ and ‘-inf’ into positive or negative
infinity. This works on any platform with IEEE 754 semantics.
(Contributed by Christian Heimes; bpo-1635(6).)
Other functions in the *note math: b1. module, ‘isinf()’ and
‘isnan()’, return true if their floating-point argument is infinite
or Not A Number. (bpo-1640(7))
Conversion functions were added to convert floating-point numbers
into hexadecimal strings (bpo-3008(8)). These functions convert
floats to and from a string representation without introducing
rounding errors from the conversion between decimal and binary.
Floats have a *note hex(): c66. method that returns a string
representation, and the ‘float.fromhex()’ method converts a string
back into a number:
>>> a = 3.75
>>> a.hex()
'0x1.e000000000000p+1'
>>> float.fromhex('0x1.e000000000000p+1')
3.75
>>> b=1./3
>>> b.hex()
'0x1.5555555555555p-2'
* A numerical nicety: when creating a complex number from two floats
on systems that support signed zeros (-0 and +0), the *note
complex(): 189. constructor will now preserve the sign of the zero.
(Fixed by Mark T. Dickinson; bpo-1507(9).)
* Classes that inherit a *note __hash__(): 340. method from a parent
class can set ‘__hash__ = None’ to indicate that the class isn’t
hashable. This will make ‘hash(obj)’ raise a *note TypeError: 192.
and the class will not be indicated as implementing the ‘Hashable’
ABC.
You should do this when you’ve defined a ‘__cmp__()’ or *note
__eq__(): 33f. method that compares objects by their value rather
than by identity. All objects have a default hash method that uses
‘id(obj)’ as the hash value. There’s no tidy way to remove the
*note __hash__(): 340. method inherited from a parent class, so
assigning ‘None’ was implemented as an override. At the C level,
extensions can set ‘tp_hash’ to *note
PyObject_HashNotImplemented(): d31. (Fixed by Nick Coghlan and
Amaury Forgeot d’Arc; bpo-2235(10).)
* The *note GeneratorExit: d32. exception now subclasses *note
BaseException: 1a8. instead of *note Exception: 1a9. This means
that an exception handler that does ‘except Exception:’ will not
inadvertently catch *note GeneratorExit: d32. (Contributed by Chad
Austin; bpo-1537(11).)
* Generator objects now have a ‘gi_code’ attribute that refers to the
original code object backing the generator. (Contributed by Collin
Winter; bpo-1473257(12).)
* The *note compile(): 1b4. built-in function now accepts keyword
arguments as well as positional parameters. (Contributed by Thomas
Wouters; bpo-1444529(13).)
* The *note complex(): 189. constructor now accepts strings
containing parenthesized complex numbers, meaning that
‘complex(repr(cplx))’ will now round-trip values. For example,
‘complex('(3+4j)')’ now returns the value (3+4j).
(bpo-1491866(14))
* The string ‘translate()’ method now accepts ‘None’ as the
translation table parameter, which is treated as the identity
transformation. This makes it easier to carry out operations that
only delete characters. (Contributed by Bengt Richter and
implemented by Raymond Hettinger; bpo-1193128(15).)
* The built-in *note dir(): d33. function now checks for a *note
__dir__(): 31b. method on the objects it receives. This method
must return a list of strings containing the names of valid
attributes for the object, and lets the object control the value
that *note dir(): d33. produces. Objects that have *note
__getattr__(): 31a. or *note __getattribute__(): 449. methods can
use this to advertise pseudo-attributes they will honor.
(bpo-1591665(16))
* Instance method objects have new attributes for the object and
function comprising the method; the new synonym for ‘im_self’ is
‘__self__’, and ‘im_func’ is also available as ‘__func__’. The old
names are still supported in Python 2.6, but are gone in 3.0.
* An obscure change: when you use the *note locals(): 455. function
inside a *note class: c6a. statement, the resulting dictionary no
longer returns free variables. (Free variables, in this case, are
variables referenced in the ‘class’ statement that aren’t
attributes of the class.)
* Menu:
* Optimizations: Optimizations<9>.
* Interpreter Changes: Interpreter Changes<2>.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1739468
(2) https://bugs.python.org/issue2196
(3) https://bugs.python.org/issue1686487
(4) https://bugs.python.org/issue3473
(5) https://bugs.python.org/issue2719
(6) https://bugs.python.org/issue1635
(7) https://bugs.python.org/issue1640
(8) https://bugs.python.org/issue3008
(9) https://bugs.python.org/issue1507
(10) https://bugs.python.org/issue2235
(11) https://bugs.python.org/issue1537
(12) https://bugs.python.org/issue1473257
(13) https://bugs.python.org/issue1444529
(14) https://bugs.python.org/issue1491866
(15) https://bugs.python.org/issue1193128
(16) https://bugs.python.org/issue1591665
File: python.info, Node: Optimizations<9>, Next: Interpreter Changes<2>, Up: Other Language Changes<10>
1.11.17.1 Optimizations
.......................
* The *note warnings: 126. module has been rewritten in C. This makes
it possible to invoke warnings from the parser, and may also make
the interpreter’s startup faster. (Contributed by Neal Norwitz and
Brett Cannon; bpo-1631171(1).)
* Type objects now have a cache of methods that can reduce the work
required to find the correct method implementation for a particular
class; once cached, the interpreter doesn’t need to traverse base
classes to figure out the right method to call. The cache is
cleared if a base class or the class itself is modified, so the
cache should remain correct even in the face of Python’s dynamic
nature. (Original optimization implemented by Armin Rigo, updated
for Python 2.6 by Kevin Jacobs; bpo-1700288(2).)
By default, this change is only applied to types that are included
with the Python core. Extension modules may not necessarily be
compatible with this cache, so they must explicitly add
‘Py_TPFLAGS_HAVE_VERSION_TAG’ to the module’s ‘tp_flags’ field to
enable the method cache. (To be compatible with the method cache,
the extension module’s code must not directly access and modify the
‘tp_dict’ member of any of the types it implements. Most modules
don’t do this, but it’s impossible for the Python interpreter to
determine that. See bpo-1878(3) for some discussion.)
* Function calls that use keyword arguments are significantly faster
by doing a quick pointer comparison, usually saving the time of a
full string comparison. (Contributed by Raymond Hettinger, after
an initial implementation by Antoine Pitrou; bpo-1819(4).)
* All of the functions in the *note struct: f8. module have been
rewritten in C, thanks to work at the Need For Speed sprint.
(Contributed by Raymond Hettinger.)
* Some of the standard built-in types now set a bit in their type
objects. This speeds up checking whether an object is a subclass
of one of these types. (Contributed by Neal Norwitz.)
* Unicode strings now use faster code for detecting whitespace and
line breaks; this speeds up the ‘split()’ method by about 25% and
‘splitlines()’ by 35%. (Contributed by Antoine Pitrou.) Memory
usage is reduced by using pymalloc for the Unicode string’s data.
* The ‘with’ statement now stores the *note __exit__(): c96. method
on the stack, producing a small speedup. (Implemented by Jeffrey
Yasskin.)
* To reduce memory usage, the garbage collector will now clear
internal free lists when garbage-collecting the highest generation
of objects. This may return memory to the operating system sooner.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1631171
(2) https://bugs.python.org/issue1700288
(3) https://bugs.python.org/issue1878
(4) https://bugs.python.org/issue1819
File: python.info, Node: Interpreter Changes<2>, Prev: Optimizations<9>, Up: Other Language Changes<10>
1.11.17.2 Interpreter Changes
.............................
Two command-line options have been reserved for use by other Python
implementations. The *note -J: d37. switch has been reserved for use by
Jython for Jython-specific options, such as switches that are passed to
the underlying JVM. *note -X: 155. has been reserved for options
specific to a particular implementation of Python such as CPython,
Jython, or IronPython. If either option is used with Python 2.6, the
interpreter will report that the option isn’t currently used.
Python can now be prevented from writing ‘.pyc’ or ‘.pyo’ files by
supplying the *note -B: d38. switch to the Python interpreter, or by
setting the *note PYTHONDONTWRITEBYTECODE: d39. environment variable
before running the interpreter. This setting is available to Python
programs as the ‘sys.dont_write_bytecode’ variable, and Python code can
change the value to modify the interpreter’s behaviour. (Contributed by
Neal Norwitz and Georg Brandl.)
The encoding used for standard input, output, and standard error can be
specified by setting the *note PYTHONIOENCODING: 92f. environment
variable before running the interpreter. The value should be a string
in the form ‘’ or ‘:’. The `encoding'
part specifies the encoding’s name, e.g. ‘utf-8’ or ‘latin-1’; the
optional `errorhandler' part specifies what to do with characters that
can’t be handled by the encoding, and should be one of “error”,
“ignore”, or “replace”. (Contributed by Martin von Löwis.)
File: python.info, Node: New and Improved Modules<2>, Next: Deprecations and Removals, Prev: Other Language Changes<10>, Up: What’s New in Python 2 6
1.11.18 New and Improved Modules
--------------------------------
As in every release, Python’s standard library received a number of
enhancements and bug fixes. Here’s a partial list of the most notable
changes, sorted alphabetically by module name. Consult the ‘Misc/NEWS’
file in the source tree for a more complete list of changes, or look
through the Subversion logs for all the details.
* The *note asyncore: b. and *note asynchat: 9. modules are being
actively maintained again, and a number of patches and bugfixes
were applied. (Maintained by Josiah Carlson; see bpo-1736190(1)
for one patch.)
* The ‘bsddb’ module also has a new maintainer, Jesús Cea Avión, and
the package is now available as a standalone package. The web page
for the package is www.jcea.es/programacion/pybsddb.htm(2). The
plan is to remove the package from the standard library in Python
3.0, because its pace of releases is much more frequent than
Python’s.
The ‘bsddb.dbshelve’ module now uses the highest pickling protocol
available, instead of restricting itself to protocol 1.
(Contributed by W. Barnes.)
* The *note cgi: 16. module will now read variables from the query
string of an HTTP POST request. This makes it possible to use form
actions with URLs that include query strings such as
“/cgi-bin/add.py?category=1”. (Contributed by Alexandre Fiori and
Nubis; bpo-1817(3).)
The ‘parse_qs()’ and ‘parse_qsl()’ functions have been relocated
from the *note cgi: 16. module to the ‘urlparse’ module. The
versions still available in the *note cgi: 16. module will trigger
*note PendingDeprecationWarning: 279. messages in 2.6
(bpo-600362(4)).
* The *note cmath: 19. module underwent extensive revision,
contributed by Mark Dickinson and Christian Heimes. Five new
functions were added:
* ‘polar()’ converts a complex number to polar form, returning
the modulus and argument of the complex number.
* ‘rect()’ does the opposite, turning a modulus, argument pair
back into the corresponding complex number.
* ‘phase()’ returns the argument (also called the angle) of a
complex number.
* ‘isnan()’ returns True if either the real or imaginary part of
its argument is a NaN.
* ‘isinf()’ returns True if either the real or imaginary part of
its argument is infinite.
The revisions also improved the numerical soundness of the *note
cmath: 19. module. For all functions, the real and imaginary parts
of the results are accurate to within a few units of least
precision (ulps) whenever possible. See bpo-1381(5) for the
details. The branch cuts for ‘asinh()’, ‘atanh()’: and ‘atan()’
have also been corrected.
The tests for the module have been greatly expanded; nearly 2000
new test cases exercise the algebraic functions.
On IEEE 754 platforms, the *note cmath: 19. module now handles IEEE
754 special values and floating-point exceptions in a manner
consistent with Annex ‘G’ of the C99 standard.
* A new data type in the *note collections: 1e. module:
‘namedtuple(typename, fieldnames)’ is a factory function that
creates subclasses of the standard tuple whose fields are
accessible by name as well as index. For example:
>>> var_type = collections.namedtuple('variable',
... 'id name type size')
>>> # Names are separated by spaces or commas.
>>> # 'id, name, type, size' would also work.
>>> var_type._fields
('id', 'name', 'type', 'size')
>>> var = var_type(1, 'frequency', 'int', 4)
>>> print var[0], var.id # Equivalent
1 1
>>> print var[2], var.type # Equivalent
int int
>>> var._asdict()
{'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
>>> v2 = var._replace(name='amplitude')
>>> v2
variable(id=1, name='amplitude', type='int', size=4)
Several places in the standard library that returned tuples have
been modified to return ‘namedtuple’ instances. For example, the
‘Decimal.as_tuple()’ method now returns a named tuple with ‘sign’,
‘digits’, and ‘exponent’ fields.
(Contributed by Raymond Hettinger.)
* Another change to the *note collections: 1e. module is that the
‘deque’ type now supports an optional `maxlen' parameter; if
supplied, the deque’s size will be restricted to no more than
`maxlen' items. Adding more items to a full deque causes old items
to be discarded.
>>> from collections import deque
>>> dq=deque(maxlen=3)
>>> dq
deque([], maxlen=3)
>>> dq.append(1); dq.append(2); dq.append(3)
>>> dq
deque([1, 2, 3], maxlen=3)
>>> dq.append(4)
>>> dq
deque([2, 3, 4], maxlen=3)
(Contributed by Raymond Hettinger.)
* The ‘Cookie’ module’s ‘Morsel’ objects now support an ‘httponly’
attribute. In some browsers. cookies with this attribute set
cannot be accessed or manipulated by JavaScript code. (Contributed
by Arvin Schnell; bpo-1638033(6).)
* A new window method in the *note curses: 2c. module, ‘chgat()’,
changes the display attributes for a certain number of characters
on a single line. (Contributed by Fabian Kreutz.)
# Boldface text starting at y=0,x=21
# and affecting the rest of the line.
stdscr.chgat(0, 21, curses.A_BOLD)
The ‘Textbox’ class in the *note curses.textpad: 2f. module now
supports editing in insert mode as well as overwrite mode. Insert
mode is enabled by supplying a true value for the `insert_mode'
parameter when creating the ‘Textbox’ instance.
* The *note datetime: 31. module’s ‘strftime()’ methods now support a
‘%f’ format code that expands to the number of microseconds in the
object, zero-padded on the left to six places. (Contributed by
Skip Montanaro; bpo-1158(7).)
* The *note decimal: 36. module was updated to version 1.66 of the
General Decimal Specification(8). New features include some
methods for some basic mathematical functions such as ‘exp()’ and
‘log10()’:
>>> Decimal(1).exp()
Decimal("2.718281828459045235360287471")
>>> Decimal("2.7182818").ln()
Decimal("0.9999999895305022877376682436")
>>> Decimal(1000).log10()
Decimal("3")
The ‘as_tuple()’ method of ‘Decimal’ objects now returns a named
tuple with ‘sign’, ‘digits’, and ‘exponent’ fields.
(Implemented by Facundo Batista and Mark Dickinson. Named tuple
support added by Raymond Hettinger.)
* The *note difflib: 37. module’s ‘SequenceMatcher’ class now returns
named tuples representing matches, with ‘a’, ‘b’, and ‘size’
attributes. (Contributed by Raymond Hettinger.)
* An optional ‘timeout’ parameter, specifying a timeout measured in
seconds, was added to the *note ftplib.FTP: 2f0. class constructor
as well as the ‘connect()’ method. (Added by Facundo Batista.)
Also, the ‘FTP’ class’s ‘storbinary()’ and ‘storlines()’ now take
an optional `callback' parameter that will be called with each
block of data after the data has been sent. (Contributed by Phil
Schwartz; bpo-1221598(9).)
* The ‘reduce()’ built-in function is also available in the *note
functools: 85. module. In Python 3.0, the builtin has been dropped
and ‘reduce()’ is only available from *note functools: 85.;
currently there are no plans to drop the builtin in the 2.x series.
(Patched by Christian Heimes; bpo-1739906(10).)
* When possible, the *note getpass: 88. module will now use
‘/dev/tty’ to print a prompt message and read the password, falling
back to standard error and standard input. If the password may be
echoed to the terminal, a warning is printed before the prompt is
displayed. (Contributed by Gregory P. Smith.)
* The *note glob.glob(): 5c6. function can now return Unicode
filenames if a Unicode path was used and Unicode filenames are
matched within the directory. (bpo-1001604(11))
* A new function in the *note heapq: 8e. module, ‘merge(iter1, iter2,
...)’, takes any number of iterables returning data in sorted
order, and returns a new generator that returns the contents of all
the iterators, also in sorted order. For example:
>>> list(heapq.merge([1, 3, 5, 9], [2, 8, 16]))
[1, 2, 3, 5, 8, 9, 16]
Another new function, ‘heappushpop(heap, item)’, pushes `item' onto
`heap', then pops off and returns the smallest item. This is more
efficient than making a call to ‘heappush()’ and then ‘heappop()’.
*note heapq: 8e. is now implemented to only use less-than
comparison, instead of the less-than-or-equal comparison it
previously used. This makes *note heapq: 8e.’s usage of a type
match the *note list.sort(): 445. method. (Contributed by Raymond
Hettinger.)
* An optional ‘timeout’ parameter, specifying a timeout measured in
seconds, was added to the ‘httplib.HTTPConnection’ and
‘HTTPSConnection’ class constructors. (Added by Facundo Batista.)
* Most of the *note inspect: a0. module’s functions, such as
‘getmoduleinfo()’ and ‘getargs()’, now return named tuples. In
addition to behaving like tuples, the elements of the return value
can also be accessed as attributes. (Contributed by Raymond
Hettinger.)
Some new functions in the module include ‘isgenerator()’,
‘isgeneratorfunction()’, and ‘isabstract()’.
* The *note itertools: a3. module gained several new functions.
‘izip_longest(iter1, iter2, ...[, fillvalue])’ makes tuples from
each of the elements; if some of the iterables are shorter than
others, the missing values are set to `fillvalue'. For example:
>>> tuple(itertools.izip_longest([1,2,3], [1,2,3,4,5]))
((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))
‘product(iter1, iter2, ..., [repeat=N])’ returns the Cartesian
product of the supplied iterables, a set of tuples containing every
possible combination of the elements returned from each iterable.
>>> list(itertools.product([1,2,3], [4,5,6]))
[(1, 4), (1, 5), (1, 6),
(2, 4), (2, 5), (2, 6),
(3, 4), (3, 5), (3, 6)]
The optional `repeat' keyword argument is used for taking the
product of an iterable or a set of iterables with themselves,
repeated `N' times. With a single iterable argument, `N'-tuples
are returned:
>>> list(itertools.product([1,2], repeat=3))
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
With two iterables, `2N'-tuples are returned.
>>> list(itertools.product([1,2], [3,4], repeat=2))
[(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
(1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
(2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
(2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]
‘combinations(iterable, r)’ returns sub-sequences of length `r'
from the elements of `iterable'.
>>> list(itertools.combinations('123', 2))
[('1', '2'), ('1', '3'), ('2', '3')]
>>> list(itertools.combinations('123', 3))
[('1', '2', '3')]
>>> list(itertools.combinations('1234', 3))
[('1', '2', '3'), ('1', '2', '4'),
('1', '3', '4'), ('2', '3', '4')]
‘permutations(iter[, r])’ returns all the permutations of length
`r' of the iterable’s elements. If `r' is not specified, it will
default to the number of elements produced by the iterable.
>>> list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2), (3, 4),
(4, 1), (4, 2), (4, 3)]
‘itertools.chain(*iterables)’ is an existing function in *note
itertools: a3. that gained a new constructor in Python 2.6.
‘itertools.chain.from_iterable(iterable)’ takes a single iterable
that should return other iterables. ‘chain()’ will then return all
the elements of the first iterable, then all the elements of the
second, and so on.
>>> list(itertools.chain.from_iterable([[1,2,3], [4,5,6]]))
[1, 2, 3, 4, 5, 6]
(All contributed by Raymond Hettinger.)
* The *note logging: aa. module’s ‘FileHandler’ class and its
subclasses ‘WatchedFileHandler’, ‘RotatingFileHandler’, and
‘TimedRotatingFileHandler’ now have an optional `delay' parameter
to their constructors. If `delay' is true, opening of the log file
is deferred until the first ‘emit()’ call is made. (Contributed by
Vinay Sajip.)
‘TimedRotatingFileHandler’ also has a `utc' constructor parameter.
If the argument is true, UTC time will be used in determining when
midnight occurs and in generating filenames; otherwise local time
will be used.
* Several new functions were added to the *note math: b1. module:
* *note isinf(): d3b. and *note isnan(): d3c. determine whether
a given float is a (positive or negative) infinity or a NaN
(Not a Number), respectively.
* *note copysign(): d3d. copies the sign bit of an IEEE 754
number, returning the absolute value of `x' combined with the
sign bit of `y'. For example, ‘math.copysign(1, -0.0)’
returns -1.0. (Contributed by Christian Heimes.)
* *note factorial(): 1ea. computes the factorial of a number.
(Contributed by Raymond Hettinger; bpo-2138(12).)
* *note fsum(): d3e. adds up the stream of numbers from an
iterable, and is careful to avoid loss of precision through
using partial sums. (Contributed by Jean Brouwers, Raymond
Hettinger, and Mark Dickinson; bpo-2819(13).)
* *note acosh(): d3f, *note asinh(): d40. and *note atanh():
d41. compute the inverse hyperbolic functions.
* *note log1p(): d42. returns the natural logarithm of `1+x'
(base `e').
* ‘trunc()’ rounds a number toward zero, returning the closest
‘Integral’ that’s between the function’s argument and zero.
Added as part of the backport of *note PEP 3141’s type
hierarchy for numbers: c57.
* The *note math: b1. module has been improved to give more
consistent behaviour across platforms, especially with respect to
handling of floating-point exceptions and IEEE 754 special values.
Whenever possible, the module follows the recommendations of the
C99 standard about 754’s special values. For example, ‘sqrt(-1.)’
should now give a *note ValueError: 1fb. across almost all
platforms, while ‘sqrt(float('NaN'))’ should return a NaN on all
IEEE 754 platforms. Where Annex ‘F’ of the C99 standard recommends
signaling ‘divide-by-zero’ or ‘invalid’, Python will raise *note
ValueError: 1fb. Where Annex ‘F’ of the C99 standard recommends
signaling ‘overflow’, Python will raise *note OverflowError: 960.
(See bpo-711019(14) and bpo-1640(15).)
(Contributed by Christian Heimes and Mark Dickinson.)
* *note mmap: 1ec. objects now have a ‘rfind()’ method that searches
for a substring beginning at the end of the string and searching
backwards. The ‘find()’ method also gained an `end' parameter
giving an index at which to stop searching. (Contributed by John
Lenton.)
* The *note operator: c2. module gained a ‘methodcaller()’ function
that takes a name and an optional set of arguments, returning a
callable that will call the named function on any arguments passed
to it. For example:
>>> # Equivalent to lambda s: s.replace('old', 'new')
>>> replacer = operator.methodcaller('replace', 'old', 'new')
>>> replacer('old wine in old bottles')
'new wine in new bottles'
(Contributed by Georg Brandl, after a suggestion by Gregory
Petrosyan.)
The ‘attrgetter()’ function now accepts dotted names and performs
the corresponding attribute lookups:
>>> inst_name = operator.attrgetter(
... '__class__.__name__')
>>> inst_name('')
'str'
>>> inst_name(help)
'_Helper'
(Contributed by Georg Brandl, after a suggestion by Barry Warsaw.)
* The *note os: c4. module now wraps several new system calls.
‘fchmod(fd, mode)’ and ‘fchown(fd, uid, gid)’ change the mode and
ownership of an opened file, and ‘lchmod(path, mode)’ changes the
mode of a symlink. (Contributed by Georg Brandl and Christian
Heimes.)
‘chflags()’ and ‘lchflags()’ are wrappers for the corresponding
system calls (where they’re available), changing the flags set on a
file. Constants for the flag values are defined in the *note stat:
f4. module; some possible values include ‘UF_IMMUTABLE’ to signal
the file may not be changed and ‘UF_APPEND’ to indicate that data
can only be appended to the file. (Contributed by M. Levinson.)
‘os.closerange(low, high)’ efficiently closes all file descriptors
from `low' to `high', ignoring any errors and not including `high'
itself. This function is now used by the *note subprocess: f9.
module to make starting processes faster. (Contributed by Georg
Brandl; bpo-1663329(16).)
* The ‘os.environ’ object’s ‘clear()’ method will now unset the
environment variables using *note os.unsetenv(): d43. in addition
to clearing the object’s keys. (Contributed by Martin Horcicka;
bpo-1181(17).)
* The *note os.walk(): 654. function now has a ‘followlinks’
parameter. If set to True, it will follow symlinks pointing to
directories and visit the directory’s contents. For backward
compatibility, the parameter’s default value is false. Note that
the function can fall into an infinite recursion if there’s a
symlink that points to a parent directory. (bpo-1273829(18))
* In the *note os.path: c5. module, the ‘splitext()’ function has
been changed to not split on leading period characters. This
produces better results when operating on Unix’s dot-files. For
example, ‘os.path.splitext('.ipython')’ now returns ‘('.ipython',
'')’ instead of ‘('', '.ipython')’. (bpo-1115886(19))
A new function, ‘os.path.relpath(path, start='.')’, returns a
relative path from the ‘start’ path, if it’s supplied, or from the
current working directory to the destination ‘path’. (Contributed
by Richard Barran; bpo-1339796(20).)
On Windows, *note os.path.expandvars(): d44. will now expand
environment variables given in the form “%var%”, and “~user” will
be expanded into the user’s home directory path. (Contributed by
Josiah Carlson; bpo-957650(21).)
* The Python debugger provided by the *note pdb: c9. module gained a
new command: “run” restarts the Python program being debugged and
can optionally take new command-line arguments for the program.
(Contributed by Rocky Bernstein; bpo-1393667(22).)
* The *note pdb.post_mortem(): d45. function, used to begin debugging
a traceback, will now use the traceback returned by *note
sys.exc_info(): c5f. if no traceback is supplied. (Contributed by
Facundo Batista; bpo-1106316(23).)
* The *note pickletools: cb. module now has an ‘optimize()’ function
that takes a string containing a pickle and removes some unused
opcodes, returning a shorter pickle that contains the same data
structure. (Contributed by Raymond Hettinger.)
* A ‘get_data()’ function was added to the *note pkgutil: cd. module
that returns the contents of resource files included with an
installed Python package. For example:
>>> import pkgutil
>>> print pkgutil.get_data('test', 'exception_hierarchy.txt')
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
...
(Contributed by Paul Moore; bpo-2439(24).)
* The ‘pyexpat’ module’s ‘Parser’ objects now allow setting their
‘buffer_size’ attribute to change the size of the buffer used to
hold character data. (Contributed by Achim Gaedke; bpo-1137(25).)
* The ‘Queue’ module now provides queue variants that retrieve
entries in different orders. The ‘PriorityQueue’ class stores
queued items in a heap and retrieves them in priority order, and
‘LifoQueue’ retrieves the most recently added entries first,
meaning that it behaves like a stack. (Contributed by Raymond
Hettinger.)
* The *note random: dc. module’s ‘Random’ objects can now be pickled
on a 32-bit system and unpickled on a 64-bit system, and vice
versa. Unfortunately, this change also means that Python 2.6’s
‘Random’ objects can’t be unpickled correctly on earlier versions
of Python. (Contributed by Shawn Ligocki; bpo-1727780(26).)
The new ‘triangular(low, high, mode)’ function returns random
numbers following a triangular distribution. The returned values
are between `low' and `high', not including `high' itself, and with
`mode' as the most frequently occurring value in the distribution.
(Contributed by Wladmir van der Laan and Raymond Hettinger;
bpo-1681432(27).)
* Long regular expression searches carried out by the *note re: dd.
module will check for signals being delivered, so time-consuming
searches can now be interrupted. (Contributed by Josh Hoyt and
Ralf Schmitt; bpo-846388(28).)
The regular expression module is implemented by compiling bytecodes
for a tiny regex-specific virtual machine. Untrusted code could
create malicious strings of bytecode directly and cause crashes, so
Python 2.6 includes a verifier for the regex bytecode.
(Contributed by Guido van Rossum from work for Google App Engine;
bpo-3487(29).)
* The *note rlcompleter: e1. module’s ‘Completer.complete()’ method
will now ignore exceptions triggered while evaluating a name.
(Fixed by Lorenz Quack; bpo-2250(30).)
* The *note sched: e3. module’s ‘scheduler’ instances now have a
read-only *note queue: da. attribute that returns the contents of
the scheduler’s queue, represented as a list of named tuples with
the fields ‘(time, priority, action, argument)’. (Contributed by
Raymond Hettinger; bpo-1861(31).)
* The *note select: e5. module now has wrapper functions for the
Linux ‘epoll()’ and BSD ‘kqueue()’ system calls. ‘modify()’ method
was added to the existing ‘poll’ objects; ‘pollobj.modify(fd,
eventmask)’ takes a file descriptor or file object and an event
mask, modifying the recorded event mask for that file.
(Contributed by Christian Heimes; bpo-1657(32).)
* The *note shutil.copytree(): 21a. function now has an optional
`ignore' argument that takes a callable object. This callable will
receive each directory path and a list of the directory’s contents,
and returns a list of names that will be ignored, not copied.
The *note shutil: e9. module also provides an ‘ignore_patterns()’
function for use with this new parameter. ‘ignore_patterns()’
takes an arbitrary number of glob-style patterns and returns a
callable that will ignore any files and directories that match any
of these patterns. The following example copies a directory tree,
but skips both ‘.svn’ directories and Emacs backup files, which
have names ending with ‘~’:
shutil.copytree('Doc/library', '/tmp/library',
ignore=shutil.ignore_patterns('*~', '.svn'))
(Contributed by Tarek Ziadé; bpo-2663(33).)
* Integrating signal handling with GUI handling event loops like
those used by Tkinter or GTk+ has long been a problem; most
software ends up polling, waking up every fraction of a second to
check if any GUI events have occurred. The *note signal: ea.
module can now make this more efficient. Calling
‘signal.set_wakeup_fd(fd)’ sets a file descriptor to be used; when
a signal is received, a byte is written to that file descriptor.
There’s also a C-level function, *note PySignal_SetWakeupFd(): d46,
for setting the descriptor.
Event loops will use this by opening a pipe to create two
descriptors, one for reading and one for writing. The writable
descriptor will be passed to ‘set_wakeup_fd()’, and the readable
descriptor will be added to the list of descriptors monitored by
the event loop via ‘select()’ or ‘poll()’. On receiving a signal,
a byte will be written and the main event loop will be woken up,
avoiding the need to poll.
(Contributed by Adam Olsen; bpo-1583(34).)
The ‘siginterrupt()’ function is now available from Python code,
and allows changing whether signals can interrupt system calls or
not. (Contributed by Ralf Schmitt.)
The ‘setitimer()’ and ‘getitimer()’ functions have also been added
(where they’re available). ‘setitimer()’ allows setting interval
timers that will cause a signal to be delivered to the process
after a specified time, measured in wall-clock time, consumed
process time, or combined process+system time. (Contributed by
Guilherme Polo; bpo-2240(35).)
* The *note smtplib: ed. module now supports SMTP over SSL thanks to
the addition of the ‘SMTP_SSL’ class. This class supports an
interface identical to the existing ‘SMTP’ class. (Contributed by
Monty Taylor.) Both class constructors also have an optional
‘timeout’ parameter that specifies a timeout for the initial
connection attempt, measured in seconds. (Contributed by Facundo
Batista.)
An implementation of the LMTP protocol ( RFC 2033(36)) was also
added to the module. LMTP is used in place of SMTP when
transferring e-mail between agents that don’t manage a mail queue.
(LMTP implemented by Leif Hedstrom; bpo-957003(37).)
‘SMTP.starttls()’ now complies with RFC 3207(38) and forgets any
knowledge obtained from the server not obtained from the TLS
negotiation itself. (Patch contributed by Bill Fenner;
bpo-829951(39).)
* The *note socket: ef. module now supports TIPC
(‘http://tipc.sourceforge.net/’), a high-performance non-IP-based
protocol designed for use in clustered environments. TIPC
addresses are 4- or 5-tuples. (Contributed by Alberto Bertogli;
bpo-1646(40).)
A new function, ‘create_connection()’, takes an address and
connects to it using an optional timeout value, returning the
connected socket object. This function also looks up the address’s
type and connects to it using IPv4 or IPv6 as appropriate.
Changing your code to use ‘create_connection()’ instead of
‘socket(socket.AF_INET, ...)’ may be all that’s required to make
your code work with IPv6.
* The base classes in the ‘SocketServer’ module now support calling a
‘handle_timeout()’ method after a span of inactivity specified by
the server’s ‘timeout’ attribute. (Contributed by Michael
Pomraning.) The ‘serve_forever()’ method now takes an optional
poll interval measured in seconds, controlling how often the server
will check for a shutdown request. (Contributed by Pedro Werneck
and Jeffrey Yasskin; bpo-742598(41), bpo-1193577(42).)
* The *note sqlite3: f2. module, maintained by Gerhard Häring, has
been updated from version 2.3.2 in Python 2.5 to version 2.4.1.
* The *note struct: f8. module now supports the C99 ‘_Bool’ type,
using the format character ‘'?'’. (Contributed by David Remahl.)
* The ‘Popen’ objects provided by the *note subprocess: f9. module
now have ‘terminate()’, ‘kill()’, and ‘send_signal()’ methods. On
Windows, ‘send_signal()’ only supports the ‘SIGTERM’ signal, and
all these methods are aliases for the Win32 API function
‘TerminateProcess()’. (Contributed by Christian Heimes.)
* A new variable in the *note sys: fd. module, ‘float_info’, is an
object containing information derived from the ‘float.h’ file about
the platform’s floating-point support. Attributes of this object
include ‘mant_dig’ (number of digits in the mantissa), ‘epsilon’
(smallest difference between 1.0 and the next largest value
representable), and several others. (Contributed by Christian
Heimes; bpo-1534(43).)
Another new variable, ‘dont_write_bytecode’, controls whether
Python writes any ‘.pyc’ or ‘.pyo’ files on importing a module. If
this variable is true, the compiled files are not written. The
variable is initially set on start-up by supplying the *note -B:
d38. switch to the Python interpreter, or by setting the *note
PYTHONDONTWRITEBYTECODE: d39. environment variable before running
the interpreter. Python code can subsequently change the value of
this variable to control whether bytecode files are written or not.
(Contributed by Neal Norwitz and Georg Brandl.)
Information about the command-line arguments supplied to the Python
interpreter is available by reading attributes of a named tuple
available as ‘sys.flags’. For example, the ‘verbose’ attribute is
true if Python was executed in verbose mode, ‘debug’ is true in
debugging mode, etc. These attributes are all read-only.
(Contributed by Christian Heimes.)
A new function, ‘getsizeof()’, takes a Python object and returns
the amount of memory used by the object, measured in bytes.
Built-in objects return correct results; third-party extensions may
not, but can define a ‘__sizeof__()’ method to return the object’s
size. (Contributed by Robert Schuppenies; bpo-2898(44).)
It’s now possible to determine the current profiler and tracer
functions by calling *note sys.getprofile(): d47. and *note
sys.gettrace(): d48. (Contributed by Georg Brandl; bpo-1648(45).)
* The *note tarfile: 101. module now supports POSIX.1-2001 (pax)
tarfiles in addition to the POSIX.1-1988 (ustar) and GNU tar
formats that were already supported. The default format is GNU
tar; specify the ‘format’ parameter to open a file using a
different format:
tar = tarfile.open("output.tar", "w",
format=tarfile.PAX_FORMAT)
The new ‘encoding’ and ‘errors’ parameters specify an encoding and
an error handling scheme for character conversions. ‘'strict'’,
‘'ignore'’, and ‘'replace'’ are the three standard ways Python can
handle errors,; ‘'utf-8'’ is a special value that replaces bad
characters with their UTF-8 representation. (Character conversions
occur because the PAX format supports Unicode filenames, defaulting
to UTF-8 encoding.)
The ‘TarFile.add()’ method now accepts an ‘exclude’ argument that’s
a function that can be used to exclude certain filenames from an
archive. The function must take a filename and return true if the
file should be excluded or false if it should be archived. The
function is applied to both the name initially passed to ‘add()’
and to the names of files in recursively-added directories.
(All changes contributed by Lars Gustäbel).
* An optional ‘timeout’ parameter was added to the *note
telnetlib.Telnet: 597. class constructor, specifying a timeout
measured in seconds. (Added by Facundo Batista.)
* The *note tempfile.NamedTemporaryFile: d49. class usually deletes
the temporary file it created when the file is closed. This
behaviour can now be changed by passing ‘delete=False’ to the
constructor. (Contributed by Damien Miller; bpo-1537850(46).)
A new class, ‘SpooledTemporaryFile’, behaves like a temporary file
but stores its data in memory until a maximum size is exceeded. On
reaching that limit, the contents will be written to an on-disk
temporary file. (Contributed by Dustin J. Mitchell.)
The ‘NamedTemporaryFile’ and ‘SpooledTemporaryFile’ classes both
work as context managers, so you can write ‘with
tempfile.NamedTemporaryFile() as tmp: ...’. (Contributed by
Alexander Belopolsky; bpo-2021(47).)
* The ‘test.test_support’ module gained a number of context managers
useful for writing tests. ‘EnvironmentVarGuard()’ is a context
manager that temporarily changes environment variables and
automatically restores them to their old values.
Another context manager, ‘TransientResource’, can surround calls to
resources that may or may not be available; it will catch and
ignore a specified list of exceptions. For example, a network test
may ignore certain failures when connecting to an external web
site:
with test_support.TransientResource(IOError,
errno=errno.ETIMEDOUT):
f = urllib.urlopen('https://sf.net')
...
Finally, ‘check_warnings()’ resets the ‘warning’ module’s warning
filters and returns an object that will record all warning messages
triggered (bpo-3781(48)):
with test_support.check_warnings() as wrec:
warnings.simplefilter("always")
# ... code that triggers a warning ...
assert str(wrec.message) == "function is outdated"
assert len(wrec.warnings) == 1, "Multiple warnings raised"
(Contributed by Brett Cannon.)
* The *note textwrap: 108. module can now preserve existing
whitespace at the beginnings and ends of the newly-created lines by
specifying ‘drop_whitespace=False’ as an argument:
>>> S = """This sentence has a bunch of
... extra whitespace."""
>>> print textwrap.fill(S, width=15)
This sentence
has a bunch
of extra
whitespace.
>>> print textwrap.fill(S, drop_whitespace=False, width=15)
This sentence
has a bunch
of extra
whitespace.
>>>
(Contributed by Dwayne Bailey; bpo-1581073(49).)
* The *note threading: 109. module API is being changed to use
properties such as ‘daemon’ instead of ‘setDaemon()’ and
‘isDaemon()’ methods, and some methods have been renamed to use
underscores instead of camel-case; for example, the ‘activeCount()’
method is renamed to ‘active_count()’. Both the 2.6 and 3.0
versions of the module support the same properties and renamed
methods, but don’t remove the old methods. No date has been set
for the deprecation of the old APIs in Python 3.x; the old APIs
won’t be removed in any 2.x version. (Carried out by several
people, most notably Benjamin Peterson.)
The *note threading: 109. module’s ‘Thread’ objects gained an
‘ident’ property that returns the thread’s identifier, a nonzero
integer. (Contributed by Gregory P. Smith; bpo-2871(50).)
* The *note timeit: 10b. module now accepts callables as well as
strings for the statement being timed and for the setup code. Two
convenience functions were added for creating ‘Timer’ instances:
‘repeat(stmt, setup, time, repeat, number)’ and ‘timeit(stmt,
setup, time, number)’ create an instance and call the corresponding
method. (Contributed by Erik Demaine; bpo-1533909(51).)
* The ‘Tkinter’ module now accepts lists and tuples for options,
separating the elements by spaces before passing the resulting
value to Tcl/Tk. (Contributed by Guilherme Polo; bpo-2906(52).)
* The *note turtle: 116. module for turtle graphics was greatly
enhanced by Gregor Lingl. New features in the module include:
* Better animation of turtle movement and rotation.
* Control over turtle movement using the new ‘delay()’,
‘tracer()’, and ‘speed()’ methods.
* The ability to set new shapes for the turtle, and to define a
new coordinate system.
* Turtles now have an ‘undo()’ method that can roll back
actions.
* Simple support for reacting to input events such as mouse and
keyboard activity, making it possible to write simple games.
* A ‘turtle.cfg’ file can be used to customize the starting
appearance of the turtle’s screen.
* The module’s docstrings can be replaced by new docstrings that
have been translated into another language.
(bpo-1513695(53))
* An optional ‘timeout’ parameter was added to the ‘urllib.urlopen()’
function and the ‘urllib.ftpwrapper’ class constructor, as well as
the ‘urllib2.urlopen()’ function. The parameter specifies a
timeout measured in seconds. For example:
>>> u = urllib2.urlopen("http://slow.example.com",
timeout=3)
Traceback (most recent call last):
...
urllib2.URLError:
>>>
(Added by Facundo Batista.)
* The Unicode database provided by the *note unicodedata: 11a. module
has been updated to version 5.1.0. (Updated by Martin von Löwis;
bpo-3811(54).)
* The *note warnings: 126. module’s ‘formatwarning()’ and
‘showwarning()’ gained an optional `line' argument that can be used
to supply the line of source code. (Added as part of
bpo-1631171(55), which re-implemented part of the *note warnings:
126. module in C code.)
A new function, ‘catch_warnings()’, is a context manager intended
for testing purposes that lets you temporarily modify the warning
filters and then restore their original values (bpo-3781(56)).
* The XML-RPC ‘SimpleXMLRPCServer’ and ‘DocXMLRPCServer’ classes can
now be prevented from immediately opening and binding to their
socket by passing ‘False’ as the `bind_and_activate' constructor
parameter. This can be used to modify the instance’s
‘allow_reuse_address’ attribute before calling the ‘server_bind()’
and ‘server_activate()’ methods to open the socket and begin
listening for connections. (Contributed by Peter Parente;
bpo-1599845(57).)
‘SimpleXMLRPCServer’ also has a ‘_send_traceback_header’ attribute;
if true, the exception and formatted traceback are returned as HTTP
headers “X-Exception” and “X-Traceback”. This feature is for
debugging purposes only and should not be used on production
servers because the tracebacks might reveal passwords or other
sensitive information. (Contributed by Alan McIntyre as part of
his project for Google’s Summer of Code 2007.)
* The ‘xmlrpclib’ module no longer automatically converts *note
datetime.date: 193. and *note datetime.time: 4f3. to the
‘xmlrpclib.DateTime’ type; the conversion semantics were not
necessarily correct for all applications. Code using ‘xmlrpclib’
should convert ‘date’ and *note time: 4f3. instances.
(bpo-1330538(58)) The code can also handle dates before 1900
(contributed by Ralf Schmitt; bpo-2014(59)) and 64-bit integers
represented by using ‘’ in XML-RPC responses (contributed by
Riku Lindblad; bpo-2985(60)).
* The *note zipfile: 142. module’s ‘ZipFile’ class now has
‘extract()’ and ‘extractall()’ methods that will unpack a single
file or all the files in the archive to the current directory, or
to a specified directory:
z = zipfile.ZipFile('python-251.zip')
# Unpack a single file, writing it relative
# to the /tmp directory.
z.extract('Python/sysmodule.c', '/tmp')
# Unpack all the files in the archive.
z.extractall()
(Contributed by Alan McIntyre; bpo-467924(61).)
The *note open(): 4f0, ‘read()’ and ‘extract()’ methods can now
take either a filename or a ‘ZipInfo’ object. This is useful when
an archive accidentally contains a duplicated filename.
(Contributed by Graham Horler; bpo-1775025(62).)
Finally, *note zipfile: 142. now supports using Unicode filenames
for archived files. (Contributed by Alexey Borzenkov;
bpo-1734346(63).)
* Menu:
* The ast module::
* The future_builtins module::
* The json module; JavaScript Object Notation: The json module JavaScript Object Notation.
* The plistlib module; A Property-List Parser: The plistlib module A Property-List Parser.
* ctypes Enhancements::
* Improved SSL Support::
---------- Footnotes ----------
(1) https://bugs.python.org/issue1736190
(2) https://www.jcea.es/programacion/pybsddb.htm
(3) https://bugs.python.org/issue1817
(4) https://bugs.python.org/issue600362
(5) https://bugs.python.org/issue1381
(6) https://bugs.python.org/issue1638033
(7) https://bugs.python.org/issue1158
(8) http://speleotrove.com/decimal/decarith.html
(9) https://bugs.python.org/issue1221598
(10) https://bugs.python.org/issue1739906
(11) https://bugs.python.org/issue1001604
(12) https://bugs.python.org/issue2138
(13) https://bugs.python.org/issue2819
(14) https://bugs.python.org/issue711019
(15) https://bugs.python.org/issue1640
(16) https://bugs.python.org/issue1663329
(17) https://bugs.python.org/issue1181
(18) https://bugs.python.org/issue1273829
(19) https://bugs.python.org/issue1115886
(20) https://bugs.python.org/issue1339796
(21) https://bugs.python.org/issue957650
(22) https://bugs.python.org/issue1393667
(23) https://bugs.python.org/issue1106316
(24) https://bugs.python.org/issue2439
(25) https://bugs.python.org/issue1137
(26) https://bugs.python.org/issue1727780
(27) https://bugs.python.org/issue1681432
(28) https://bugs.python.org/issue846388
(29) https://bugs.python.org/issue3487
(30) https://bugs.python.org/issue2250
(31) https://bugs.python.org/issue1861
(32) https://bugs.python.org/issue1657
(33) https://bugs.python.org/issue2663
(34) https://bugs.python.org/issue1583
(35) https://bugs.python.org/issue2240
(36) https://tools.ietf.org/html/rfc2033.html
(37) https://bugs.python.org/issue957003
(38) https://tools.ietf.org/html/rfc3207.html
(39) https://bugs.python.org/issue829951
(40) https://bugs.python.org/issue1646
(41) https://bugs.python.org/issue742598
(42) https://bugs.python.org/issue1193577
(43) https://bugs.python.org/issue1534
(44) https://bugs.python.org/issue2898
(45) https://bugs.python.org/issue1648
(46) https://bugs.python.org/issue1537850
(47) https://bugs.python.org/issue2021
(48) https://bugs.python.org/issue3781
(49) https://bugs.python.org/issue1581073
(50) https://bugs.python.org/issue2871
(51) https://bugs.python.org/issue1533909
(52) https://bugs.python.org/issue2906
(53) https://bugs.python.org/issue1513695
(54) https://bugs.python.org/issue3811
(55) https://bugs.python.org/issue1631171
(56) https://bugs.python.org/issue3781
(57) https://bugs.python.org/issue1599845
(58) https://bugs.python.org/issue1330538
(59) https://bugs.python.org/issue2014
(60) https://bugs.python.org/issue2985
(61) https://bugs.python.org/issue467924
(62) https://bugs.python.org/issue1775025
(63) https://bugs.python.org/issue1734346
File: python.info, Node: The ast module, Next: The future_builtins module, Up: New and Improved Modules<2>
1.11.18.1 The ‘ast’ module
..........................
The *note ast: 8. module provides an Abstract Syntax Tree representation
of Python code, and Armin Ronacher contributed a set of helper functions
that perform a variety of common tasks. These will be useful for HTML
templating packages, code analyzers, and similar tools that process
Python code.
The ‘parse()’ function takes an expression and returns an AST. The
‘dump()’ function outputs a representation of a tree, suitable for
debugging:
import ast
t = ast.parse("""
d = {}
for i in 'abcdefghijklm':
d[i + i] = ord(i) - ord('a') + 1
print d
""")
print ast.dump(t)
This outputs a deeply nested tree:
Module(body=[
Assign(targets=[
Name(id='d', ctx=Store())
], value=Dict(keys=[], values=[]))
For(target=Name(id='i', ctx=Store()),
iter=Str(s='abcdefghijklm'), body=[
Assign(targets=[
Subscript(value=
Name(id='d', ctx=Load()),
slice=
Index(value=
BinOp(left=Name(id='i', ctx=Load()), op=Add(),
right=Name(id='i', ctx=Load()))), ctx=Store())
], value=
BinOp(left=
BinOp(left=
Call(func=
Name(id='ord', ctx=Load()), args=[
Name(id='i', ctx=Load())
], keywords=[], starargs=None, kwargs=None),
op=Sub(), right=Call(func=
Name(id='ord', ctx=Load()), args=[
Str(s='a')
], keywords=[], starargs=None, kwargs=None)),
op=Add(), right=Num(n=1)))
], orelse=[])
Print(dest=None, values=[
Name(id='d', ctx=Load())
], nl=True)
])
The ‘literal_eval()’ method takes a string or an AST representing a
literal expression, parses and evaluates it, and returns the resulting
value. A literal expression is a Python expression containing only
strings, numbers, dictionaries, etc. but no statements or function
calls. If you need to evaluate an expression but cannot accept the
security risk of using an *note eval(): b90. call, ‘literal_eval()’ will
handle it safely:
>>> literal = '("a", "b", {2:4, 3:8, 1:2})'
>>> print ast.literal_eval(literal)
('a', 'b', {1: 2, 2: 4, 3: 8})
>>> print ast.literal_eval('"a" + "b"')
Traceback (most recent call last):
...
ValueError: malformed string
The module also includes ‘NodeVisitor’ and ‘NodeTransformer’ classes for
traversing and modifying an AST, and functions for common
transformations such as changing line numbers.
File: python.info, Node: The future_builtins module, Next: The json module JavaScript Object Notation, Prev: The ast module, Up: New and Improved Modules<2>
1.11.18.2 The ‘future_builtins’ module
......................................
Python 3.0 makes many changes to the repertoire of built-in functions,
and most of the changes can’t be introduced in the Python 2.x series
because they would break compatibility. The ‘future_builtins’ module
provides versions of these built-in functions that can be imported when
writing 3.0-compatible code.
The functions in this module currently include:
* ‘ascii(obj)’: equivalent to *note repr(): 7cc. In Python 3.0,
*note repr(): 7cc. will return a Unicode string, while *note
ascii(): d4c. will return a pure ASCII bytestring.
* ‘filter(predicate, iterable)’, ‘map(func, iterable1, ...)’: the 3.0
versions return iterators, unlike the 2.x builtins which return
lists.
* ‘hex(value)’, ‘oct(value)’: instead of calling the ‘__hex__()’ or
‘__oct__()’ methods, these versions will call the *note
__index__(): 18a. method and convert the result to hexadecimal or
octal. *note oct(): c65. will use the new ‘0o’ notation for its
result.
File: python.info, Node: The json module JavaScript Object Notation, Next: The plistlib module A Property-List Parser, Prev: The future_builtins module, Up: New and Improved Modules<2>
1.11.18.3 The ‘json’ module: JavaScript Object Notation
.......................................................
The new *note json: a4. module supports the encoding and decoding of
Python types in JSON (Javascript Object Notation). JSON is a
lightweight interchange format often used in web applications. For more
information about JSON, see ‘http://www.json.org’.
*note json: a4. comes with support for decoding and encoding most
built-in Python types. The following example encodes and decodes a
dictionary:
>>> import json
>>> data = {"spam": "foo", "parrot": 42}
>>> in_json = json.dumps(data) # Encode the data
>>> in_json
'{"parrot": 42, "spam": "foo"}'
>>> json.loads(in_json) # Decode into a Python object
{"spam": "foo", "parrot": 42}
It’s also possible to write your own decoders and encoders to support
more types. Pretty-printing of the JSON strings is also supported.
*note json: a4. (originally called simplejson) was written by Bob
Ippolito.
File: python.info, Node: The plistlib module A Property-List Parser, Next: ctypes Enhancements, Prev: The json module JavaScript Object Notation, Up: New and Improved Modules<2>
1.11.18.4 The ‘plistlib’ module: A Property-List Parser
.......................................................
The ‘.plist’ format is commonly used on Mac OS X to store basic data
types (numbers, strings, lists, and dictionaries) by serializing them
into an XML-based format. It resembles the XML-RPC serialization of
data types.
Despite being primarily used on Mac OS X, the format has nothing
Mac-specific about it and the Python implementation works on any
platform that Python supports, so the *note plistlib: cf. module has
been promoted to the standard library.
Using the module is simple:
import sys
import plistlib
import datetime
# Create data structure
data_struct = dict(lastAccessed=datetime.datetime.now(),
version=1,
categories=('Personal','Shared','Private'))
# Create string containing XML.
plist_str = plistlib.writePlistToString(data_struct)
new_struct = plistlib.readPlistFromString(plist_str)
print data_struct
print new_struct
# Write data structure to a file and read it back.
plistlib.writePlist(data_struct, '/tmp/customizations.plist')
new_struct = plistlib.readPlist('/tmp/customizations.plist')
# read/writePlist accepts file-like objects as well as paths.
plistlib.writePlist(data_struct, sys.stdout)
File: python.info, Node: ctypes Enhancements, Next: Improved SSL Support, Prev: The plistlib module A Property-List Parser, Up: New and Improved Modules<2>
1.11.18.5 ctypes Enhancements
.............................
Thomas Heller continued to maintain and enhance the *note ctypes: 2b.
module.
*note ctypes: 2b. now supports a ‘c_bool’ datatype that represents the
C99 ‘bool’ type. (Contributed by David Remahl; bpo-1649190(1).)
The *note ctypes: 2b. string, buffer and array types have improved
support for extended slicing syntax, where various combinations of
‘(start, stop, step)’ are supplied. (Implemented by Thomas Wouters.)
All *note ctypes: 2b. data types now support ‘from_buffer()’ and
‘from_buffer_copy()’ methods that create a ctypes instance based on a
provided buffer object. ‘from_buffer_copy()’ copies the contents of the
object, while ‘from_buffer()’ will share the same memory area.
A new calling convention tells *note ctypes: 2b. to clear the ‘errno’ or
Win32 LastError variables at the outset of each wrapped call.
(Implemented by Thomas Heller; bpo-1798(2).)
You can now retrieve the Unix ‘errno’ variable after a function call.
When creating a wrapped function, you can supply ‘use_errno=True’ as a
keyword parameter to the ‘DLL()’ function and then call the module-level
methods ‘set_errno()’ and ‘get_errno()’ to set and retrieve the error
value.
The Win32 LastError variable is similarly supported by the ‘DLL()’,
‘OleDLL()’, and ‘WinDLL()’ functions. You supply ‘use_last_error=True’
as a keyword parameter and then call the module-level methods
‘set_last_error()’ and ‘get_last_error()’.
The ‘byref()’ function, used to retrieve a pointer to a ctypes instance,
now has an optional `offset' parameter that is a byte count that will be
added to the returned pointer.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1649190
(2) https://bugs.python.org/issue1798
File: python.info, Node: Improved SSL Support, Prev: ctypes Enhancements, Up: New and Improved Modules<2>
1.11.18.6 Improved SSL Support
..............................
Bill Janssen made extensive improvements to Python 2.6’s support for the
Secure Sockets Layer by adding a new module, *note ssl: f3, that’s built
atop the OpenSSL(1) library. This new module provides more control over
the protocol negotiated, the X.509 certificates used, and has better
support for writing SSL servers (as opposed to clients) in Python. The
existing SSL support in the *note socket: ef. module hasn’t been removed
and continues to work, though it will be removed in Python 3.0.
To use the new module, you must first create a TCP connection in the
usual way and then pass it to the *note ssl.wrap_socket(): 46f.
function. It’s possible to specify whether a certificate is required,
and to obtain certificate info by calling the ‘getpeercert()’ method.
See also
........
The documentation for the *note ssl: f3. module.
---------- Footnotes ----------
(1) https://www.openssl.org/
File: python.info, Node: Deprecations and Removals, Next: Build and C API Changes<9>, Prev: New and Improved Modules<2>, Up: What’s New in Python 2 6
1.11.19 Deprecations and Removals
---------------------------------
* String exceptions have been removed. Attempting to use them raises
a *note TypeError: 192.
* Changes to the *note Exception: 1a9. interface as dictated by PEP
352(1) continue to be made. For 2.6, the ‘message’ attribute is
being deprecated in favor of the ‘args’ attribute.
* (3.0-warning mode) Python 3.0 will feature a reorganized standard
library that will drop many outdated modules and rename others.
Python 2.6 running in 3.0-warning mode will warn about these
modules when they are imported.
The list of deprecated modules is: ‘audiodev’, ‘bgenlocations’,
‘buildtools’, ‘bundlebuilder’, ‘Canvas’, ‘compiler’, ‘dircache’,
‘dl’, ‘fpformat’, ‘gensuitemodule’, ‘ihooks’, ‘imageop’, ‘imgfile’,
‘linuxaudiodev’, ‘mhlib’, ‘mimetools’, ‘multifile’, ‘new’, ‘pure’,
‘statvfs’, ‘sunaudiodev’, ‘test.testall’, and ‘toaiff’.
* The ‘gopherlib’ module has been removed.
* The ‘MimeWriter’ module and ‘mimify’ module have been deprecated;
use the *note email: 69. package instead.
* The ‘md5’ module has been deprecated; use the *note hashlib: 8d.
module instead.
* The ‘posixfile’ module has been deprecated; *note fcntl.lockf():
d52. provides better locking.
* The ‘popen2’ module has been deprecated; use the *note subprocess:
f9. module.
* The ‘rgbimg’ module has been removed.
* The ‘sets’ module has been deprecated; it’s better to use the
built-in *note set: b6f. and *note frozenset: bee. types.
* The ‘sha’ module has been deprecated; use the *note hashlib: 8d.
module instead.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0352
File: python.info, Node: Build and C API Changes<9>, Next: Porting to Python 2 6, Prev: Deprecations and Removals, Up: What’s New in Python 2 6
1.11.20 Build and C API Changes
-------------------------------
Changes to Python’s build process and to the C API include:
* Python now must be compiled with C89 compilers (after 19 years!).
This means that the Python source tree has dropped its own
implementations of ‘memmove()’ and ‘strerror()’, which are in the
C89 standard library.
* Python 2.6 can be built with Microsoft Visual Studio 2008 (version
9.0), and this is the new default compiler. See the ‘PCbuild’
directory for the build files. (Implemented by Christian Heimes.)
* On Mac OS X, Python 2.6 can be compiled as a 4-way universal build.
The ‘configure’ script can take a
‘--with-universal-archs=[32-bit|64-bit|all]’ switch, controlling
whether the binaries are built for 32-bit architectures (x86,
PowerPC), 64-bit (x86-64 and PPC-64), or both. (Contributed by
Ronald Oussoren.)
* The BerkeleyDB module now has a C API object, available as
‘bsddb.db.api’. This object can be used by other C extensions that
wish to use the ‘bsddb’ module for their own purposes.
(Contributed by Duncan Grisby.)
* The new buffer interface, previously described in *note the PEP
3118 section: d24, adds *note PyObject_GetBuffer(): d25. and *note
PyBuffer_Release(): d54, as well as a few other functions.
* Python’s use of the C stdio library is now thread-safe, or at least
as thread-safe as the underlying library is. A long-standing
potential bug occurred if one thread closed a file object while
another thread was reading from or writing to the object. In 2.6
file objects have a reference count, manipulated by the
‘PyFile_IncUseCount()’ and ‘PyFile_DecUseCount()’ functions. File
objects can’t be closed unless the reference count is zero.
‘PyFile_IncUseCount()’ should be called while the GIL is still
held, before carrying out an I/O operation using the ‘FILE *’
pointer, and ‘PyFile_DecUseCount()’ should be called immediately
after the GIL is re-acquired. (Contributed by Antoine Pitrou and
Gregory P. Smith.)
* Importing modules simultaneously in two different threads no longer
deadlocks; it will now raise an *note ImportError: 334. A new API
function, *note PyImport_ImportModuleNoBlock(): 9c1, will look for
a module in ‘sys.modules’ first, then try to import it after
acquiring an import lock. If the import lock is held by another
thread, an *note ImportError: 334. is raised. (Contributed by
Christian Heimes.)
* Several functions return information about the platform’s
floating-point support. *note PyFloat_GetMax(): d55. returns the
maximum representable floating point value, and *note
PyFloat_GetMin(): d56. returns the minimum positive value. *note
PyFloat_GetInfo(): d57. returns an object containing more
information from the ‘float.h’ file, such as ‘"mant_dig"’ (number
of digits in the mantissa), ‘"epsilon"’ (smallest difference
between 1.0 and the next largest value representable), and several
others. (Contributed by Christian Heimes; bpo-1534(1).)
* C functions and methods that use *note PyComplex_AsCComplex(): d58.
will now accept arguments that have a *note __complex__(): 18d.
method. In particular, the functions in the *note cmath: 19.
module will now accept objects with this method. This is a
backport of a Python 3.0 change. (Contributed by Mark Dickinson;
bpo-1675423(2).)
* Python’s C API now includes two functions for case-insensitive
string comparisons, ‘PyOS_stricmp(char*, char*)’ and
‘PyOS_strnicmp(char*, char*, Py_ssize_t)’. (Contributed by
Christian Heimes; bpo-1635(3).)
* Many C extensions define their own little macro for adding integers
and strings to the module’s dictionary in the ‘init*’ function.
Python 2.6 finally defines standard macros for adding values to a
module, *note PyModule_AddStringMacro: d59. and
‘PyModule_AddIntMacro()’. (Contributed by Christian Heimes.)
* Some macros were renamed in both 3.0 and 2.6 to make it clearer
that they are macros, not functions. ‘Py_Size()’ became
‘Py_SIZE()’, ‘Py_Type()’ became ‘Py_TYPE()’, and ‘Py_Refcnt()’
became ‘Py_REFCNT()’. The mixed-case macros are still available in
Python 2.6 for backward compatibility. (bpo-1629(4))
* Distutils now places C extensions it builds in a different
directory when running on a debug version of Python. (Contributed
by Collin Winter; bpo-1530959(5).)
* Several basic data types, such as integers and strings, maintain
internal free lists of objects that can be re-used. The data
structures for these free lists now follow a naming convention: the
variable is always named ‘free_list’, the counter is always named
‘numfree’, and a macro ‘Py_MAXFREELIST’ is always
defined.
* A new Makefile target, “make patchcheck”, prepares the Python
source tree for making a patch: it fixes trailing whitespace in all
modified ‘.py’ files, checks whether the documentation has been
changed, and reports whether the ‘Misc/ACKS’ and ‘Misc/NEWS’ files
have been updated. (Contributed by Brett Cannon.)
Another new target, “make profile-opt”, compiles a Python binary
using GCC’s profile-guided optimization. It compiles Python with
profiling enabled, runs the test suite to obtain a set of profiling
results, and then compiles using these results for optimization.
(Contributed by Gregory P. Smith.)
* Menu:
* Port-Specific Changes; Windows: Port-Specific Changes Windows<2>.
* Port-Specific Changes; Mac OS X: Port-Specific Changes Mac OS X<2>.
* Port-Specific Changes; IRIX: Port-Specific Changes IRIX.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1534
(2) https://bugs.python.org/issue1675423
(3) https://bugs.python.org/issue1635
(4) https://bugs.python.org/issue1629
(5) https://bugs.python.org/issue1530959
File: python.info, Node: Port-Specific Changes Windows<2>, Next: Port-Specific Changes Mac OS X<2>, Up: Build and C API Changes<9>
1.11.20.1 Port-Specific Changes: Windows
........................................
* The support for Windows 95, 98, ME and NT4 has been dropped.
Python 2.6 requires at least Windows 2000 SP4.
* The new default compiler on Windows is Visual Studio 2008 (version
9.0). The build directories for Visual Studio 2003 (version 7.1)
and 2005 (version 8.0) were moved into the PC/ directory. The new
‘PCbuild’ directory supports cross compilation for X64, debug
builds and Profile Guided Optimization (PGO). PGO builds are
roughly 10% faster than normal builds. (Contributed by Christian
Heimes with help from Amaury Forgeot d’Arc and Martin von Löwis.)
* The *note msvcrt: b6. module now supports both the normal and wide
char variants of the console I/O API. The ‘getwch()’ function reads
a keypress and returns a Unicode value, as does the ‘getwche()’
function. The ‘putwch()’ function takes a Unicode character and
writes it to the console. (Contributed by Christian Heimes.)
* *note os.path.expandvars(): d44. will now expand environment
variables in the form “%var%”, and “~user” will be expanded into
the user’s home directory path. (Contributed by Josiah Carlson;
bpo-957650(1).)
* The *note socket: ef. module’s socket objects now have an ‘ioctl()’
method that provides a limited interface to the ‘WSAIoctl()’ system
interface.
* The ‘_winreg’ module now has a function,
‘ExpandEnvironmentStrings()’, that expands environment variable
references such as ‘%NAME%’ in an input string. The handle objects
provided by this module now support the context protocol, so they
can be used in *note with: 6e9. statements. (Contributed by
Christian Heimes.)
‘_winreg’ also has better support for x64 systems, exposing the
‘DisableReflectionKey()’, ‘EnableReflectionKey()’, and
‘QueryReflectionKey()’ functions, which enable and disable registry
reflection for 32-bit processes running on 64-bit systems.
(bpo-1753245(2))
* The *note msilib: b5. module’s ‘Record’ object gained
‘GetInteger()’ and ‘GetString()’ methods that return field values
as an integer or a string. (Contributed by Floris Bruynooghe;
bpo-2125(3).)
---------- Footnotes ----------
(1) https://bugs.python.org/issue957650
(2) https://bugs.python.org/issue1753245
(3) https://bugs.python.org/issue2125
File: python.info, Node: Port-Specific Changes Mac OS X<2>, Next: Port-Specific Changes IRIX, Prev: Port-Specific Changes Windows<2>, Up: Build and C API Changes<9>
1.11.20.2 Port-Specific Changes: Mac OS X
.........................................
* When compiling a framework build of Python, you can now specify the
framework name to be used by providing the ‘--with-framework-name=’
option to the ‘configure’ script.
* The ‘macfs’ module has been removed. This in turn required the
‘macostools.touched()’ function to be removed because it depended
on the ‘macfs’ module. (bpo-1490190(1))
* Many other Mac OS modules have been deprecated and will be removed
in Python 3.0: ‘_builtinSuites’, ‘aepack’, ‘aetools’, ‘aetypes’,
‘applesingle’, ‘appletrawmain’, ‘appletrunner’, ‘argvemulator’,
‘Audio_mac’, ‘autoGIL’, ‘Carbon’, ‘cfmfile’, ‘CodeWarrior’,
‘ColorPicker’, ‘EasyDialogs’, ‘Explorer’, ‘Finder’, ‘FrameWork’,
‘findertools’, ‘ic’, ‘icglue’, ‘icopen’, ‘macerrors’, ‘MacOS’,
‘macfs’, ‘macostools’, ‘macresource’, ‘MiniAEFrame’, ‘Nav’,
‘Netscape’, ‘OSATerminology’, ‘pimp’, ‘PixMapWrapper’, ‘StdSuites’,
‘SystemEvents’, ‘Terminal’, and ‘terminalcommand’.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1490190
File: python.info, Node: Port-Specific Changes IRIX, Prev: Port-Specific Changes Mac OS X<2>, Up: Build and C API Changes<9>
1.11.20.3 Port-Specific Changes: IRIX
.....................................
A number of old IRIX-specific modules were deprecated and will be
removed in Python 3.0: ‘al’ and ‘AL’, ‘cd’, ‘cddb’, ‘cdplayer’, ‘CL’ and
‘cl’, ‘DEVICE’, ‘ERRNO’, ‘FILE’, ‘FL’ and ‘fl’, ‘flp’, ‘fm’, ‘GET’,
‘GLWS’, ‘GL’ and ‘gl’, ‘IN’, ‘IOCTL’, ‘jpeg’, ‘panelparser’, ‘readcd’,
‘SV’ and ‘sv’, ‘torgb’, ‘videoreader’, and ‘WAIT’.
File: python.info, Node: Porting to Python 2 6, Next: Acknowledgements<2>, Prev: Build and C API Changes<9>, Up: What’s New in Python 2 6
1.11.21 Porting to Python 2.6
-----------------------------
This section lists previously described changes and other bugfixes that
may require changes to your code:
* Classes that aren’t supposed to be hashable should set ‘__hash__ =
None’ in their definitions to indicate the fact.
* String exceptions have been removed. Attempting to use them raises
a *note TypeError: 192.
* The *note __init__(): d5e. method of *note collections.deque: 531.
now clears any existing contents of the deque before adding
elements from the iterable. This change makes the behavior match
‘list.__init__()’.
* *note object.__init__(): d5e. previously accepted arbitrary
arguments and keyword arguments, ignoring them. In Python 2.6,
this is no longer allowed and will result in a *note TypeError:
192. This will affect *note __init__(): d5e. methods that end up
calling the corresponding method on *note object: 2b0. (perhaps
through using *note super(): 4e2.). See bpo-1683368(1) for
discussion.
* The ‘Decimal’ constructor now accepts leading and trailing
whitespace when passed a string. Previously it would raise an
‘InvalidOperation’ exception. On the other hand, the
‘create_decimal()’ method of ‘Context’ objects now explicitly
disallows extra whitespace, raising a ‘ConversionSyntax’ exception.
* Due to an implementation accident, if you passed a file path to the
built-in *note __import__(): 610. function, it would actually
import the specified file. This was never intended to work,
however, and the implementation now explicitly checks for this case
and raises an *note ImportError: 334.
* C API: the *note PyImport_Import(): d5f. and *note
PyImport_ImportModule(): c73. functions now default to absolute
imports, not relative imports. This will affect C extensions that
import other modules.
* C API: extension data types that shouldn’t be hashable should
define their ‘tp_hash’ slot to *note PyObject_HashNotImplemented():
d31.
* The *note socket: ef. module exception *note socket.error: 995. now
inherits from *note IOError: 992. Previously it wasn’t a subclass
of ‘StandardError’ but now it is, through *note IOError: 992.
(Implemented by Gregory P. Smith; bpo-1706815(2).)
* The ‘xmlrpclib’ module no longer automatically converts *note
datetime.date: 193. and *note datetime.time: 4f3. to the
‘xmlrpclib.DateTime’ type; the conversion semantics were not
necessarily correct for all applications. Code using ‘xmlrpclib’
should convert ‘date’ and *note time: 4f3. instances.
(bpo-1330538(3))
* (3.0-warning mode) The *note Exception: 1a9. class now warns when
accessed using slicing or index access; having *note Exception:
1a9. behave like a tuple is being phased out.
* (3.0-warning mode) inequality comparisons between two dictionaries
or two objects that don’t implement comparison methods are reported
as warnings. ‘dict1 == dict2’ still works, but ‘dict1 < dict2’ is
being phased out.
Comparisons between cells, which are an implementation detail of
Python’s scoping rules, also cause warnings because such
comparisons are forbidden entirely in 3.0.
---------- Footnotes ----------
(1) https://bugs.python.org/issue1683368
(2) https://bugs.python.org/issue1706815
(3) https://bugs.python.org/issue1330538
File: python.info, Node: Acknowledgements<2>, Prev: Porting to Python 2 6, Up: What’s New in Python 2 6
1.11.22 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy, Jim
Jewett, Kent Johnson, Chris Lambacher, Martin Michlmayr, Antoine Pitrou,
Brian Warner.
File: python.info, Node: What’s New in Python 2 5, Next: What’s New in Python 2 4, Prev: What’s New in Python 2 6, Up: What’s New in Python
1.12 What’s New in Python 2.5
=============================
Author: A.M. Kuchling
This article explains the new features in Python 2.5. The final release
of Python 2.5 is scheduled for August 2006; PEP 356(1) describes the
planned release schedule.
The changes in Python 2.5 are an interesting mix of language and library
improvements. The library enhancements will be more important to
Python’s user community, I think, because several widely-useful packages
were added. New modules include ElementTree for XML processing
(‘xml.etree’), the SQLite database module (‘sqlite’), and the *note
ctypes: 2b. module for calling C functions.
The language changes are of middling significance. Some pleasant new
features were added, but most of them aren’t features that you’ll use
every day. Conditional expressions were finally added to the language
using a novel syntax; see section *note PEP 308; Conditional
Expressions: d64. The new ‘*note with: 6e9.’ statement will make
writing cleanup code easier (section *note PEP 343; The ‘with’
statement: d65.). Values can now be passed into generators (section
*note PEP 342; New Generator Features: d66.). Imports are now visible
as either absolute or relative (section *note PEP 328; Absolute and
Relative Imports: d67.). Some corner cases of exception handling are
handled better (section *note PEP 341; Unified try/except/finally:
d68.). All these improvements are worthwhile, but they’re improvements
to one specific language feature or another; none of them are broad
modifications to Python’s semantics.
As well as the language and library additions, other improvements and
bugfixes were made throughout the source tree. A search through the SVN
change logs finds there were 353 patches applied and 458 bugs fixed
between Python 2.4 and 2.5. (Both figures are likely to be
underestimates.)
This article doesn’t try to be a complete specification of the new
features; instead changes are briefly introduced using helpful examples.
For full details, you should always refer to the documentation for
Python 2.5 at ‘https://docs.python.org’. If you want to understand the
complete implementation and design rationale, refer to the PEP for a
particular new feature.
Comments, suggestions, and error reports for this document are welcome;
please e-mail them to the author or open a bug in the Python bug
tracker.
* Menu:
* PEP 308; Conditional Expressions: PEP 308 Conditional Expressions.
* PEP 309; Partial Function Application: PEP 309 Partial Function Application.
* PEP 314; Metadata for Python Software Packages v1.1: PEP 314 Metadata for Python Software Packages v1 1.
* PEP 328; Absolute and Relative Imports: PEP 328 Absolute and Relative Imports.
* PEP 338; Executing Modules as Scripts: PEP 338 Executing Modules as Scripts.
* PEP 341; Unified try/except/finally: PEP 341 Unified try/except/finally.
* PEP 342; New Generator Features: PEP 342 New Generator Features.
* PEP 343; The ‘with’ statement: PEP 343 The ‘with’ statement<2>.
* PEP 352; Exceptions as New-Style Classes: PEP 352 Exceptions as New-Style Classes.
* PEP 353; Using ssize_t as the index type: PEP 353 Using ssize_t as the index type.
* PEP 357; The ‘__index__’ method: PEP 357 The ‘__index__’ method.
* Other Language Changes: Other Language Changes<11>.
* New, Improved, and Removed Modules: New Improved and Removed Modules.
* Build and C API Changes: Build and C API Changes<10>.
* Porting to Python 2.5: Porting to Python 2 5.
* Acknowledgements: Acknowledgements<3>.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0356
File: python.info, Node: PEP 308 Conditional Expressions, Next: PEP 309 Partial Function Application, Up: What’s New in Python 2 5
1.12.1 PEP 308: Conditional Expressions
---------------------------------------
For a long time, people have been requesting a way to write conditional
expressions, which are expressions that return value A or value B
depending on whether a Boolean value is true or false. A conditional
expression lets you write a single assignment statement that has the
same effect as the following:
if condition:
x = true_value
else:
x = false_value
There have been endless tedious discussions of syntax on both python-dev
and comp.lang.python. A vote was even held that found the majority of
voters wanted conditional expressions in some form, but there was no
syntax that was preferred by a clear majority. Candidates included C’s
‘cond ? true_v : false_v’, ‘if cond then true_v else false_v’, and 16
other variations.
Guido van Rossum eventually chose a surprising syntax:
x = true_value if condition else false_value
Evaluation is still lazy as in existing Boolean expressions, so the
order of evaluation jumps around a bit. The `condition' expression in
the middle is evaluated first, and the `true_value' expression is
evaluated only if the condition was true. Similarly, the `false_value'
expression is only evaluated when the condition is false.
This syntax may seem strange and backwards; why does the condition go in
the `middle' of the expression, and not in the front as in C’s ‘c ? x :
y’? The decision was checked by applying the new syntax to the modules
in the standard library and seeing how the resulting code read. In many
cases where a conditional expression is used, one value seems to be the
‘common case’ and one value is an ‘exceptional case’, used only on rarer
occasions when the condition isn’t met. The conditional syntax makes
this pattern a bit more obvious:
contents = ((doc + '\n') if doc else '')
I read the above statement as meaning “here `contents' is usually
assigned a value of ‘doc+'\n'’; sometimes `doc' is empty, in which
special case an empty string is returned.” I doubt I will use
conditional expressions very often where there isn’t a clear common and
uncommon case.
There was some discussion of whether the language should require
surrounding conditional expressions with parentheses. The decision was
made to `not' require parentheses in the Python language’s grammar, but
as a matter of style I think you should always use them. Consider these
two statements:
# First version -- no parens
level = 1 if logging else 0
# Second version -- with parens
level = (1 if logging else 0)
In the first version, I think a reader’s eye might group the statement
into ‘level = 1’, ‘if logging’, ‘else 0’, and think that the condition
decides whether the assignment to `level' is performed. The second
version reads better, in my opinion, because it makes it clear that the
assignment is always performed and the choice is being made between two
values.
Another reason for including the brackets: a few odd combinations of
list comprehensions and lambdas could look like incorrect conditional
expressions. See PEP 308(1) for some examples. If you put parentheses
around your conditional expressions, you won’t run into this case.
See also
........
PEP 308(2) - Conditional Expressions
PEP written by Guido van Rossum and Raymond D. Hettinger;
implemented by Thomas Wouters.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0308
(2) https://www.python.org/dev/peps/pep-0308
File: python.info, Node: PEP 309 Partial Function Application, Next: PEP 314 Metadata for Python Software Packages v1 1, Prev: PEP 308 Conditional Expressions, Up: What’s New in Python 2 5
1.12.2 PEP 309: Partial Function Application
--------------------------------------------
The *note functools: 85. module is intended to contain tools for
functional-style programming.
One useful tool in this module is the ‘partial()’ function. For
programs written in a functional style, you’ll sometimes want to
construct variants of existing functions that have some of the
parameters filled in. Consider a Python function ‘f(a, b, c)’; you
could create a new function ‘g(b, c)’ that was equivalent to ‘f(1, b,
c)’. This is called “partial function application”.
‘partial()’ takes the arguments ‘(function, arg1, arg2, ...
kwarg1=value1, kwarg2=value2)’. The resulting object is callable, so
you can just call it to invoke `function' with the filled-in arguments.
Here’s a small but realistic example:
import functools
def log (message, subsystem):
"Write the contents of 'message' to the specified subsystem."
print '%s: %s' % (subsystem, message)
...
server_log = functools.partial(log, subsystem='server')
server_log('Unable to open socket')
Here’s another example, from a program that uses PyGTK. Here a
context-sensitive pop-up menu is being constructed dynamically. The
callback provided for the menu option is a partially applied version of
the ‘open_item()’ method, where the first argument has been provided.
...
class Application:
def open_item(self, path):
...
def init (self):
open_func = functools.partial(self.open_item, item_path)
popup_menu.append( ("Open", open_func, 1) )
Another function in the *note functools: 85. module is the
‘update_wrapper(wrapper, wrapped)’ function that helps you write
well-behaved decorators. ‘update_wrapper()’ copies the name, module,
and docstring attribute to a wrapper function so that tracebacks inside
the wrapped function are easier to understand. For example, you might
write:
def my_decorator(f):
def wrapper(*args, **kwds):
print 'Calling decorated function'
return f(*args, **kwds)
functools.update_wrapper(wrapper, f)
return wrapper
‘wraps()’ is a decorator that can be used inside your own decorators to
copy the wrapped function’s information. An alternate version of the
previous example would be:
def my_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwds):
print 'Calling decorated function'
return f(*args, **kwds)
return wrapper
See also
........
PEP 309(1) - Partial Function Application
PEP proposed and written by Peter Harris; implemented by Hye-Shik
Chang and Nick Coghlan, with adaptations by Raymond Hettinger.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0309
File: python.info, Node: PEP 314 Metadata for Python Software Packages v1 1, Next: PEP 328 Absolute and Relative Imports, Prev: PEP 309 Partial Function Application, Up: What’s New in Python 2 5
1.12.3 PEP 314: Metadata for Python Software Packages v1.1
----------------------------------------------------------
Some simple dependency support was added to Distutils. The ‘setup()’
function now has ‘requires’, ‘provides’, and ‘obsoletes’ keyword
parameters. When you build a source distribution using the ‘sdist’
command, the dependency information will be recorded in the ‘PKG-INFO’
file.
Another new keyword parameter is ‘download_url’, which should be set to
a URL for the package’s source code. This means it’s now possible to
look up an entry in the package index, determine the dependencies for a
package, and download the required packages.
VERSION = '1.0'
setup(name='PyPackage',
version=VERSION,
requires=['numarray', 'zlib (>=1.1.4)'],
obsoletes=['OldPackage']
download_url=('http://www.example.com/pypackage/dist/pkg-%s.tar.gz'
% VERSION),
)
Another new enhancement to the Python package index at
‘https://pypi.org’ is storing source and binary archives for a package.
The new ‘upload’ Distutils command will upload a package to the
repository.
Before a package can be uploaded, you must be able to build a
distribution using the ‘sdist’ Distutils command. Once that works, you
can run ‘python setup.py upload’ to add your package to the PyPI
archive. Optionally you can GPG-sign the package by supplying the
‘--sign’ and ‘--identity’ options.
Package uploading was implemented by Martin von Löwis and Richard Jones.
See also
........
PEP 314(1) - Metadata for Python Software Packages v1.1
PEP proposed and written by A.M. Kuchling, Richard Jones, and Fred
Drake; implemented by Richard Jones and Fred Drake.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0314
File: python.info, Node: PEP 328 Absolute and Relative Imports, Next: PEP 338 Executing Modules as Scripts, Prev: PEP 314 Metadata for Python Software Packages v1 1, Up: What’s New in Python 2 5
1.12.4 PEP 328: Absolute and Relative Imports
---------------------------------------------
The simpler part of PEP 328(1) was implemented in Python 2.4:
parentheses could now be used to enclose the names imported from a
module using the ‘from ... import ...’ statement, making it easier to
import many different names.
The more complicated part has been implemented in Python 2.5: importing
a module can be specified to use absolute or package-relative imports.
The plan is to move toward making absolute imports the default in future
versions of Python.
Let’s say you have a package directory like this:
pkg/
pkg/__init__.py
pkg/main.py
pkg/string.py
This defines a package named ‘pkg’ containing the ‘pkg.main’ and
‘pkg.string’ submodules.
Consider the code in the ‘main.py’ module. What happens if it executes
the statement ‘import string’? In Python 2.4 and earlier, it will first
look in the package’s directory to perform a relative import, finds
‘pkg/string.py’, imports the contents of that file as the ‘pkg.string’
module, and that module is bound to the name ‘string’ in the ‘pkg.main’
module’s namespace.
That’s fine if ‘pkg.string’ was what you wanted. But what if you wanted
Python’s standard *note string: f6. module? There’s no clean way to
ignore ‘pkg.string’ and look for the standard module; generally you had
to look at the contents of ‘sys.modules’, which is slightly unclean.
Holger Krekel’s ‘py.std’ package provides a tidier way to perform
imports from the standard library, ‘import py; py.std.string.join()’,
but that package isn’t available on all Python installations.
Reading code which relies on relative imports is also less clear,
because a reader may be confused about which module, *note string: f6.
or ‘pkg.string’, is intended to be used. Python users soon learned not
to duplicate the names of standard library modules in the names of their
packages’ submodules, but you can’t protect against having your
submodule’s name being used for a new module added in a future version
of Python.
In Python 2.5, you can switch *note import: c1d.’s behaviour to absolute
imports using a ‘from __future__ import absolute_import’ directive.
This absolute-import behaviour will become the default in a future
version (probably Python 2.7). Once absolute imports are the default,
‘import string’ will always find the standard library’s version. It’s
suggested that users should begin using absolute imports as much as
possible, so it’s preferable to begin writing ‘from pkg import string’
in your code.
Relative imports are still possible by adding a leading period to the
module name when using the ‘from ... import’ form:
# Import names from pkg.string
from .string import name1, name2
# Import pkg.string
from . import string
This imports the *note string: f6. module relative to the current
package, so in ‘pkg.main’ this will import `name1' and `name2' from
‘pkg.string’. Additional leading periods perform the relative import
starting from the parent of the current package. For example, code in
the ‘A.B.C’ module can do:
from . import D # Imports A.B.D
from .. import E # Imports A.E
from ..F import G # Imports A.F.G
Leading periods cannot be used with the ‘import modname’ form of the
import statement, only the ‘from ... import’ form.
See also
........
PEP 328(2) - Imports: Multi-Line and Absolute/Relative
PEP written by Aahz; implemented by Thomas Wouters.
‘https://pylib.readthedocs.io/’
The py library by Holger Krekel, which contains the ‘py.std’
package.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0328
(2) https://www.python.org/dev/peps/pep-0328
File: python.info, Node: PEP 338 Executing Modules as Scripts, Next: PEP 341 Unified try/except/finally, Prev: PEP 328 Absolute and Relative Imports, Up: What’s New in Python 2 5
1.12.5 PEP 338: Executing Modules as Scripts
--------------------------------------------
The *note -m: 337. switch added in Python 2.4 to execute a module as a
script gained a few more abilities. Instead of being implemented in C
code inside the Python interpreter, the switch now uses an
implementation in a new module, *note runpy: e2.
The *note runpy: e2. module implements a more sophisticated import
mechanism so that it’s now possible to run modules in a package such as
‘pychecker.checker’. The module also supports alternative import
mechanisms such as the *note zipimport: 143. module. This means you can
add a .zip archive’s path to ‘sys.path’ and then use the *note -m: 337.
switch to execute code from the archive.
See also
........
PEP 338(1) - Executing modules as scripts
PEP written and implemented by Nick Coghlan.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0338
File: python.info, Node: PEP 341 Unified try/except/finally, Next: PEP 342 New Generator Features, Prev: PEP 338 Executing Modules as Scripts, Up: What’s New in Python 2 5
1.12.6 PEP 341: Unified try/except/finally
------------------------------------------
Until Python 2.5, the *note try: d72. statement came in two flavours.
You could use a *note finally: 182. block to ensure that code is always
executed, or one or more *note except: b3e. blocks to catch specific
exceptions. You couldn’t combine both ‘except’ blocks and a ‘finally’
block, because generating the right bytecode for the combined version
was complicated and it wasn’t clear what the semantics of the combined
statement should be.
Guido van Rossum spent some time working with Java, which does support
the equivalent of combining *note except: b3e. blocks and a *note
finally: 182. block, and this clarified what the statement should mean.
In Python 2.5, you can now write:
try:
block-1 ...
except Exception1:
handler-1 ...
except Exception2:
handler-2 ...
else:
else-block
finally:
final-block
The code in `block-1' is executed. If the code raises an exception, the
various *note except: b3e. blocks are tested: if the exception is of
class ‘Exception1’, `handler-1' is executed; otherwise if it’s of class
‘Exception2’, `handler-2' is executed, and so forth. If no exception is
raised, the `else-block' is executed.
No matter what happened previously, the `final-block' is executed once
the code block is complete and any raised exceptions handled. Even if
there’s an error in an exception handler or the `else-block' and a new
exception is raised, the code in the `final-block' is still run.
See also
........
PEP 341(1) - Unifying try-except and try-finally
PEP written by Georg Brandl; implementation by Thomas Lee.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0341
File: python.info, Node: PEP 342 New Generator Features, Next: PEP 343 The ‘with’ statement<2>, Prev: PEP 341 Unified try/except/finally, Up: What’s New in Python 2 5
1.12.7 PEP 342: New Generator Features
--------------------------------------
Python 2.5 adds a simple way to pass values `into' a generator. As
introduced in Python 2.3, generators only produce output; once a
generator’s code was invoked to create an iterator, there was no way to
pass any new information into the function when its execution is
resumed. Sometimes the ability to pass in some information would be
useful. Hackish solutions to this include making the generator’s code
look at a global variable and then changing the global variable’s value,
or passing in some mutable object that callers then modify.
To refresh your memory of basic generators, here’s a simple example:
def counter (maximum):
i = 0
while i < maximum:
yield i
i += 1
When you call ‘counter(10)’, the result is an iterator that returns the
values from 0 up to 9. On encountering the *note yield: 18f. statement,
the iterator returns the provided value and suspends the function’s
execution, preserving the local variables. Execution resumes on the
following call to the iterator’s *note next(): 682. method, picking up
after the ‘yield’ statement.
In Python 2.3, *note yield: 18f. was a statement; it didn’t return any
value. In 2.5, ‘yield’ is now an expression, returning a value that can
be assigned to a variable or otherwise operated on:
val = (yield i)
I recommend that you always put parentheses around a *note yield: 18f.
expression when you’re doing something with the returned value, as in
the above example. The parentheses aren’t always necessary, but it’s
easier to always add them instead of having to remember when they’re
needed.
( PEP 342(1) explains the exact rules, which are that a *note yield:
18f.-expression must always be parenthesized except when it occurs at
the top-level expression on the right-hand side of an assignment. This
means you can write ‘val = yield i’ but have to use parentheses when
there’s an operation, as in ‘val = (yield i) + 12’.)
Values are sent into a generator by calling its ‘send(value)’ method.
The generator’s code is then resumed and the *note yield: 18f.
expression returns the specified `value'. If the regular *note next():
682. method is called, the ‘yield’ returns *note None: 157.
Here’s the previous example, modified to allow changing the value of the
internal counter.
def counter (maximum):
i = 0
while i < maximum:
val = (yield i)
# If value provided, change counter
if val is not None:
i = val
else:
i += 1
And here’s an example of changing the counter:
>>> it = counter(10)
>>> print it.next()
0
>>> print it.next()
1
>>> print it.send(8)
8
>>> print it.next()
9
>>> print it.next()
Traceback (most recent call last):
File "t.py", line 15, in ?
print it.next()
StopIteration
*note yield: 18f. will usually return *note None: 157, so you should
always check for this case. Don’t just use its value in expressions
unless you’re sure that the ‘send()’ method will be the only method used
to resume your generator function.
In addition to ‘send()’, there are two other new methods on generators:
* ‘throw(type, value=None, traceback=None)’ is used to raise an
exception inside the generator; the exception is raised by the
*note yield: 18f. expression where the generator’s execution is
paused.
* ‘close()’ raises a new *note GeneratorExit: d32. exception inside
the generator to terminate the iteration. On receiving this
exception, the generator’s code must either raise *note
GeneratorExit: d32. or *note StopIteration: 486. Catching the
*note GeneratorExit: d32. exception and returning a value is
illegal and will trigger a *note RuntimeError: 2ba.; if the
function raises some other exception, that exception is propagated
to the caller. ‘close()’ will also be called by Python’s garbage
collector when the generator is garbage-collected.
If you need to run cleanup code when a *note GeneratorExit: d32.
occurs, I suggest using a ‘try: ... finally:’ suite instead of
catching *note GeneratorExit: d32.
The cumulative effect of these changes is to turn generators from
one-way producers of information into both producers and consumers.
Generators also become `coroutines', a more generalized form of
subroutines. Subroutines are entered at one point and exited at another
point (the top of the function, and a *note return: 190. statement), but
coroutines can be entered, exited, and resumed at many different points
(the *note yield: 18f. statements). We’ll have to figure out patterns
for using coroutines effectively in Python.
The addition of the ‘close()’ method has one side effect that isn’t
obvious. ‘close()’ is called when a generator is garbage-collected, so
this means the generator’s code gets one last chance to run before the
generator is destroyed. This last chance means that ‘try...finally’
statements in generators can now be guaranteed to work; the *note
finally: 182. clause will now always get a chance to run. The syntactic
restriction that you couldn’t mix *note yield: 18f. statements with a
‘try...finally’ suite has therefore been removed. This seems like a
minor bit of language trivia, but using generators and ‘try...finally’
is actually necessary in order to implement the *note with: 6e9.
statement described by PEP 343(2). I’ll look at this new statement in
the following section.
Another even more esoteric effect of this change: previously, the
‘gi_frame’ attribute of a generator was always a frame object. It’s now
possible for ‘gi_frame’ to be ‘None’ once the generator has been
exhausted.
See also
........
PEP 342(3) - Coroutines via Enhanced Generators
PEP written by Guido van Rossum and Phillip J. Eby; implemented by
Phillip J. Eby. Includes examples of some fancier uses of
generators as coroutines.
Earlier versions of these features were proposed in PEP 288(4) by
Raymond Hettinger and PEP 325(5) by Samuele Pedroni.
‘https://en.wikipedia.org/wiki/Coroutine’
The Wikipedia entry for coroutines.
‘http://www.sidhe.org/~dan/blog/archives/000178.html’
An explanation of coroutines from a Perl point of view, written by
Dan Sugalski.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0342
(2) https://www.python.org/dev/peps/pep-0343
(3) https://www.python.org/dev/peps/pep-0342
(4) https://www.python.org/dev/peps/pep-0288
(5) https://www.python.org/dev/peps/pep-0325
File: python.info, Node: PEP 343 The ‘with’ statement<2>, Next: PEP 352 Exceptions as New-Style Classes, Prev: PEP 342 New Generator Features, Up: What’s New in Python 2 5
1.12.8 PEP 343: The ‘with’ statement
------------------------------------
The ‘*note with: 6e9.’ statement clarifies code that previously would
use ‘try...finally’ blocks to ensure that clean-up code is executed. In
this section, I’ll discuss the statement as it will commonly be used.
In the next section, I’ll examine the implementation details and show
how to write objects for use with this statement.
The ‘*note with: 6e9.’ statement is a new control-flow structure whose
basic structure is:
with expression [as variable]:
with-block
The expression is evaluated, and it should result in an object that
supports the context management protocol (that is, has *note
__enter__(): c95. and *note __exit__(): c96. methods.
The object’s *note __enter__(): c95. is called before `with-block' is
executed and therefore can run set-up code. It also may return a value
that is bound to the name `variable', if given. (Note carefully that
`variable' is `not' assigned the result of `expression'.)
After execution of the `with-block' is finished, the object’s *note
__exit__(): c96. method is called, even if the block raised an
exception, and can therefore run clean-up code.
To enable the statement in Python 2.5, you need to add the following
directive to your module:
from __future__ import with_statement
The statement will always be enabled in Python 2.6.
Some standard Python objects now support the context management protocol
and can be used with the ‘*note with: 6e9.’ statement. File objects are
one example:
with open('/etc/passwd', 'r') as f:
for line in f:
print line
... more processing code ...
After this statement has executed, the file object in `f' will have been
automatically closed, even if the *note for: c30. loop raised an
exception part-way through the block.
Note: In this case, `f' is the same object created by *note open():
4f0, because ‘file.__enter__()’ returns `self'.
The *note threading: 109. module’s locks and condition variables also
support the ‘*note with: 6e9.’ statement:
lock = threading.Lock()
with lock:
# Critical section of code
...
The lock is acquired before the block is executed and always released
once the block is complete.
The new ‘localcontext()’ function in the *note decimal: 36. module makes
it easy to save and restore the current decimal context, which
encapsulates the desired precision and rounding characteristics for
computations:
from decimal import Decimal, Context, localcontext
# Displays with default precision of 28 digits
v = Decimal('578')
print v.sqrt()
with localcontext(Context(prec=16)):
# All code in this block uses a precision of 16 digits.
# The original context is restored on exiting the block.
print v.sqrt()
* Menu:
* Writing Context Managers: Writing Context Managers<2>.
* The contextlib module: The contextlib module<2>.
File: python.info, Node: Writing Context Managers<2>, Next: The contextlib module<2>, Up: PEP 343 The ‘with’ statement<2>
1.12.8.1 Writing Context Managers
.................................
Under the hood, the ‘*note with: 6e9.’ statement is fairly complicated.
Most people will only use ‘‘with’’ in company with existing objects and
don’t need to know these details, so you can skip the rest of this
section if you like. Authors of new objects will need to understand the
details of the underlying implementation and should keep reading.
A high-level explanation of the context management protocol is:
* The expression is evaluated and should result in an object called a
“context manager”. The context manager must have *note
__enter__(): c95. and *note __exit__(): c96. methods.
* The context manager’s *note __enter__(): c95. method is called.
The value returned is assigned to `VAR'. If no ‘'as VAR'’ clause is
present, the value is simply discarded.
* The code in `BLOCK' is executed.
* If `BLOCK' raises an exception, the ‘__exit__(type, value,
traceback)’ is called with the exception details, the same values
returned by *note sys.exc_info(): c5f. The method’s return value
controls whether the exception is re-raised: any false value
re-raises the exception, and ‘True’ will result in suppressing it.
You’ll only rarely want to suppress the exception, because if you
do the author of the code containing the ‘*note with: 6e9.’
statement will never realize anything went wrong.
* If `BLOCK' didn’t raise an exception, the *note __exit__(): c96.
method is still called, but `type', `value', and `traceback' are
all ‘None’.
Let’s think through an example. I won’t present detailed code but will
only sketch the methods necessary for a database that supports
transactions.
(For people unfamiliar with database terminology: a set of changes to
the database are grouped into a transaction. Transactions can be either
committed, meaning that all the changes are written into the database,
or rolled back, meaning that the changes are all discarded and the
database is unchanged. See any database textbook for more information.)
Let’s assume there’s an object representing a database connection. Our
goal will be to let the user write code like this:
db_connection = DatabaseConnection()
with db_connection as cursor:
cursor.execute('insert into ...')
cursor.execute('delete from ...')
# ... more operations ...
The transaction should be committed if the code in the block runs
flawlessly or rolled back if there’s an exception. Here’s the basic
interface for ‘DatabaseConnection’ that I’ll assume:
class DatabaseConnection:
# Database interface
def cursor (self):
"Returns a cursor object and starts a new transaction"
def commit (self):
"Commits current transaction"
def rollback (self):
"Rolls back current transaction"
The *note __enter__(): c95. method is pretty easy, having only to start
a new transaction. For this application the resulting cursor object
would be a useful result, so the method will return it. The user can
then add ‘as cursor’ to their ‘*note with: 6e9.’ statement to bind the
cursor to a variable name.
class DatabaseConnection:
...
def __enter__ (self):
# Code to start a new transaction
cursor = self.cursor()
return cursor
The *note __exit__(): c96. method is the most complicated because it’s
where most of the work has to be done. The method has to check if an
exception occurred. If there was no exception, the transaction is
committed. The transaction is rolled back if there was an exception.
In the code below, execution will just fall off the end of the function,
returning the default value of ‘None’. ‘None’ is false, so the
exception will be re-raised automatically. If you wished, you could be
more explicit and add a *note return: 190. statement at the marked
location.
class DatabaseConnection:
...
def __exit__ (self, type, value, tb):
if tb is None:
# No exception, so commit
self.commit()
else:
# Exception occurred, so rollback.
self.rollback()
# return False
File: python.info, Node: The contextlib module<2>, Prev: Writing Context Managers<2>, Up: PEP 343 The ‘with’ statement<2>
1.12.8.2 The contextlib module
..............................
The new *note contextlib: 24. module provides some functions and a
decorator that are useful for writing objects for use with the ‘*note
with: 6e9.’ statement.
The decorator is called ‘contextmanager()’, and lets you write a single
generator function instead of defining a new class. The generator
should yield exactly one value. The code up to the *note yield: 18f.
will be executed as the *note __enter__(): c95. method, and the value
yielded will be the method’s return value that will get bound to the
variable in the ‘*note with: 6e9.’ statement’s ‘as’ clause, if any. The
code after the *note yield: 18f. will be executed in the *note
__exit__(): c96. method. Any exception raised in the block will be
raised by the ‘yield’ statement.
Our database example from the previous section could be written using
this decorator as:
from contextlib import contextmanager
@contextmanager
def db_transaction (connection):
cursor = connection.cursor()
try:
yield cursor
except:
connection.rollback()
raise
else:
connection.commit()
db = DatabaseConnection()
with db_transaction(db) as cursor:
...
The *note contextlib: 24. module also has a ‘nested(mgr1, mgr2, ...)’
function that combines a number of context managers so you don’t need to
write nested ‘*note with: 6e9.’ statements. In this example, the single
‘‘with’’ statement both starts a database transaction and acquires a
thread lock:
lock = threading.Lock()
with nested (db_transaction(db), lock) as (cursor, locked):
...
Finally, the ‘closing(object)’ function returns `object' so that it can
be bound to a variable, and calls ‘object.close’ at the end of the
block.
import urllib, sys
from contextlib import closing
with closing(urllib.urlopen('http://www.yahoo.com')) as f:
for line in f:
sys.stdout.write(line)
See also
........
PEP 343(1) - The “with” statement
PEP written by Guido van Rossum and Nick Coghlan; implemented by
Mike Bland, Guido van Rossum, and Neal Norwitz. The PEP shows the
code generated for a ‘*note with: 6e9.’ statement, which can be
helpful in learning how the statement works.
The documentation for the *note contextlib: 24. module.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0343
File: python.info, Node: PEP 352 Exceptions as New-Style Classes, Next: PEP 353 Using ssize_t as the index type, Prev: PEP 343 The ‘with’ statement<2>, Up: What’s New in Python 2 5
1.12.9 PEP 352: Exceptions as New-Style Classes
-----------------------------------------------
Exception classes can now be new-style classes, not just classic
classes, and the built-in *note Exception: 1a9. class and all the
standard built-in exceptions (*note NameError: d7b, *note ValueError:
1fb, etc.) are now new-style classes.
The inheritance hierarchy for exceptions has been rearranged a bit. In
2.5, the inheritance relationships are:
BaseException # New in Python 2.5
|- KeyboardInterrupt
|- SystemExit
|- Exception
|- (all other current built-in exceptions)
This rearrangement was done because people often want to catch all
exceptions that indicate program errors. *note KeyboardInterrupt: 197.
and *note SystemExit: 5fc. aren’t errors, though, and usually represent
an explicit action such as the user hitting ‘Control-C’ or code calling
*note sys.exit(): ce2. A bare ‘except:’ will catch all exceptions, so
you commonly need to list *note KeyboardInterrupt: 197. and *note
SystemExit: 5fc. in order to re-raise them. The usual pattern is:
try:
...
except (KeyboardInterrupt, SystemExit):
raise
except:
# Log error...
# Continue running program...
In Python 2.5, you can now write ‘except Exception’ to achieve the same
result, catching all the exceptions that usually indicate errors but
leaving *note KeyboardInterrupt: 197. and *note SystemExit: 5fc. alone.
As in previous versions, a bare ‘except:’ still catches all exceptions.
The goal for Python 3.0 is to require any class raised as an exception
to derive from *note BaseException: 1a8. or some descendant of *note
BaseException: 1a8, and future releases in the Python 2.x series may
begin to enforce this constraint. Therefore, I suggest you begin making
all your exception classes derive from *note Exception: 1a9. now. It’s
been suggested that the bare ‘except:’ form should be removed in Python
3.0, but Guido van Rossum hasn’t decided whether to do this or not.
Raising of strings as exceptions, as in the statement ‘raise "Error
occurred"’, is deprecated in Python 2.5 and will trigger a warning. The
aim is to be able to remove the string-exception feature in a few
releases.
See also
........
PEP 352(1) - Required Superclass for Exceptions
PEP written by Brett Cannon and Guido van Rossum; implemented by
Brett Cannon.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0352
File: python.info, Node: PEP 353 Using ssize_t as the index type, Next: PEP 357 The ‘__index__’ method, Prev: PEP 352 Exceptions as New-Style Classes, Up: What’s New in Python 2 5
1.12.10 PEP 353: Using ssize_t as the index type
------------------------------------------------
A wide-ranging change to Python’s C API, using a new ‘Py_ssize_t’ type
definition instead of ‘int’, will permit the interpreter to handle more
data on 64-bit platforms. This change doesn’t affect Python’s capacity
on 32-bit platforms.
Various pieces of the Python interpreter used C’s ‘int’ type to store
sizes or counts; for example, the number of items in a list or tuple
were stored in an ‘int’. The C compilers for most 64-bit platforms
still define ‘int’ as a 32-bit type, so that meant that lists could only
hold up to ‘2**31 - 1’ = 2147483647 items. (There are actually a few
different programming models that 64-bit C compilers can use – see
‘http://www.unix.org/version2/whatsnew/lp64_wp.html’ for a discussion –
but the most commonly available model leaves ‘int’ as 32 bits.)
A limit of 2147483647 items doesn’t really matter on a 32-bit platform
because you’ll run out of memory before hitting the length limit. Each
list item requires space for a pointer, which is 4 bytes, plus space for
a *note PyObject: 4ba. representing the item. 2147483647*4 is already
more bytes than a 32-bit address space can contain.
It’s possible to address that much memory on a 64-bit platform, however.
The pointers for a list that size would only require 16 GiB of space, so
it’s not unreasonable that Python programmers might construct lists that
large. Therefore, the Python interpreter had to be changed to use some
type other than ‘int’, and this will be a 64-bit type on 64-bit
platforms. The change will cause incompatibilities on 64-bit machines,
so it was deemed worth making the transition now, while the number of
64-bit users is still relatively small. (In 5 or 10 years, we may `all'
be on 64-bit machines, and the transition would be more painful then.)
This change most strongly affects authors of C extension modules.
Python strings and container types such as lists and tuples now use
‘Py_ssize_t’ to store their size. Functions such as *note
PyList_Size(): d7e. now return ‘Py_ssize_t’. Code in extension modules
may therefore need to have some variables changed to ‘Py_ssize_t’.
The *note PyArg_ParseTuple(): 26a. and *note Py_BuildValue(): 2ce.
functions have a new conversion code, ‘n’, for ‘Py_ssize_t’. *note
PyArg_ParseTuple(): 26a.’s ‘s#’ and ‘t#’ still output ‘int’ by default,
but you can define the macro ‘PY_SSIZE_T_CLEAN’ before including
‘Python.h’ to make them return ‘Py_ssize_t’.
PEP 353(1) has a section on conversion guidelines that extension authors
should read to learn about supporting 64-bit platforms.
See also
........
PEP 353(2) - Using ssize_t as the index type
PEP written and implemented by Martin von Löwis.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0353
(2) https://www.python.org/dev/peps/pep-0353
File: python.info, Node: PEP 357 The ‘__index__’ method, Next: Other Language Changes<11>, Prev: PEP 353 Using ssize_t as the index type, Up: What’s New in Python 2 5
1.12.11 PEP 357: The ‘__index__’ method
---------------------------------------
The NumPy developers had a problem that could only be solved by adding a
new special method, *note __index__(): 18a. When using slice notation,
as in ‘[start:stop:step]’, the values of the `start', `stop', and `step'
indexes must all be either integers or long integers. NumPy defines a
variety of specialized integer types corresponding to unsigned and
signed integers of 8, 16, 32, and 64 bits, but there was no way to
signal that these types could be used as slice indexes.
Slicing can’t just use the existing *note __int__(): 18b. method because
that method is also used to implement coercion to integers. If slicing
used *note __int__(): 18b, floating-point numbers would also become
legal slice indexes and that’s clearly an undesirable behaviour.
Instead, a new special method called *note __index__(): 18a. was added.
It takes no arguments and returns an integer giving the slice index to
use. For example:
class C:
def __index__ (self):
return self.value
The return value must be either a Python integer or long integer. The
interpreter will check that the type returned is correct, and raises a
*note TypeError: 192. if this requirement isn’t met.
A corresponding ‘nb_index’ slot was added to the C-level *note
PyNumberMethods: d81. structure to let C extensions implement this
protocol. ‘PyNumber_Index(obj)’ can be used in extension code to call
the *note __index__(): 18a. function and retrieve its result.
See also
........
PEP 357(1) - Allowing Any Object to be Used for Slicing
PEP written and implemented by Travis Oliphant.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0357
File: python.info, Node: Other Language Changes<11>, Next: New Improved and Removed Modules, Prev: PEP 357 The ‘__index__’ method, Up: What’s New in Python 2 5
1.12.12 Other Language Changes
------------------------------
Here are all of the changes that Python 2.5 makes to the core Python
language.
* The *note dict: 1b8. type has a new hook for letting subclasses
provide a default value when a key isn’t contained in the
dictionary. When a key isn’t found, the dictionary’s
‘__missing__(key)’ method will be called. This hook is used to
implement the new ‘defaultdict’ class in the *note collections: 1e.
module. The following example defines a dictionary that returns
zero for any missing key:
class zerodict (dict):
def __missing__ (self, key):
return 0
d = zerodict({1:1, 2:2})
print d[1], d[2] # Prints 1, 2
print d[3], d[4] # Prints 0, 0
* Both 8-bit and Unicode strings have new ‘partition(sep)’ and
‘rpartition(sep)’ methods that simplify a common use case.
The ‘find(S)’ method is often used to get an index which is then
used to slice the string and obtain the pieces that are before and
after the separator. ‘partition(sep)’ condenses this pattern into
a single method call that returns a 3-tuple containing the
substring before the separator, the separator itself, and the
substring after the separator. If the separator isn’t found, the
first element of the tuple is the entire string and the other two
elements are empty. ‘rpartition(sep)’ also returns a 3-tuple but
starts searching from the end of the string; the ‘r’ stands for
‘reverse’.
Some examples:
>>> ('http://www.python.org').partition('://')
('http', '://', 'www.python.org')
>>> ('file:/usr/share/doc/index.html').partition('://')
('file:/usr/share/doc/index.html', '', '')
>>> (u'Subject: a quick question').partition(':')
(u'Subject', u':', u' a quick question')
>>> 'www.python.org'.rpartition('.')
('www.python', '.', 'org')
>>> 'www.python.org'.rpartition(':')
('', '', 'www.python.org')
(Implemented by Fredrik Lundh following a suggestion by Raymond
Hettinger.)
* The ‘startswith()’ and ‘endswith()’ methods of string types now
accept tuples of strings to check for.
def is_image_file (filename):
return filename.endswith(('.gif', '.jpg', '.tiff'))
(Implemented by Georg Brandl following a suggestion by Tom Lynn.)
* The *note min(): 809. and *note max(): 80a. built-in functions
gained a ‘key’ keyword parameter analogous to the ‘key’ argument
for ‘sort()’. This parameter supplies a function that takes a
single argument and is called for every value in the list; *note
min(): 809./*note max(): 80a. will return the element with the
smallest/largest return value from this function. For example, to
find the longest string in a list, you can do:
L = ['medium', 'longest', 'short']
# Prints 'longest'
print max(L, key=len)
# Prints 'short', because lexicographically 'short' has the largest value
print max(L)
(Contributed by Steven Bethard and Raymond Hettinger.)
* Two new built-in functions, *note any(): d84. and *note all(): d85,
evaluate whether an iterator contains any true or false values.
*note any(): d84. returns *note True: 499. if any value returned by
the iterator is true; otherwise it will return *note False: 388.
*note all(): d85. returns *note True: 499. only if all of the
values returned by the iterator evaluate as true. (Suggested by
Guido van Rossum, and implemented by Raymond Hettinger.)
* The result of a class’s *note __hash__(): 340. method can now be
either a long integer or a regular integer. If a long integer is
returned, the hash of that value is taken. In earlier versions the
hash value was required to be a regular integer, but in 2.5 the
*note id(): d86. built-in was changed to always return non-negative
numbers, and users often seem to use ‘id(self)’ in *note
__hash__(): 340. methods (though this is discouraged).
* ASCII is now the default encoding for modules. It’s now a syntax
error if a module contains string literals with 8-bit characters
but doesn’t have an encoding declaration. In Python 2.4 this
triggered a warning, not a syntax error. See PEP 263(1) for how to
declare a module’s encoding; for example, you might add a line like
this near the top of the source file:
# -*- coding: latin1 -*-
* A new warning, *note UnicodeWarning: d87, is triggered when you
attempt to compare a Unicode string and an 8-bit string that can’t
be converted to Unicode using the default ASCII encoding. The
result of the comparison is false:
>>> chr(128) == unichr(128) # Can't convert chr(128) to Unicode
__main__:1: UnicodeWarning: Unicode equal comparison failed
to convert both arguments to Unicode - interpreting them
as being unequal
False
>>> chr(127) == unichr(127) # chr(127) can be converted
True
Previously this would raise a *note UnicodeDecodeError: 1fd.
exception, but in 2.5 this could result in puzzling problems when
accessing a dictionary. If you looked up ‘unichr(128)’ and
‘chr(128)’ was being used as a key, you’d get a *note
UnicodeDecodeError: 1fd. exception. Other changes in 2.5 resulted
in this exception being raised instead of suppressed by the code in
‘dictobject.c’ that implements dictionaries.
Raising an exception for such a comparison is strictly correct, but
the change might have broken code, so instead *note UnicodeWarning:
d87. was introduced.
(Implemented by Marc-André Lemburg.)
* One error that Python programmers sometimes make is forgetting to
include an ‘__init__.py’ module in a package directory. Debugging
this mistake can be confusing, and usually requires running Python
with the *note -v: d88. switch to log all the paths searched. In
Python 2.5, a new *note ImportWarning: 5d8. warning is triggered
when an import would have picked up a directory as a package but no
‘__init__.py’ was found. This warning is silently ignored by
default; provide the *note -Wd: 417. option when running the Python
executable to display the warning message. (Implemented by Thomas
Wouters.)
* The list of base classes in a class definition can now be empty.
As an example, this is now legal:
class C():
pass
(Implemented by Brett Cannon.)
* Menu:
* Interactive Interpreter Changes::
* Optimizations: Optimizations<10>.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0263
File: python.info, Node: Interactive Interpreter Changes, Next: Optimizations<10>, Up: Other Language Changes<11>
1.12.12.1 Interactive Interpreter Changes
.........................................
In the interactive interpreter, ‘quit’ and ‘exit’ have long been strings
so that new users get a somewhat helpful message when they try to quit:
>>> quit
'Use Ctrl-D (i.e. EOF) to exit.'
In Python 2.5, ‘quit’ and ‘exit’ are now objects that still produce
string representations of themselves, but are also callable. Newbies
who try ‘quit()’ or ‘exit()’ will now exit the interpreter as they
expect. (Implemented by Georg Brandl.)
The Python executable now accepts the standard long options *note –help:
d8b. and *note –version: 5d1.; on Windows, it also accepts the *note /?:
d8c. option for displaying a help message. (Implemented by Georg
Brandl.)
File: python.info, Node: Optimizations<10>, Prev: Interactive Interpreter Changes, Up: Other Language Changes<11>
1.12.12.2 Optimizations
.......................
Several of the optimizations were developed at the NeedForSpeed sprint,
an event held in Reykjavik, Iceland, from May 21–28 2006. The sprint
focused on speed enhancements to the CPython implementation and was
funded by EWT LLC with local support from CCP Games. Those
optimizations added at this sprint are specially marked in the following
list.
* When they were introduced in Python 2.4, the built-in *note set:
b6f. and *note frozenset: bee. types were built on top of Python’s
dictionary type. In 2.5 the internal data structure has been
customized for implementing sets, and as a result sets will use a
third less memory and are somewhat faster. (Implemented by Raymond
Hettinger.)
* The speed of some Unicode operations, such as finding substrings,
string splitting, and character map encoding and decoding, has been
improved. (Substring search and splitting improvements were added
by Fredrik Lundh and Andrew Dalke at the NeedForSpeed sprint.
Character maps were improved by Walter Dörwald and Martin von
Löwis.)
* The ‘long(str, base)’ function is now faster on long digit strings
because fewer intermediate results are calculated. The peak is for
strings of around 800–1000 digits where the function is 6 times
faster. (Contributed by Alan McIntyre and committed at the
NeedForSpeed sprint.)
* It’s now illegal to mix iterating over a file with ‘for line in
file’ and calling the file object’s ‘read()’/*note readline():
de./‘readlines()’ methods. Iteration uses an internal buffer and
the ‘read*()’ methods don’t use that buffer. Instead they would
return the data following the buffer, causing the data to appear
out of order. Mixing iteration and these methods will now trigger
a *note ValueError: 1fb. from the ‘read*()’ method. (Implemented
by Thomas Wouters.)
* The *note struct: f8. module now compiles structure format strings
into an internal representation and caches this representation,
yielding a 20% speedup. (Contributed by Bob Ippolito at the
NeedForSpeed sprint.)
* The *note re: dd. module got a 1 or 2% speedup by switching to
Python’s allocator functions instead of the system’s ‘malloc()’ and
‘free()’. (Contributed by Jack Diederich at the NeedForSpeed
sprint.)
* The code generator’s peephole optimizer now performs simple
constant folding in expressions. If you write something like ‘a =
2+3’, the code generator will do the arithmetic and produce code
corresponding to ‘a = 5’. (Proposed and implemented by Raymond
Hettinger.)
* Function calls are now faster because code objects now keep the
most recently finished frame (a “zombie frame”) in an internal
field of the code object, reusing it the next time the code object
is invoked. (Original patch by Michael Hudson, modified by Armin
Rigo and Richard Jones; committed at the NeedForSpeed sprint.)
Frame objects are also slightly smaller, which may improve cache
locality and reduce memory usage a bit. (Contributed by Neal
Norwitz.)
* Python’s built-in exceptions are now new-style classes, a change
that speeds up instantiation considerably. Exception handling in
Python 2.5 is therefore about 30% faster than in 2.4. (Contributed
by Richard Jones, Georg Brandl and Sean Reifschneider at the
NeedForSpeed sprint.)
* Importing now caches the paths tried, recording whether they exist
or not so that the interpreter makes fewer ‘open()’ and ‘stat()’
calls on startup. (Contributed by Martin von Löwis and Georg
Brandl.)
File: python.info, Node: New Improved and Removed Modules, Next: Build and C API Changes<10>, Prev: Other Language Changes<11>, Up: What’s New in Python 2 5
1.12.13 New, Improved, and Removed Modules
------------------------------------------
The standard library received many enhancements and bug fixes in Python
2.5. Here’s a partial list of the most notable changes, sorted
alphabetically by module name. Consult the ‘Misc/NEWS’ file in the
source tree for a more complete list of changes, or look through the SVN
logs for all the details.
* The *note audioop: d. module now supports the a-LAW encoding, and
the code for u-LAW encoding has been improved. (Contributed by
Lars Immisch.)
* The *note codecs: 1c. module gained support for incremental codecs.
The ‘codec.lookup()’ function now returns a ‘CodecInfo’ instance
instead of a tuple. ‘CodecInfo’ instances behave like a 4-tuple to
preserve backward compatibility but also have the attributes
‘encode’, ‘decode’, ‘incrementalencoder’, ‘incrementaldecoder’,
‘streamwriter’, and ‘streamreader’. Incremental codecs can receive
input and produce output in multiple chunks; the output is the same
as if the entire input was fed to the non-incremental codec. See
the *note codecs: 1c. module documentation for details. (Designed
and implemented by Walter Dörwald.)
* The *note collections: 1e. module gained a new type, ‘defaultdict’,
that subclasses the standard *note dict: 1b8. type. The new type
mostly behaves like a dictionary but constructs a default value
when a key isn’t present, automatically adding it to the dictionary
for the requested key value.
The first argument to ‘defaultdict’’s constructor is a factory
function that gets called whenever a key is requested but not
found. This factory function receives no arguments, so you can use
built-in type constructors such as *note list(): 262. or *note
int(): 184. For example, you can make an index of words based on
their initial letter like this:
words = """Nel mezzo del cammin di nostra vita
mi ritrovai per una selva oscura
che la diritta via era smarrita""".lower().split()
index = defaultdict(list)
for w in words:
init_letter = w[0]
index[init_letter].append(w)
Printing ‘index’ results in the following output:
defaultdict(, {'c': ['cammin', 'che'], 'e': ['era'],
'd': ['del', 'di', 'diritta'], 'm': ['mezzo', 'mi'],
'l': ['la'], 'o': ['oscura'], 'n': ['nel', 'nostra'],
'p': ['per'], 's': ['selva', 'smarrita'],
'r': ['ritrovai'], 'u': ['una'], 'v': ['vita', 'via']}
(Contributed by Guido van Rossum.)
* The ‘deque’ double-ended queue type supplied by the *note
collections: 1e. module now has a ‘remove(value)’ method that
removes the first occurrence of `value' in the queue, raising *note
ValueError: 1fb. if the value isn’t found. (Contributed by Raymond
Hettinger.)
* New module: The *note contextlib: 24. module contains helper
functions for use with the new ‘*note with: 6e9.’ statement. See
section *note The contextlib module: d77. for more about this
module.
* New module: The *note cProfile: 28. module is a C implementation of
the existing *note profile: d3. module that has much lower
overhead. The module’s interface is the same as *note profile:
d3.: you run ‘cProfile.run('main()')’ to profile a function, can
save profile data to a file, etc. It’s not yet known if the
Hotshot profiler, which is also written in C but doesn’t match the
*note profile: d3. module’s interface, will continue to be
maintained in future versions of Python. (Contributed by Armin
Rigo.)
Also, the *note pstats: d4. module for analyzing the data measured
by the profiler now supports directing the output to any file
object by supplying a `stream' argument to the ‘Stats’ constructor.
(Contributed by Skip Montanaro.)
* The *note csv: 2a. module, which parses files in comma-separated
value format, received several enhancements and a number of
bugfixes. You can now set the maximum size in bytes of a field by
calling the ‘csv.field_size_limit(new_limit)’ function; omitting
the `new_limit' argument will return the currently-set limit. The
‘reader’ class now has a ‘line_num’ attribute that counts the
number of physical lines read from the source; records can span
multiple physical lines, so ‘line_num’ is not the same as the
number of records read.
The CSV parser is now stricter about multi-line quoted fields.
Previously, if a line ended within a quoted field without a
terminating newline character, a newline would be inserted into the
returned field. This behavior caused problems when reading files
that contained carriage return characters within fields, so the
code was changed to return the field without inserting newlines.
As a consequence, if newlines embedded within fields are important,
the input should be split into lines in a manner that preserves the
newline characters.
(Contributed by Skip Montanaro and Andrew McNamara.)
* The *note datetime: 194. class in the *note datetime: 31. module
now has a ‘strptime(string, format)’ method for parsing date
strings, contributed by Josh Spoerri. It uses the same format
characters as *note time.strptime(): d91. and *note
time.strftime(): b65.:
from datetime import datetime
ts = datetime.strptime('10:13:15 2006-03-07',
'%H:%M:%S %Y-%m-%d')
* The ‘SequenceMatcher.get_matching_blocks()’ method in the *note
difflib: 37. module now guarantees to return a minimal list of
blocks describing matching subsequences. Previously, the algorithm
would occasionally break a block of matching elements into two list
entries. (Enhancement by Tim Peters.)
* The *note doctest: 67. module gained a ‘SKIP’ option that keeps an
example from being executed at all. This is intended for code
snippets that are usage examples intended for the reader and aren’t
actually test cases.
An `encoding' parameter was added to the ‘testfile()’ function and
the ‘DocFileSuite’ class to specify the file’s encoding. This
makes it easier to use non-ASCII characters in tests contained
within a docstring. (Contributed by Bjorn Tillenius.)
* The *note email: 69. package has been updated to version 4.0.
(Contributed by Barry Warsaw.)
* The *note fileinput: 80. module was made more flexible. Unicode
filenames are now supported, and a `mode' parameter that defaults
to ‘"r"’ was added to the *note input(): c6b. function to allow
opening files in binary or *note universal newlines: 5f4. mode.
Another new parameter, `openhook', lets you use a function other
than *note open(): 4f0. to open the input files. Once you’re
iterating over the set of files, the ‘FileInput’ object’s new
‘fileno()’ returns the file descriptor for the currently opened
file. (Contributed by Georg Brandl.)
* In the *note gc: 86. module, the new ‘get_count()’ function returns
a 3-tuple containing the current collection counts for the three GC
generations. This is accounting information for the garbage
collector; when these counts reach a specified threshold, a garbage
collection sweep will be made. The existing *note gc.collect():
22e. function now takes an optional `generation' argument of 0, 1,
or 2 to specify which generation to collect. (Contributed by Barry
Warsaw.)
* The ‘nsmallest()’ and ‘nlargest()’ functions in the *note heapq:
8e. module now support a ‘key’ keyword parameter similar to the one
provided by the *note min(): 809./*note max(): 80a. functions and
the ‘sort()’ methods. For example:
>>> import heapq
>>> L = ["short", 'medium', 'longest', 'longer still']
>>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically
['longer still', 'longest']
>>> heapq.nsmallest(2, L, key=len) # Return two shortest elements
['short', 'medium']
(Contributed by Raymond Hettinger.)
* The *note itertools.islice(): 3a2. function now accepts ‘None’ for
the start and step arguments. This makes it more compatible with
the attributes of slice objects, so that you can now write the
following:
s = slice(5) # Create slice object
itertools.islice(iterable, s.start, s.stop, s.step)
(Contributed by Raymond Hettinger.)
* The *note format(): 4db. function in the *note locale: a9. module
has been modified and two new functions were added,
‘format_string()’ and ‘currency()’.
The *note format(): 4db. function’s `val' parameter could
previously be a string as long as no more than one %char specifier
appeared; now the parameter must be exactly one %char specifier
with no surrounding text. An optional `monetary' parameter was
also added which, if ‘True’, will use the locale’s rules for
formatting currency in placing a separator between groups of three
digits.
To format strings with multiple %char specifiers, use the new
‘format_string()’ function that works like *note format(): 4db. but
also supports mixing %char specifiers with arbitrary text.
A new ‘currency()’ function was also added that formats a number
according to the current locale’s settings.
(Contributed by Georg Brandl.)
* The *note mailbox: ae. module underwent a massive rewrite to add
the capability to modify mailboxes in addition to reading them. A
new set of classes that include ‘mbox’, ‘MH’, and ‘Maildir’ are
used to read mailboxes, and have an ‘add(message)’ method to add
messages, ‘remove(key)’ to remove messages, and ‘lock()’/‘unlock()’
to lock/unlock the mailbox. The following example converts a
maildir-format mailbox into an mbox-format one:
import mailbox
# 'factory=None' uses email.Message.Message as the class representing
# individual messages.
src = mailbox.Maildir('maildir', factory=None)
dest = mailbox.mbox('/tmp/mbox')
for msg in src:
dest.add(msg)
(Contributed by Gregory K. Johnson. Funding was provided by
Google’s 2005 Summer of Code.)
* New module: the *note msilib: b5. module allows creating Microsoft
Installer ‘.msi’ files and CAB files. Some support for reading the
‘.msi’ database is also included. (Contributed by Martin von
Löwis.)
* The *note nis: bf. module now supports accessing domains other than
the system default domain by supplying a `domain' argument to the
*note nis.match(): d92. and *note nis.maps(): d93. functions.
(Contributed by Ben Bell.)
* The *note operator: c2. module’s ‘itemgetter()’ and ‘attrgetter()’
functions now support multiple fields. A call such as
‘operator.attrgetter('a', 'b')’ will return a function that
retrieves the ‘a’ and ‘b’ attributes. Combining this new feature
with the ‘sort()’ method’s ‘key’ parameter lets you easily sort
lists using multiple fields. (Contributed by Raymond Hettinger.)
* The *note optparse: c3. module was updated to version 1.5.1 of the
Optik library. The ‘OptionParser’ class gained an ‘epilog’
attribute, a string that will be printed after the help message,
and a ‘destroy()’ method to break reference cycles created by the
object. (Contributed by Greg Ward.)
* The *note os: c4. module underwent several changes. The
‘stat_float_times’ variable now defaults to true, meaning that
*note os.stat(): 1f1. will now return time values as floats. (This
doesn’t necessarily mean that *note os.stat(): 1f1. will return
times that are precise to fractions of a second; not all systems
support such precision.)
Constants named *note os.SEEK_SET: d94, *note os.SEEK_CUR: d95, and
*note os.SEEK_END: d96. have been added; these are the parameters
to the *note os.lseek(): a5e. function. Two new constants for
locking are *note os.O_SHLOCK: d97. and *note os.O_EXLOCK: d98.
Two new functions, ‘wait3()’ and ‘wait4()’, were added. They’re
similar the ‘waitpid()’ function which waits for a child process to
exit and returns a tuple of the process ID and its exit status, but
‘wait3()’ and ‘wait4()’ return additional information. ‘wait3()’
doesn’t take a process ID as input, so it waits for any child
process to exit and returns a 3-tuple of `process-id',
`exit-status', `resource-usage' as returned from the *note
resource.getrusage(): d99. function. ‘wait4(pid)’ does take a
process ID. (Contributed by Chad J. Schroeder.)
On FreeBSD, the *note os.stat(): 1f1. function now returns times
with nanosecond resolution, and the returned object now has
‘st_gen’ and ‘st_birthtime’. The ‘st_flags’ attribute is also
available, if the platform supports it. (Contributed by Antti
Louko and Diego Pettenò.)
* The Python debugger provided by the *note pdb: c9. module can now
store lists of commands to execute when a breakpoint is reached and
execution stops. Once breakpoint #1 has been created, enter
‘commands 1’ and enter a series of commands to be executed,
finishing the list with ‘end’. The command list can include
commands that resume execution, such as ‘continue’ or ‘next’.
(Contributed by Grégoire Dooms.)
* The *note pickle: ca. and ‘cPickle’ modules no longer accept a
return value of ‘None’ from the *note __reduce__(): 19b. method;
the method must return a tuple of arguments instead. The ability
to return ‘None’ was deprecated in Python 2.4, so this completes
the removal of the feature.
* The *note pkgutil: cd. module, containing various utility functions
for finding packages, was enhanced to support PEP 302(1)’s import
hooks and now also works for packages stored in ZIP-format
archives. (Contributed by Phillip J. Eby.)
* The pybench benchmark suite by Marc-André Lemburg is now included
in the ‘Tools/pybench’ directory. The pybench suite is an
improvement on the commonly used ‘pystone.py’ program because
pybench provides a more detailed measurement of the interpreter’s
speed. It times particular operations such as function calls,
tuple slicing, method lookups, and numeric operations, instead of
performing many different operations and reducing the result to a
single number as ‘pystone.py’ does.
* The ‘pyexpat’ module now uses version 2.0 of the Expat parser.
(Contributed by Trent Mick.)
* The *note Queue: d18. class provided by the ‘Queue’ module gained
two new methods. ‘join()’ blocks until all items in the queue have
been retrieved and all processing work on the items have been
completed. Worker threads call the other new method,
‘task_done()’, to signal that processing for an item has been
completed. (Contributed by Raymond Hettinger.)
* The old ‘regex’ and ‘regsub’ modules, which have been deprecated
ever since Python 2.0, have finally been deleted. Other deleted
modules: ‘statcache’, ‘tzparse’, ‘whrandom’.
* Also deleted: the ‘lib-old’ directory, which includes ancient
modules such as ‘dircmp’ and ‘ni’, was removed. ‘lib-old’ wasn’t
on the default ‘sys.path’, so unless your programs explicitly added
the directory to ‘sys.path’, this removal shouldn’t affect your
code.
* The *note rlcompleter: e1. module is no longer dependent on
importing the *note readline: de. module and therefore now works on
non-Unix platforms. (Patch from Robert Kiendl.)
* The ‘SimpleXMLRPCServer’ and ‘DocXMLRPCServer’ classes now have a
‘rpc_paths’ attribute that constrains XML-RPC operations to a
limited set of URL paths; the default is to allow only ‘'/'’ and
‘'/RPC2'’. Setting ‘rpc_paths’ to ‘None’ or an empty tuple
disables this path checking.
* The *note socket: ef. module now supports ‘AF_NETLINK’ sockets on
Linux, thanks to a patch from Philippe Biondi. Netlink sockets are
a Linux-specific mechanism for communications between a user-space
process and kernel code; an introductory article about them is at
‘https://www.linuxjournal.com/article/7356’. In Python code,
netlink addresses are represented as a tuple of 2 integers, ‘(pid,
group_mask)’.
Two new methods on socket objects, ‘recv_into(buffer)’ and
‘recvfrom_into(buffer)’, store the received data in an object that
supports the buffer protocol instead of returning the data as a
string. This means you can put the data directly into an array or
a memory-mapped file.
Socket objects also gained ‘getfamily()’, ‘gettype()’, and
‘getproto()’ accessor methods to retrieve the family, type, and
protocol values for the socket.
* New module: the *note spwd: f1. module provides functions for
accessing the shadow password database on systems that support
shadow passwords.
* The *note struct: f8. is now faster because it compiles format
strings into ‘Struct’ objects with ‘pack()’ and ‘unpack()’ methods.
This is similar to how the *note re: dd. module lets you create
compiled regular expression objects. You can still use the
module-level ‘pack()’ and ‘unpack()’ functions; they’ll create
‘Struct’ objects and cache them. Or you can use ‘Struct’ instances
directly:
s = struct.Struct('ih3s')
data = s.pack(1972, 187, 'abc')
year, number, name = s.unpack(data)
You can also pack and unpack data to and from buffer objects
directly using the ‘pack_into(buffer, offset, v1, v2, ...)’ and
‘unpack_from(buffer, offset)’ methods. This lets you store data
directly into an array or a memory-mapped file.
(‘Struct’ objects were implemented by Bob Ippolito at the
NeedForSpeed sprint. Support for buffer objects was added by
Martin Blais, also at the NeedForSpeed sprint.)
* The Python developers switched from CVS to Subversion during the
2.5 development process. Information about the exact build version
is available as the ‘sys.subversion’ variable, a 3-tuple of
‘(interpreter-name, branch-name, revision-range)’. For example, at
the time of writing my copy of 2.5 was reporting ‘('CPython',
'trunk', '45313:45315')’.
This information is also available to C extensions via the *note
Py_GetBuildInfo(): d9a. function that returns a string of build
information like this: ‘"trunk:45355:45356M, Apr 13 2006,
07:42:19"’. (Contributed by Barry Warsaw.)
* Another new function, *note sys._current_frames(): d9b, returns the
current stack frames for all running threads as a dictionary
mapping thread identifiers to the topmost stack frame currently
active in that thread at the time the function is called.
(Contributed by Tim Peters.)
* The ‘TarFile’ class in the *note tarfile: 101. module now has an
‘extractall()’ method that extracts all members from the archive
into the current working directory. It’s also possible to set a
different directory as the extraction target, and to unpack only a
subset of the archive’s members.
The compression used for a tarfile opened in stream mode can now be
autodetected using the mode ‘'r|*'’. (Contributed by Lars
Gustäbel.)
* The *note threading: 109. module now lets you set the stack size
used when new threads are created. The ‘stack_size([*size*])’
function returns the currently configured stack size, and supplying
the optional `size' parameter sets a new value. Not all platforms
support changing the stack size, but Windows, POSIX threading, and
OS/2 all do. (Contributed by Andrew MacIntyre.)
* The *note unicodedata: 11a. module has been updated to use version
4.1.0 of the Unicode character database. Version 3.2.0 is required
by some specifications, so it’s still available as *note
unicodedata.ucd_3_2_0: d9c.
* New module: the *note uuid: 124. module generates universally
unique identifiers (UUIDs) according to RFC 4122(2). The RFC
defines several different UUID versions that are generated from a
starting string, from system properties, or purely randomly. This
module contains a ‘UUID’ class and functions named ‘uuid1()’,
‘uuid3()’, ‘uuid4()’, and ‘uuid5()’ to generate different versions
of UUID. (Version 2 UUIDs are not specified in RFC 4122(3) and are
not supported by this module.)
>>> import uuid
>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
(Contributed by Ka-Ping Yee.)
* The *note weakref: 128. module’s ‘WeakKeyDictionary’ and
‘WeakValueDictionary’ types gained new methods for iterating over
the weak references contained in the dictionary. ‘iterkeyrefs()’
and ‘keyrefs()’ methods were added to ‘WeakKeyDictionary’, and
‘itervaluerefs()’ and ‘valuerefs()’ were added to
‘WeakValueDictionary’. (Contributed by Fred L. Drake, Jr.)
* The *note webbrowser: 129. module received a number of
enhancements. It’s now usable as a script with ‘python -m
webbrowser’, taking a URL as the argument; there are a number of
switches to control the behaviour (‘-n’ for a new browser window,
‘-t’ for a new tab). New module-level functions, ‘open_new()’ and
‘open_new_tab()’, were added to support this. The module’s *note
open(): 4f0. function supports an additional feature, an
`autoraise' parameter that signals whether to raise the open window
when possible. A number of additional browsers were added to the
supported list such as Firefox, Opera, Konqueror, and elinks.
(Contributed by Oleg Broytmann and Georg Brandl.)
* The ‘xmlrpclib’ module now supports returning *note datetime: 194.
objects for the XML-RPC date type. Supply ‘use_datetime=True’ to
the ‘loads()’ function or the ‘Unmarshaller’ class to enable this
feature. (Contributed by Skip Montanaro.)
* The *note zipfile: 142. module now supports the ZIP64 version of
the format, meaning that a .zip archive can now be larger than 4
GiB and can contain individual files larger than 4 GiB.
(Contributed by Ronald Oussoren.)
* The *note zlib: 144. module’s ‘Compress’ and ‘Decompress’ objects
now support a *note copy(): 26. method that makes a copy of the
object’s internal state and returns a new ‘Compress’ or
‘Decompress’ object. (Contributed by Chris AtLee.)
* Menu:
* The ctypes package::
* The ElementTree package::
* The hashlib package::
* The sqlite3 package::
* The wsgiref package::
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0302
(2) https://tools.ietf.org/html/rfc4122.html
(3) https://tools.ietf.org/html/rfc4122.html
File: python.info, Node: The ctypes package, Next: The ElementTree package, Up: New Improved and Removed Modules
1.12.13.1 The ctypes package
............................
The *note ctypes: 2b. package, written by Thomas Heller, has been added
to the standard library. *note ctypes: 2b. lets you call arbitrary
functions in shared libraries or DLLs. Long-time users may remember the
‘dl’ module, which provides functions for loading shared libraries and
calling functions in them. The *note ctypes: 2b. package is much
fancier.
To load a shared library or DLL, you must create an instance of the
‘CDLL’ class and provide the name or path of the shared library or DLL.
Once that’s done, you can call arbitrary functions by accessing them as
attributes of the ‘CDLL’ object.
import ctypes
libc = ctypes.CDLL('libc.so.6')
result = libc.printf("Line of output\n")
Type constructors for the various C types are provided: ‘c_int()’,
‘c_float()’, ‘c_double()’, ‘c_char_p()’ (equivalent to ‘char *’), and so
forth. Unlike Python’s types, the C versions are all mutable; you can
assign to their ‘value’ attribute to change the wrapped value. Python
integers and strings will be automatically converted to the
corresponding C types, but for other types you must call the correct
type constructor. (And I mean `must'; getting it wrong will often
result in the interpreter crashing with a segmentation fault.)
You shouldn’t use ‘c_char_p()’ with a Python string when the C function
will be modifying the memory area, because Python strings are supposed
to be immutable; breaking this rule will cause puzzling bugs. When you
need a modifiable memory area, use ‘create_string_buffer()’:
s = "this is a string"
buf = ctypes.create_string_buffer(s)
libc.strfry(buf)
C functions are assumed to return integers, but you can set the
‘restype’ attribute of the function object to change this:
>>> libc.atof('2.71828')
-1783957616
>>> libc.atof.restype = ctypes.c_double
>>> libc.atof('2.71828')
2.71828
*note ctypes: 2b. also provides a wrapper for Python’s C API as the
‘ctypes.pythonapi’ object. This object does `not' release the global
interpreter lock before calling a function, because the lock must be
held when calling into the interpreter’s code. There’s a ‘py_object()’
type constructor that will create a *note PyObject *: 4ba. pointer. A
simple usage:
import ctypes
d = {}
ctypes.pythonapi.PyObject_SetItem(ctypes.py_object(d),
ctypes.py_object("abc"), ctypes.py_object(1))
# d is now {'abc', 1}.
Don’t forget to use ‘py_object()’; if it’s omitted you end up with a
segmentation fault.
*note ctypes: 2b. has been around for a while, but people still write
and distribution hand-coded extension modules because you can’t rely on
*note ctypes: 2b. being present. Perhaps developers will begin to write
Python wrappers atop a library accessed through *note ctypes: 2b.
instead of extension modules, now that *note ctypes: 2b. is included
with core Python.
See also
........
‘http://starship.python.net/crew/theller/ctypes/’
The ctypes web page, with a tutorial, reference, and FAQ.
The documentation for the *note ctypes: 2b. module.
File: python.info, Node: The ElementTree package, Next: The hashlib package, Prev: The ctypes package, Up: New Improved and Removed Modules
1.12.13.2 The ElementTree package
.................................
A subset of Fredrik Lundh’s ElementTree library for processing XML has
been added to the standard library as ‘xml.etree’. The available
modules are ‘ElementTree’, ‘ElementPath’, and ‘ElementInclude’ from
ElementTree 1.2.6. The ‘cElementTree’ accelerator module is also
included.
The rest of this section will provide a brief overview of using
ElementTree. Full documentation for ElementTree is available at
‘http://effbot.org/zone/element-index.htm’.
ElementTree represents an XML document as a tree of element nodes. The
text content of the document is stored as the ‘text’ and ‘tail’
attributes of (This is one of the major differences between ElementTree
and the Document Object Model; in the DOM there are many different types
of node, including ‘TextNode’.)
The most commonly used parsing function is ‘parse()’, that takes either
a string (assumed to contain a filename) or a file-like object and
returns an ‘ElementTree’ instance:
from xml.etree import ElementTree as ET
tree = ET.parse('ex-1.xml')
feed = urllib.urlopen(
'http://planet.python.org/rss10.xml')
tree = ET.parse(feed)
Once you have an ‘ElementTree’ instance, you can call its ‘getroot()’
method to get the root ‘Element’ node.
There’s also an ‘XML()’ function that takes a string literal and returns
an ‘Element’ node (not an ‘ElementTree’). This function provides a tidy
way to incorporate XML fragments, approaching the convenience of an XML
literal:
svg = ET.XML("""
""")
svg.set('height', '320px')
svg.append(elem1)
Each XML element supports some dictionary-like and some list-like access
methods. Dictionary-like operations are used to access attribute
values, and list-like operations are used to access child nodes.
Operation Result
-------------------------------------------------------------------------------------
‘elem[n]’ Returns n’th child element.
‘elem[m:n]’ Returns list of m’th through n’th child
elements.
‘len(elem)’ Returns number of child elements.
‘list(elem)’ Returns list of child elements.
‘elem.append(elem2)’ Adds `elem2' as a child.
‘elem.insert(index, elem2)’ Inserts `elem2' at the specified location.
‘del elem[n]’ Deletes n’th child element.
‘elem.keys()’ Returns list of attribute names.
‘elem.get(name)’ Returns value of attribute `name'.
‘elem.set(name, value)’ Sets new value for attribute `name'.
‘elem.attrib’ Retrieves the dictionary containing
attributes.
‘del elem.attrib[name]’ Deletes attribute `name'.
Comments and processing instructions are also represented as ‘Element’
nodes. To check if a node is a comment or processing instructions:
if elem.tag is ET.Comment:
...
elif elem.tag is ET.ProcessingInstruction:
...
To generate XML output, you should call the ‘ElementTree.write()’
method. Like ‘parse()’, it can take either a string or a file-like
object:
# Encoding is US-ASCII
tree.write('output.xml')
# Encoding is UTF-8
f = open('output.xml', 'w')
tree.write(f, encoding='utf-8')
(Caution: the default encoding used for output is ASCII. For general XML
work, where an element’s name may contain arbitrary Unicode characters,
ASCII isn’t a very useful encoding because it will raise an exception if
an element’s name contains any characters with values greater than 127.
Therefore, it’s best to specify a different encoding such as UTF-8 that
can handle any Unicode character.)
This section is only a partial description of the ElementTree
interfaces. Please read the package’s official documentation for more
details.
See also
........
‘http://effbot.org/zone/element-index.htm’
Official documentation for ElementTree.
File: python.info, Node: The hashlib package, Next: The sqlite3 package, Prev: The ElementTree package, Up: New Improved and Removed Modules
1.12.13.3 The hashlib package
.............................
A new *note hashlib: 8d. module, written by Gregory P. Smith, has been
added to replace the ‘md5’ and ‘sha’ modules. *note hashlib: 8d. adds
support for additional secure hashes (SHA-224, SHA-256, SHA-384, and
SHA-512). When available, the module uses OpenSSL for fast platform
optimized implementations of algorithms.
The old ‘md5’ and ‘sha’ modules still exist as wrappers around hashlib
to preserve backwards compatibility. The new module’s interface is very
close to that of the old modules, but not identical. The most
significant difference is that the constructor functions for creating
new hashing objects are named differently.
# Old versions
h = md5.md5()
h = md5.new()
# New version
h = hashlib.md5()
# Old versions
h = sha.sha()
h = sha.new()
# New version
h = hashlib.sha1()
# Hash that weren't previously available
h = hashlib.sha224()
h = hashlib.sha256()
h = hashlib.sha384()
h = hashlib.sha512()
# Alternative form
h = hashlib.new('md5') # Provide algorithm as a string
Once a hash object has been created, its methods are the same as before:
‘update(string)’ hashes the specified string into the current digest
state, ‘digest()’ and ‘hexdigest()’ return the digest value as a binary
string or a string of hex digits, and *note copy(): 26. returns a new
hashing object with the same digest state.
See also
........
The documentation for the *note hashlib: 8d. module.
File: python.info, Node: The sqlite3 package, Next: The wsgiref package, Prev: The hashlib package, Up: New Improved and Removed Modules
1.12.13.4 The sqlite3 package
.............................
The pysqlite module (‘http://www.pysqlite.org’), a wrapper for the
SQLite embedded database, has been added to the standard library under
the package name *note sqlite3: f2.
SQLite is a C library that provides a lightweight disk-based database
that doesn’t require a separate server process and allows accessing the
database using a nonstandard variant of the SQL query language. Some
applications can use SQLite for internal data storage. It’s also
possible to prototype an application using SQLite and then port the code
to a larger database such as PostgreSQL or Oracle.
pysqlite was written by Gerhard Häring and provides a SQL interface
compliant with the DB-API 2.0 specification described by PEP 249(1).
If you’re compiling the Python source yourself, note that the source
tree doesn’t include the SQLite code, only the wrapper module. You’ll
need to have the SQLite libraries and headers installed before compiling
Python, and the build process will compile the module when the necessary
headers are available.
To use the module, you must first create a ‘Connection’ object that
represents the database. Here the data will be stored in the
‘/tmp/example’ file:
conn = sqlite3.connect('/tmp/example')
You can also supply the special name ‘:memory:’ to create a database in
RAM.
Once you have a ‘Connection’, you can create a ‘Cursor’ object and call
its ‘execute()’ method to perform SQL commands:
c = conn.cursor()
# Create table
c.execute('''create table stocks
(date text, trans text, symbol text,
qty real, price real)''')
# Insert a row of data
c.execute("""insert into stocks
values ('2006-01-05','BUY','RHAT',100,35.14)""")
Usually your SQL operations will need to use values from Python
variables. You shouldn’t assemble your query using Python’s string
operations because doing so is insecure; it makes your program
vulnerable to an SQL injection attack.
Instead, use the DB-API’s parameter substitution. Put ‘?’ as a
placeholder wherever you want to use a value, and then provide a tuple
of values as the second argument to the cursor’s ‘execute()’ method.
(Other database modules may use a different placeholder, such as ‘%s’ or
‘:1’.) For example:
# Never do this -- insecure!
symbol = 'IBM'
c.execute("... where symbol = '%s'" % symbol)
# Do this instead
t = (symbol,)
c.execute('select * from stocks where symbol=?', t)
# Larger example
for t in (('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
):
c.execute('insert into stocks values (?,?,?,?,?)', t)
To retrieve data after executing a SELECT statement, you can either
treat the cursor as an iterator, call the cursor’s ‘fetchone()’ method
to retrieve a single matching row, or call ‘fetchall()’ to get a list of
the matching rows.
This example uses the iterator form:
>>> c = conn.cursor()
>>> c.execute('select * from stocks order by price')
>>> for row in c:
... print row
...
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.140000000000001)
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
(u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0)
>>>
For more information about the SQL dialect supported by SQLite, see
‘https://www.sqlite.org’.
See also
........
‘http://www.pysqlite.org’
The pysqlite web page.
‘https://www.sqlite.org’
The SQLite web page; the documentation describes the syntax and the
available data types for the supported SQL dialect.
The documentation for the *note sqlite3: f2. module.
PEP 249(2) - Database API Specification 2.0
PEP written by Marc-André Lemburg.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0249
(2) https://www.python.org/dev/peps/pep-0249
File: python.info, Node: The wsgiref package, Prev: The sqlite3 package, Up: New Improved and Removed Modules
1.12.13.5 The wsgiref package
.............................
The Web Server Gateway Interface (WSGI) v1.0 defines a standard
interface between web servers and Python web applications and is
described in PEP 333(1). The *note wsgiref: 12c. package is a reference
implementation of the WSGI specification.
The package includes a basic HTTP server that will run a WSGI
application; this server is useful for debugging but isn’t intended for
production use. Setting up a server takes only a few lines of code:
from wsgiref import simple_server
wsgi_app = ...
host = ''
port = 8000
httpd = simple_server.make_server(host, port, wsgi_app)
httpd.serve_forever()
See also
........
‘http://www.wsgi.org’
A central web site for WSGI-related resources.
PEP 333(2) - Python Web Server Gateway Interface v1.0
PEP written by Phillip J. Eby.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0333
(2) https://www.python.org/dev/peps/pep-0333
File: python.info, Node: Build and C API Changes<10>, Next: Porting to Python 2 5, Prev: New Improved and Removed Modules, Up: What’s New in Python 2 5
1.12.14 Build and C API Changes
-------------------------------
Changes to Python’s build process and to the C API include:
* The Python source tree was converted from CVS to Subversion, in a
complex migration procedure that was supervised and flawlessly
carried out by Martin von Löwis. The procedure was developed as
PEP 347(1).
* Coverity, a company that markets a source code analysis tool called
Prevent, provided the results of their examination of the Python
source code. The analysis found about 60 bugs that were quickly
fixed. Many of the bugs were refcounting problems, often occurring
in error-handling code. See ‘https://scan.coverity.com’ for the
statistics.
* The largest change to the C API came from PEP 353(2), which
modifies the interpreter to use a ‘Py_ssize_t’ type definition
instead of ‘int’. See the earlier section *note PEP 353; Using
ssize_t as the index type: d7c. for a discussion of this change.
* The design of the bytecode compiler has changed a great deal, no
longer generating bytecode by traversing the parse tree. Instead
the parse tree is converted to an abstract syntax tree (or AST),
and it is the abstract syntax tree that’s traversed to produce the
bytecode.
It’s possible for Python code to obtain AST objects by using the
*note compile(): 1b4. built-in and specifying ‘_ast.PyCF_ONLY_AST’
as the value of the `flags' parameter:
from _ast import PyCF_ONLY_AST
ast = compile("""a=0
for i in range(10):
a += i
""", "", 'exec', PyCF_ONLY_AST)
assignment = ast.body[0]
for_loop = ast.body[1]
No official documentation has been written for the AST code yet,
but PEP 339(3) discusses the design. To start learning about the
code, read the definition of the various AST nodes in
‘Parser/Python.asdl’. A Python script reads this file and
generates a set of C structure definitions in
‘Include/Python-ast.h’. The ‘PyParser_ASTFromString()’ and
‘PyParser_ASTFromFile()’, defined in ‘Include/pythonrun.h’, take
Python source as input and return the root of an AST representing
the contents. This AST can then be turned into a code object by
‘PyAST_Compile()’. For more information, read the source code, and
then ask questions on python-dev.
The AST code was developed under Jeremy Hylton’s management, and
implemented by (in alphabetical order) Brett Cannon, Nick Coghlan,
Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim
Peters, Armin Rigo, and Neil Schemenauer, plus the participants in
a number of AST sprints at conferences such as PyCon.
* Evan Jones’s patch to obmalloc, first described in a talk at PyCon
DC 2005, was applied. Python 2.4 allocated small objects in
256K-sized arenas, but never freed arenas. With this patch, Python
will free arenas when they’re empty. The net effect is that on
some platforms, when you allocate many objects, Python’s memory
usage may actually drop when you delete them and the memory may be
returned to the operating system. (Implemented by Evan Jones, and
reworked by Tim Peters.)
Note that this change means extension modules must be more careful
when allocating memory. Python’s API has many different functions
for allocating memory that are grouped into families. For example,
*note PyMem_Malloc(): 505, *note PyMem_Realloc(): 96f, and *note
PyMem_Free(): da9. are one family that allocates raw memory, while
*note PyObject_Malloc(): 508, *note PyObject_Realloc(): daa, and
*note PyObject_Free(): 504. are another family that’s supposed to
be used for creating Python objects.
Previously these different families all reduced to the platform’s
‘malloc()’ and ‘free()’ functions. This meant it didn’t matter if
you got things wrong and allocated memory with the ‘PyMem()’
function but freed it with the *note PyObject(): 4ba. function.
With 2.5’s changes to obmalloc, these families now do different
things and mismatches will probably result in a segfault. You
should carefully test your C extension modules with Python 2.5.
* The built-in set types now have an official C API. Call *note
PySet_New(): dab. and *note PyFrozenSet_New(): dac. to create a new
set, *note PySet_Add(): dad. and *note PySet_Discard(): dae. to add
and remove elements, and *note PySet_Contains(): daf. and *note
PySet_Size(): db0. to examine the set’s state. (Contributed by
Raymond Hettinger.)
* C code can now obtain information about the exact revision of the
Python interpreter by calling the *note Py_GetBuildInfo(): d9a.
function that returns a string of build information like this:
‘"trunk:45355:45356M, Apr 13 2006, 07:42:19"’. (Contributed by
Barry Warsaw.)
* Two new macros can be used to indicate C functions that are local
to the current file so that a faster calling convention can be
used. ‘Py_LOCAL(type)’ declares the function as returning a value
of the specified `type' and uses a fast-calling qualifier.
‘Py_LOCAL_INLINE(type)’ does the same thing and also requests the
function be inlined. If ‘PY_LOCAL_AGGRESSIVE()’ is defined before
‘python.h’ is included, a set of more aggressive optimizations are
enabled for the module; you should benchmark the results to find
out if these optimizations actually make the code faster.
(Contributed by Fredrik Lundh at the NeedForSpeed sprint.)
* ‘PyErr_NewException(name, base, dict)’ can now accept a tuple of
base classes as its `base' argument. (Contributed by Georg
Brandl.)
* The ‘PyErr_Warn()’ function for issuing warnings is now deprecated
in favour of ‘PyErr_WarnEx(category, message, stacklevel)’ which
lets you specify the number of stack frames separating this
function and the caller. A `stacklevel' of 1 is the function
calling *note PyErr_WarnEx(): db1, 2 is the function above that,
and so forth. (Added by Neal Norwitz.)
* The CPython interpreter is still written in C, but the code can now
be compiled with a C++ compiler without errors. (Implemented by
Anthony Baxter, Martin von Löwis, Skip Montanaro.)
* The ‘PyRange_New()’ function was removed. It was never documented,
never used in the core code, and had dangerously lax error
checking. In the unlikely case that your extensions were using it,
you can replace it by something like the following:
range = PyObject_CallFunction((PyObject*) &PyRange_Type, "lll",
start, stop, step);
* Menu:
* Port-Specific Changes::
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0347
(2) https://www.python.org/dev/peps/pep-0353
(3) https://www.python.org/dev/peps/pep-0339
File: python.info, Node: Port-Specific Changes, Up: Build and C API Changes<10>
1.12.14.1 Port-Specific Changes
...............................
* MacOS X (10.3 and higher): dynamic loading of modules now uses the
‘dlopen()’ function instead of MacOS-specific functions.
* MacOS X: an ‘--enable-universalsdk’ switch was added to the
‘configure’ script that compiles the interpreter as a universal
binary able to run on both PowerPC and Intel processors.
(Contributed by Ronald Oussoren; bpo-2573(1).)
* Windows: ‘.dll’ is no longer supported as a filename extension for
extension modules. ‘.pyd’ is now the only filename extension that
will be searched for.
---------- Footnotes ----------
(1) https://bugs.python.org/issue2573
File: python.info, Node: Porting to Python 2 5, Next: Acknowledgements<3>, Prev: Build and C API Changes<10>, Up: What’s New in Python 2 5
1.12.15 Porting to Python 2.5
-----------------------------
This section lists previously described changes that may require changes
to your code:
* ASCII is now the default encoding for modules. It’s now a syntax
error if a module contains string literals with 8-bit characters
but doesn’t have an encoding declaration. In Python 2.4 this
triggered a warning, not a syntax error.
* Previously, the ‘gi_frame’ attribute of a generator was always a
frame object. Because of the PEP 342(1) changes described in
section *note PEP 342; New Generator Features: d66, it’s now
possible for ‘gi_frame’ to be ‘None’.
* A new warning, *note UnicodeWarning: d87, is triggered when you
attempt to compare a Unicode string and an 8-bit string that can’t
be converted to Unicode using the default ASCII encoding.
Previously such comparisons would raise a *note UnicodeDecodeError:
1fd. exception.
* Library: the *note csv: 2a. module is now stricter about multi-line
quoted fields. If your files contain newlines embedded within
fields, the input should be split into lines in a manner which
preserves the newline characters.
* Library: the *note locale: a9. module’s *note format(): 4db.
function’s would previously accept any string as long as no more
than one %char specifier appeared. In Python 2.5, the argument
must be exactly one %char specifier with no surrounding text.
* Library: The *note pickle: ca. and ‘cPickle’ modules no longer
accept a return value of ‘None’ from the *note __reduce__(): 19b.
method; the method must return a tuple of arguments instead. The
modules also no longer accept the deprecated `bin' keyword
parameter.
* Library: The ‘SimpleXMLRPCServer’ and ‘DocXMLRPCServer’ classes now
have a ‘rpc_paths’ attribute that constrains XML-RPC operations to
a limited set of URL paths; the default is to allow only ‘'/'’ and
‘'/RPC2'’. Setting ‘rpc_paths’ to ‘None’ or an empty tuple
disables this path checking.
* C API: Many functions now use ‘Py_ssize_t’ instead of ‘int’ to
allow processing more data on 64-bit machines. Extension code may
need to make the same change to avoid warnings and to support
64-bit machines. See the earlier section *note PEP 353; Using
ssize_t as the index type: d7c. for a discussion of this change.
* C API: The obmalloc changes mean that you must be careful to not
mix usage of the ‘PyMem_*()’ and ‘PyObject_*()’ families of
functions. Memory allocated with one family’s ‘*_Malloc()’ must be
freed with the corresponding family’s ‘*_Free()’ function.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0342
File: python.info, Node: Acknowledgements<3>, Prev: Porting to Python 2 5, Up: What’s New in Python 2 5
1.12.16 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Georg Brandl, Nick Coghlan, Phillip J. Eby, Lars Gustäbel,
Raymond Hettinger, Ralf W. Grosse-Kunstleve, Kent Johnson, Iain Lowe,
Martin von Löwis, Fredrik Lundh, Andrew McNamara, Skip Montanaro,
Gustavo Niemeyer, Paul Prescod, James Pryor, Mike Rovner, Scott Weikart,
Barry Warsaw, Thomas Wouters.
File: python.info, Node: What’s New in Python 2 4, Next: What’s New in Python 2 3, Prev: What’s New in Python 2 5, Up: What’s New in Python
1.13 What’s New in Python 2.4
=============================
Author: A.M. Kuchling
This article explains the new features in Python 2.4.1, released on
March 30, 2005.
Python 2.4 is a medium-sized release. It doesn’t introduce as many
changes as the radical Python 2.2, but introduces more features than the
conservative 2.3 release. The most significant new language features
are function decorators and generator expressions; most other changes
are to the standard library.
According to the CVS change logs, there were 481 patches applied and 502
bugs fixed between Python 2.3 and 2.4. Both figures are likely to be
underestimates.
This article doesn’t attempt to provide a complete specification of
every single new feature, but instead provides a brief introduction to
each feature. For full details, you should refer to the documentation
for Python 2.4, such as the Python Library Reference and the Python
Reference Manual. Often you will be referred to the PEP for a
particular new feature for explanations of the implementation and design
rationale.
* Menu:
* PEP 218; Built-In Set Objects: PEP 218 Built-In Set Objects.
* PEP 237; Unifying Long Integers and Integers: PEP 237 Unifying Long Integers and Integers.
* PEP 289; Generator Expressions: PEP 289 Generator Expressions.
* PEP 292; Simpler String Substitutions: PEP 292 Simpler String Substitutions.
* PEP 318; Decorators for Functions and Methods: PEP 318 Decorators for Functions and Methods.
* PEP 322; Reverse Iteration: PEP 322 Reverse Iteration.
* PEP 324; New subprocess Module: PEP 324 New subprocess Module.
* PEP 327; Decimal Data Type: PEP 327 Decimal Data Type.
* PEP 328; Multi-line Imports: PEP 328 Multi-line Imports.
* PEP 331; Locale-Independent Float/String Conversions: PEP 331 Locale-Independent Float/String Conversions.
* Other Language Changes: Other Language Changes<12>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules<3>.
* Build and C API Changes: Build and C API Changes<11>.
* Porting to Python 2.4: Porting to Python 2 4.
* Acknowledgements: Acknowledgements<4>.
File: python.info, Node: PEP 218 Built-In Set Objects, Next: PEP 237 Unifying Long Integers and Integers, Up: What’s New in Python 2 4
1.13.1 PEP 218: Built-In Set Objects
------------------------------------
Python 2.3 introduced the ‘sets’ module. C implementations of set data
types have now been added to the Python core as two new built-in types,
‘set(iterable)’ and ‘frozenset(iterable)’. They provide high speed
operations for membership testing, for eliminating duplicates from
sequences, and for mathematical operations like unions, intersections,
differences, and symmetric differences.
>>> a = set('abracadabra') # form a set from a string
>>> 'z' in a # fast membership testing
False
>>> a # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> ''.join(a) # convert back into a string
'arbcd'
>>> b = set('alacazam') # form a second set
>>> a - b # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b # letters in both a and b
set(['a', 'c'])
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
>>> a.add('z') # add a new element
>>> a.update('wxy') # add multiple new elements
>>> a
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
>>> a.remove('x') # take one element out
>>> a
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
The *note frozenset(): bee. type is an immutable version of *note set():
b6f. Since it is immutable and hashable, it may be used as a dictionary
key or as a member of another set.
The ‘sets’ module remains in the standard library, and may be useful if
you wish to subclass the ‘Set’ or ‘ImmutableSet’ classes. There are
currently no plans to deprecate the module.
See also
........
PEP 218(1) - Adding a Built-In Set Object Type
Originally proposed by Greg Wilson and ultimately implemented by
Raymond Hettinger.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0218
File: python.info, Node: PEP 237 Unifying Long Integers and Integers, Next: PEP 289 Generator Expressions, Prev: PEP 218 Built-In Set Objects, Up: What’s New in Python 2 4
1.13.2 PEP 237: Unifying Long Integers and Integers
---------------------------------------------------
The lengthy transition process for this PEP, begun in Python 2.2, takes
another step forward in Python 2.4. In 2.3, certain integer operations
that would behave differently after int/long unification triggered *note
FutureWarning: 325. warnings and returned values limited to 32 or 64
bits (depending on your platform). In 2.4, these expressions no longer
produce a warning and instead produce a different result that’s usually
a long integer.
The problematic expressions are primarily left shifts and lengthy
hexadecimal and octal constants. For example, ‘2 << 32’ results in a
warning in 2.3, evaluating to 0 on 32-bit platforms. In Python 2.4,
this expression now returns the correct answer, 8589934592.
See also
........
PEP 237(1) - Unifying Long Integers and Integers
Original PEP written by Moshe Zadka and GvR. The changes for 2.4
were implemented by Kalle Svensson.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0237
File: python.info, Node: PEP 289 Generator Expressions, Next: PEP 292 Simpler String Substitutions, Prev: PEP 237 Unifying Long Integers and Integers, Up: What’s New in Python 2 4
1.13.3 PEP 289: Generator Expressions
-------------------------------------
The iterator feature introduced in Python 2.2 and the *note itertools:
a3. module make it easier to write programs that loop through large data
sets without having the entire data set in memory at one time. List
comprehensions don’t fit into this picture very well because they
produce a Python list object containing all of the items. This
unavoidably pulls all of the objects into memory, which can be a problem
if your data set is very large. When trying to write a
functionally-styled program, it would be natural to write something
like:
links = [link for link in get_all_links() if not link.followed]
for link in links:
...
instead of
for link in get_all_links():
if link.followed:
continue
...
The first form is more concise and perhaps more readable, but if you’re
dealing with a large number of link objects you’d have to write the
second form to avoid having all link objects in memory at the same time.
Generator expressions work similarly to list comprehensions but don’t
materialize the entire list; instead they create a generator that will
return elements one by one. The above example could be written as:
links = (link for link in get_all_links() if not link.followed)
for link in links:
...
Generator expressions always have to be written inside parentheses, as
in the above example. The parentheses signalling a function call also
count, so if you want to create an iterator that will be immediately
passed to a function you could write:
print sum(obj.count for obj in list_all_objects())
Generator expressions differ from list comprehensions in various small
ways. Most notably, the loop variable (`obj' in the above example) is
not accessible outside of the generator expression. List comprehensions
leave the variable assigned to its last value; future versions of Python
will change this, making list comprehensions match generator expressions
in this respect.
See also
........
PEP 289(1) - Generator Expressions
Proposed by Raymond Hettinger and implemented by Jiwon Seo with
early efforts steered by Hye-Shik Chang.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0289
File: python.info, Node: PEP 292 Simpler String Substitutions, Next: PEP 318 Decorators for Functions and Methods, Prev: PEP 289 Generator Expressions, Up: What’s New in Python 2 4
1.13.4 PEP 292: Simpler String Substitutions
--------------------------------------------
Some new classes in the standard library provide an alternative
mechanism for substituting variables into strings; this style of
substitution may be better for applications where untrained users need
to edit templates.
The usual way of substituting variables by name is the ‘%’ operator:
>>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'}
'2: The Best of Times'
When writing the template string, it can be easy to forget the ‘i’ or
‘s’ after the closing parenthesis. This isn’t a big problem if the
template is in a Python module, because you run the code, get an
“Unsupported format character” *note ValueError: 1fb, and fix the
problem. However, consider an application such as Mailman where
template strings or translations are being edited by users who aren’t
aware of the Python language. The format string’s syntax is complicated
to explain to such users, and if they make a mistake, it’s difficult to
provide helpful feedback to them.
PEP 292 adds a ‘Template’ class to the *note string: f6. module that
uses ‘$’ to indicate a substitution:
>>> import string
>>> t = string.Template('$page: $title')
>>> t.substitute({'page':2, 'title': 'The Best of Times'})
'2: The Best of Times'
If a key is missing from the dictionary, the ‘substitute()’ method will
raise a *note KeyError: 2c7. There’s also a ‘safe_substitute()’ method
that ignores missing keys:
>>> t = string.Template('$page: $title')
>>> t.safe_substitute({'page':3})
'3: $title'
See also
........
PEP 292(1) - Simpler String Substitutions
Written and implemented by Barry Warsaw.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0292
File: python.info, Node: PEP 318 Decorators for Functions and Methods, Next: PEP 322 Reverse Iteration, Prev: PEP 292 Simpler String Substitutions, Up: What’s New in Python 2 4
1.13.5 PEP 318: Decorators for Functions and Methods
----------------------------------------------------
Python 2.2 extended Python’s object model by adding static methods and
class methods, but it didn’t extend Python’s syntax to provide any new
way of defining static or class methods. Instead, you had to write a
*note def: dbe. statement in the usual way, and pass the resulting
method to a *note staticmethod(): 1d9. or *note classmethod(): 1d8.
function that would wrap up the function as a method of the new type.
Your code would look like this:
class C:
def meth (cls):
...
meth = classmethod(meth) # Rebind name to wrapped-up class method
If the method was very long, it would be easy to miss or forget the
*note classmethod(): 1d8. invocation after the function body.
The intention was always to add some syntax to make such definitions
more readable, but at the time of 2.2’s release a good syntax was not
obvious. Today a good syntax `still' isn’t obvious but users are asking
for easier access to the feature; a new syntactic feature has been added
to meet this need.
The new feature is called “function decorators”. The name comes from
the idea that *note classmethod(): 1d8, *note staticmethod(): 1d9, and
friends are storing additional information on a function object; they’re
`decorating' functions with more details.
The notation borrows from Java and uses the ‘'@'’ character as an
indicator. Using the new syntax, the example above would be written:
class C:
@classmethod
def meth (cls):
...
The ‘@classmethod’ is shorthand for the ‘meth=classmethod(meth)’
assignment. More generally, if you have the following:
@A
@B
@C
def f ():
...
It’s equivalent to the following pre-decorator code:
def f(): ...
f = A(B(C(f)))
Decorators must come on the line before a function definition, one
decorator per line, and can’t be on the same line as the def statement,
meaning that ‘@A def f(): ...’ is illegal. You can only decorate
function definitions, either at the module level or inside a class; you
can’t decorate class definitions.
A decorator is just a function that takes the function to be decorated
as an argument and returns either the same function or some new object.
The return value of the decorator need not be callable (though it
typically is), unless further decorators will be applied to the result.
It’s easy to write your own decorators. The following simple example
just sets an attribute on the function object:
>>> def deco(func):
... func.attr = 'decorated'
... return func
...
>>> @deco
... def f(): pass
...
>>> f
>>> f.attr
'decorated'
>>>
As a slightly more realistic example, the following decorator checks
that the supplied argument is an integer:
def require_int (func):
def wrapper (arg):
assert isinstance(arg, int)
return func(arg)
return wrapper
@require_int
def p1 (arg):
print arg
@require_int
def p2(arg):
print arg*2
An example in PEP 318(1) contains a fancier version of this idea that
lets you both specify the required type and check the returned type.
Decorator functions can take arguments. If arguments are supplied, your
decorator function is called with only those arguments and must return a
new decorator function; this function must take a single function and
return a function, as previously described. In other words, ‘@A @B
@C(args)’ becomes:
def f(): ...
_deco = C(args)
f = A(B(_deco(f)))
Getting this right can be slightly brain-bending, but it’s not too
difficult.
A small related change makes the ‘func_name’ attribute of functions
writable. This attribute is used to display function names in
tracebacks, so decorators should change the name of any new function
that’s constructed and returned.
See also
........
PEP 318(2) - Decorators for Functions, Methods and Classes
Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several
people wrote patches implementing function decorators, but the one
that was actually checked in was patch #979728, written by Mark
Russell.
‘https://wiki.python.org/moin/PythonDecoratorLibrary’
This Wiki page contains several examples of decorators.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0318
(2) https://www.python.org/dev/peps/pep-0318
File: python.info, Node: PEP 322 Reverse Iteration, Next: PEP 324 New subprocess Module, Prev: PEP 318 Decorators for Functions and Methods, Up: What’s New in Python 2 4
1.13.6 PEP 322: Reverse Iteration
---------------------------------
A new built-in function, ‘reversed(seq)’, takes a sequence and returns
an iterator that loops over the elements of the sequence in reverse
order.
>>> for i in reversed(xrange(1,4)):
... print i
...
3
2
1
Compared to extended slicing, such as ‘range(1,4)[::-1]’, *note
reversed(): 18e. is easier to read, runs faster, and uses substantially
less memory.
Note that *note reversed(): 18e. only accepts sequences, not arbitrary
iterators. If you want to reverse an iterator, first convert it to a
list with *note list(): 262.
>>> input = open('/etc/passwd', 'r')
>>> for line in reversed(list(input)):
... print line
...
root:*:0:0:System Administrator:/var/root:/bin/tcsh
...
See also
........
PEP 322(1) - Reverse Iteration
Written and implemented by Raymond Hettinger.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0322
File: python.info, Node: PEP 324 New subprocess Module, Next: PEP 327 Decimal Data Type, Prev: PEP 322 Reverse Iteration, Up: What’s New in Python 2 4
1.13.7 PEP 324: New subprocess Module
-------------------------------------
The standard library provides a number of ways to execute a subprocess,
offering different features and different levels of complexity.
‘os.system(command)’ is easy to use, but slow (it runs a shell process
which executes the command) and dangerous (you have to be careful about
escaping the shell’s metacharacters). The ‘popen2’ module offers
classes that can capture standard output and standard error from the
subprocess, but the naming is confusing. The *note subprocess: f9.
module cleans this up, providing a unified interface that offers all the
features you might need.
Instead of ‘popen2’’s collection of classes, *note subprocess: f9.
contains a single class called ‘Popen’ whose constructor supports a
number of different keyword arguments.
class Popen(args, bufsize=0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0):
`args' is commonly a sequence of strings that will be the arguments to
the program executed as the subprocess. (If the `shell' argument is
true, `args' can be a string which will then be passed on to the shell
for interpretation, just as *note os.system(): dc1. does.)
`stdin', `stdout', and `stderr' specify what the subprocess’s input,
output, and error streams will be. You can provide a file object or a
file descriptor, or you can use the constant ‘subprocess.PIPE’ to create
a pipe between the subprocess and the parent.
The constructor has a number of handy options:
* `close_fds' requests that all file descriptors be closed before
running the subprocess.
* `cwd' specifies the working directory in which the subprocess will
be executed (defaulting to whatever the parent’s working directory
is).
* `env' is a dictionary specifying environment variables.
* `preexec_fn' is a function that gets called before the child is
started.
* `universal_newlines' opens the child’s input and output using
Python’s *note universal newlines: 5f4. feature.
Once you’ve created the ‘Popen’ instance, you can call its ‘wait()’
method to pause until the subprocess has exited, ‘poll()’ to check if
it’s exited without pausing, or ‘communicate(data)’ to send the string
`data' to the subprocess’s standard input. ‘communicate(data)’ then
reads any data that the subprocess has sent to its standard output or
standard error, returning a tuple ‘(stdout_data, stderr_data)’.
‘call()’ is a shortcut that passes its arguments along to the ‘Popen’
constructor, waits for the command to complete, and returns the status
code of the subprocess. It can serve as a safer analog to *note
os.system(): dc1.:
sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb'])
if sts == 0:
# Success
...
else:
# dpkg returned an error
...
The command is invoked without use of the shell. If you really do want
to use the shell, you can add ‘shell=True’ as a keyword argument and
provide a string instead of a sequence:
sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True)
The PEP takes various examples of shell and Python code and shows how
they’d be translated into Python code that uses *note subprocess: f9.
Reading this section of the PEP is highly recommended.
See also
........
PEP 324(1) - subprocess - New process module
Written and implemented by Peter Åstrand, with assistance from
Fredrik Lundh and others.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0324
File: python.info, Node: PEP 327 Decimal Data Type, Next: PEP 328 Multi-line Imports, Prev: PEP 324 New subprocess Module, Up: What’s New in Python 2 4
1.13.8 PEP 327: Decimal Data Type
---------------------------------
Python has always supported floating-point (FP) numbers, based on the
underlying C ‘double’ type, as a data type. However, while most
programming languages provide a floating-point type, many people (even
programmers) are unaware that floating-point numbers don’t represent
certain decimal fractions accurately. The new ‘Decimal’ type can
represent these fractions accurately, up to a user-specified precision
limit.
* Menu:
* Why is Decimal needed?::
* The Decimal type::
* The Context type::
File: python.info, Node: Why is Decimal needed?, Next: The Decimal type, Up: PEP 327 Decimal Data Type
1.13.8.1 Why is Decimal needed?
...............................
The limitations arise from the representation used for floating-point
numbers. FP numbers are made up of three components:
* The sign, which is positive or negative.
* The mantissa, which is a single-digit binary number followed by a
fractional part. For example, ‘1.01’ in base-2 notation is ‘1 +
0/2 + 1/4’, or 1.25 in decimal notation.
* The exponent, which tells where the decimal point is located in the
number represented.
For example, the number 1.25 has positive sign, a mantissa value of 1.01
(in binary), and an exponent of 0 (the decimal point doesn’t need to be
shifted). The number 5 has the same sign and mantissa, but the exponent
is 2 because the mantissa is multiplied by 4 (2 to the power of the
exponent 2); 1.25 * 4 equals 5.
Modern systems usually provide floating-point support that conforms to a
standard called IEEE 754. C’s ‘double’ type is usually implemented as a
64-bit IEEE 754 number, which uses 52 bits of space for the mantissa.
This means that numbers can only be specified to 52 bits of precision.
If you’re trying to represent numbers whose expansion repeats endlessly,
the expansion is cut off after 52 bits. Unfortunately, most software
needs to produce output in base 10, and common fractions in base 10 are
often repeating decimals in binary. For example, 1.1 decimal is binary
‘1.0001100110011 ...’; .1 = 1/16 + 1/32 + 1/256 plus an infinite number
of additional terms. IEEE 754 has to chop off that infinitely repeated
decimal after 52 digits, so the representation is slightly inaccurate.
Sometimes you can see this inaccuracy when the number is printed:
>>> 1.1
1.1000000000000001
The inaccuracy isn’t always visible when you print the number because
the FP-to-decimal-string conversion is provided by the C library, and
most C libraries try to produce sensible output. Even if it’s not
displayed, however, the inaccuracy is still there and subsequent
operations can magnify the error.
For many applications this doesn’t matter. If I’m plotting points and
displaying them on my monitor, the difference between 1.1 and
1.1000000000000001 is too small to be visible. Reports often limit
output to a certain number of decimal places, and if you round the
number to two or three or even eight decimal places, the error is never
apparent. However, for applications where it does matter, it’s a lot of
work to implement your own custom arithmetic routines.
Hence, the ‘Decimal’ type was created.
File: python.info, Node: The Decimal type, Next: The Context type, Prev: Why is Decimal needed?, Up: PEP 327 Decimal Data Type
1.13.8.2 The ‘Decimal’ type
...........................
A new module, *note decimal: 36, was added to Python’s standard library.
It contains two classes, ‘Decimal’ and ‘Context’. ‘Decimal’ instances
represent numbers, and ‘Context’ instances are used to wrap up various
settings such as the precision and default rounding mode.
‘Decimal’ instances are immutable, like regular Python integers and FP
numbers; once it’s been created, you can’t change the value an instance
represents. ‘Decimal’ instances can be created from integers or
strings:
>>> import decimal
>>> decimal.Decimal(1972)
Decimal("1972")
>>> decimal.Decimal("1.1")
Decimal("1.1")
You can also provide tuples containing the sign, the mantissa
represented as a tuple of decimal digits, and the exponent:
>>> decimal.Decimal((1, (1, 4, 7, 5), -2))
Decimal("-14.75")
Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1
is negative.
Converting from floating-point numbers poses a bit of a problem: should
the FP number representing 1.1 turn into the decimal number for exactly
1.1, or for 1.1 plus whatever inaccuracies are introduced? The decision
was to dodge the issue and leave such a conversion out of the API.
Instead, you should convert the floating-point number into a string
using the desired precision and pass the string to the ‘Decimal’
constructor:
>>> f = 1.1
>>> decimal.Decimal(str(f))
Decimal("1.1")
>>> decimal.Decimal('%.12f' % f)
Decimal("1.100000000000")
Once you have ‘Decimal’ instances, you can perform the usual
mathematical operations on them. One limitation: exponentiation
requires an integer exponent:
>>> a = decimal.Decimal('35.72')
>>> b = decimal.Decimal('1.73')
>>> a+b
Decimal("37.45")
>>> a-b
Decimal("33.99")
>>> a*b
Decimal("61.7956")
>>> a/b
Decimal("20.64739884393063583815028902")
>>> a ** 2
Decimal("1275.9184")
>>> a**b
Traceback (most recent call last):
...
decimal.InvalidOperation: x ** (non-integer)
You can combine ‘Decimal’ instances with integers, but not with
floating-point numbers:
>>> a + 4
Decimal("39.72")
>>> a + 4.5
Traceback (most recent call last):
...
TypeError: You can interact Decimal only with int, long or Decimal data types.
>>>
‘Decimal’ numbers can be used with the *note math: b1. and *note cmath:
19. modules, but note that they’ll be immediately converted to
floating-point numbers before the operation is performed, resulting in a
possible loss of precision and accuracy. You’ll also get back a regular
floating-point number and not a ‘Decimal’.
>>> import math, cmath
>>> d = decimal.Decimal('123456789012.345')
>>> math.sqrt(d)
351364.18288201344
>>> cmath.sqrt(-d)
351364.18288201344j
‘Decimal’ instances have a ‘sqrt()’ method that returns a ‘Decimal’, but
if you need other things such as trigonometric functions you’ll have to
implement them.
>>> d.sqrt()
Decimal("351364.1828820134592177245001")
File: python.info, Node: The Context type, Prev: The Decimal type, Up: PEP 327 Decimal Data Type
1.13.8.3 The ‘Context’ type
...........................
Instances of the ‘Context’ class encapsulate several settings for
decimal operations:
* ‘prec’ is the precision, the number of decimal places.
* ‘rounding’ specifies the rounding mode. The *note decimal: 36.
module has constants for the various possibilities: ‘ROUND_DOWN’,
‘ROUND_CEILING’, ‘ROUND_HALF_EVEN’, and various others.
* ‘traps’ is a dictionary specifying what happens on encountering
certain error conditions: either an exception is raised or a value
is returned. Some examples of error conditions are division by
zero, loss of precision, and overflow.
There’s a thread-local default context available by calling
‘getcontext()’; you can change the properties of this context to alter
the default precision, rounding, or trap handling. The following
example shows the effect of changing the precision of the default
context:
>>> decimal.getcontext().prec
28
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.1428571428571428571428571429")
>>> decimal.getcontext().prec = 9
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.142857143")
The default action for error conditions is selectable; the module can
either return a special value such as infinity or not-a-number, or
exceptions can be raised:
>>> decimal.Decimal(1) / decimal.Decimal(0)
Traceback (most recent call last):
...
decimal.DivisionByZero: x / 0
>>> decimal.getcontext().traps[decimal.DivisionByZero] = False
>>> decimal.Decimal(1) / decimal.Decimal(0)
Decimal("Infinity")
>>>
The ‘Context’ instance also has various methods for formatting numbers
such as ‘to_eng_string()’ and ‘to_sci_string()’.
For more information, see the documentation for the *note decimal: 36.
module, which includes a quick-start tutorial and a reference.
See also
........
PEP 327(1) - Decimal Data Type
Written by Facundo Batista and implemented by Facundo Batista, Eric
Price, Raymond Hettinger, Aahz, and Tim Peters.
‘http://www.lahey.com/float.htm’
The article uses Fortran code to illustrate many of the problems
that floating-point inaccuracy can cause.
‘http://speleotrove.com/decimal/’
A description of a decimal-based representation. This
representation is being proposed as a standard, and underlies the
new Python decimal type. Much of this material was written by Mike
Cowlishaw, designer of the Rexx language.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0327
File: python.info, Node: PEP 328 Multi-line Imports, Next: PEP 331 Locale-Independent Float/String Conversions, Prev: PEP 327 Decimal Data Type, Up: What’s New in Python 2 4
1.13.9 PEP 328: Multi-line Imports
----------------------------------
One language change is a small syntactic tweak aimed at making it easier
to import many names from a module. In a ‘from module import names’
statement, `names' is a sequence of names separated by commas. If the
sequence is very long, you can either write multiple imports from the
same module, or you can use backslashes to escape the line endings like
this:
from SimpleXMLRPCServer import SimpleXMLRPCServer,\
SimpleXMLRPCRequestHandler,\
CGIXMLRPCRequestHandler,\
resolve_dotted_attribute
The syntactic change in Python 2.4 simply allows putting the names
within parentheses. Python ignores newlines within a parenthesized
expression, so the backslashes are no longer needed:
from SimpleXMLRPCServer import (SimpleXMLRPCServer,
SimpleXMLRPCRequestHandler,
CGIXMLRPCRequestHandler,
resolve_dotted_attribute)
The PEP also proposes that all *note import: c1d. statements be absolute
imports, with a leading ‘.’ character to indicate a relative import.
This part of the PEP was not implemented for Python 2.4, but was
completed for Python 2.5.
See also
........
PEP 328(1) - Imports: Multi-Line and Absolute/Relative
Written by Aahz. Multi-line imports were implemented by Dima
Dorfman.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0328
File: python.info, Node: PEP 331 Locale-Independent Float/String Conversions, Next: Other Language Changes<12>, Prev: PEP 328 Multi-line Imports, Up: What’s New in Python 2 4
1.13.10 PEP 331: Locale-Independent Float/String Conversions
------------------------------------------------------------
The *note locale: a9. modules lets Python software select various
conversions and display conventions that are localized to a particular
country or language. However, the module was careful to not change the
numeric locale because various functions in Python’s implementation
required that the numeric locale remain set to the ‘'C'’ locale. Often
this was because the code was using the C library’s ‘atof()’ function.
Not setting the numeric locale caused trouble for extensions that used
third-party C libraries, however, because they wouldn’t have the correct
locale set. The motivating example was GTK+, whose user interface
widgets weren’t displaying numbers in the current locale.
The solution described in the PEP is to add three new functions to the
Python API that perform ASCII-only conversions, ignoring the locale
setting:
* ‘PyOS_ascii_strtod(str, ptr)’ and ‘PyOS_ascii_atof(str, ptr)’ both
convert a string to a C ‘double’.
* ‘PyOS_ascii_formatd(buffer, buf_len, format, d)’ converts a
‘double’ to an ASCII string.
The code for these functions came from the GLib library
(‘https://developer.gnome.org/glib/stable/’), whose developers kindly
relicensed the relevant functions and donated them to the Python
Software Foundation. The *note locale: a9. module can now change the
numeric locale, letting extensions such as GTK+ produce the correct
results.
See also
........
PEP 331(1) - Locale-Independent Float/String Conversions
Written by Christian R. Reis, and implemented by Gustavo Carneiro.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0331
File: python.info, Node: Other Language Changes<12>, Next: New Improved and Deprecated Modules<3>, Prev: PEP 331 Locale-Independent Float/String Conversions, Up: What’s New in Python 2 4
1.13.11 Other Language Changes
------------------------------
Here are all of the changes that Python 2.4 makes to the core Python
language.
* Decorators for functions and methods were added ( PEP 318(1)).
* Built-in *note set(): b6f. and *note frozenset(): bee. types were
added ( PEP 218(2)). Other new built-ins include the
‘reversed(seq)’ function ( PEP 322(3)).
* Generator expressions were added ( PEP 289(4)).
* Certain numeric expressions no longer return values restricted to
32 or 64 bits ( PEP 237(5)).
* You can now put parentheses around the list of names in a ‘from
module import names’ statement ( PEP 328(6)).
* The *note dict.update(): dc9. method now accepts the same argument
forms as the *note dict: 1b8. constructor. This includes any
mapping, any iterable of key/value pairs, and keyword arguments.
(Contributed by Raymond Hettinger.)
* The string methods ‘ljust()’, ‘rjust()’, and ‘center()’ now take an
optional argument for specifying a fill character other than a
space. (Contributed by Raymond Hettinger.)
* Strings also gained an ‘rsplit()’ method that works like the
‘split()’ method but splits from the end of the string.
(Contributed by Sean Reifschneider.)
>>> 'www.python.org'.split('.', 1)
['www', 'python.org']
'www.python.org'.rsplit('.', 1)
['www.python', 'org']
* Three keyword parameters, `cmp', `key', and `reverse', were added
to the ‘sort()’ method of lists. These parameters make some common
usages of ‘sort()’ simpler. All of these parameters are optional.
For the `cmp' parameter, the value should be a comparison function
that takes two parameters and returns -1, 0, or +1 depending on how
the parameters compare. This function will then be used to sort
the list. Previously this was the only parameter that could be
provided to ‘sort()’.
`key' should be a single-parameter function that takes a list
element and returns a comparison key for the element. The list is
then sorted using the comparison keys. The following example sorts
a list case-insensitively:
>>> L = ['A', 'b', 'c', 'D']
>>> L.sort() # Case-sensitive sort
>>> L
['A', 'D', 'b', 'c']
>>> # Using 'key' parameter to sort list
>>> L.sort(key=lambda x: x.lower())
>>> L
['A', 'b', 'c', 'D']
>>> # Old-fashioned way
>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
>>> L
['A', 'b', 'c', 'D']
The last example, which uses the `cmp' parameter, is the old way to
perform a case-insensitive sort. It works but is slower than using
a `key' parameter. Using `key' calls ‘lower()’ method once for
each element in the list while using `cmp' will call it twice for
each comparison, so using `key' saves on invocations of the
‘lower()’ method.
For simple key functions and comparison functions, it is often
possible to avoid a *note lambda: c2f. expression by using an
unbound method instead. For example, the above case-insensitive
sort is best written as:
>>> L.sort(key=str.lower)
>>> L
['A', 'b', 'c', 'D']
Finally, the `reverse' parameter takes a Boolean value. If the
value is true, the list will be sorted into reverse order. Instead
of ‘L.sort(); L.reverse()’, you can now write
‘L.sort(reverse=True)’.
The results of sorting are now guaranteed to be stable. This means
that two entries with equal keys will be returned in the same order
as they were input. For example, you can sort a list of people by
name, and then sort the list by age, resulting in a list sorted by
age where people with the same age are in name-sorted order.
(All changes to ‘sort()’ contributed by Raymond Hettinger.)
* There is a new built-in function ‘sorted(iterable)’ that works like
the in-place *note list.sort(): 445. method but can be used in
expressions. The differences are:
* the input may be any iterable;
* a newly formed copy is sorted, leaving the original intact; and
* the expression returns the new sorted copy
>>> L = [9,7,8,3,2,4,1,6,5]
>>> [10+i for i in sorted(L)] # usable in a list comprehension
[11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> L # original is left unchanged
[9,7,8,3,2,4,1,6,5]
>>> sorted('Monty Python') # any iterable may be an input
[' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y']
>>> # List the contents of a dict sorted by key values
>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
>>> for k, v in sorted(colormap.iteritems()):
... print k, v
...
black 4
blue 2
green 3
red 1
yellow 5
(Contributed by Raymond Hettinger.)
* Integer operations will no longer trigger an ‘OverflowWarning’.
The ‘OverflowWarning’ warning will disappear in Python 2.5.
* The interpreter gained a new switch, *note -m: 337, that takes a
name, searches for the corresponding module on ‘sys.path’, and runs
the module as a script. For example, you can now run the Python
profiler with ‘python -m profile’. (Contributed by Nick Coghlan.)
* The ‘eval(expr, globals, locals)’ and ‘execfile(filename, globals,
locals)’ functions and the ‘exec’ statement now accept any mapping
type for the `locals' parameter. Previously this had to be a
regular Python dictionary. (Contributed by Raymond Hettinger.)
* The *note zip(): c32. built-in function and ‘itertools.izip()’ now
return an empty list if called with no arguments. Previously they
raised a *note TypeError: 192. exception. This makes them more
suitable for use with variable length argument lists:
>>> def transpose(array):
... return zip(*array)
...
>>> transpose([(1,2,3), (4,5,6)])
[(1, 4), (2, 5), (3, 6)]
>>> transpose([])
[]
(Contributed by Raymond Hettinger.)
* Encountering a failure while importing a module no longer leaves a
partially-initialized module object in ‘sys.modules’. The
incomplete module object left behind would fool further imports of
the same module into succeeding, leading to confusing errors.
(Fixed by Tim Peters.)
* *note None: 157. is now a constant; code that binds a new value to
the name ‘None’ is now a syntax error. (Contributed by Raymond
Hettinger.)
* Menu:
* Optimizations: Optimizations<11>.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0318
(2) https://www.python.org/dev/peps/pep-0218
(3) https://www.python.org/dev/peps/pep-0322
(4) https://www.python.org/dev/peps/pep-0289
(5) https://www.python.org/dev/peps/pep-0237
(6) https://www.python.org/dev/peps/pep-0328
File: python.info, Node: Optimizations<11>, Up: Other Language Changes<12>
1.13.11.1 Optimizations
.......................
* The inner loops for list and tuple slicing were optimized and now
run about one-third faster. The inner loops for dictionaries were
also optimized, resulting in performance boosts for ‘keys()’,
‘values()’, ‘items()’, ‘iterkeys()’, ‘itervalues()’, and
‘iteritems()’. (Contributed by Raymond Hettinger.)
* The machinery for growing and shrinking lists was optimized for
speed and for space efficiency. Appending and popping from lists
now runs faster due to more efficient code paths and less frequent
use of the underlying system ‘realloc()’. List comprehensions also
benefit. ‘list.extend()’ was also optimized and no longer converts
its argument into a temporary list before extending the base list.
(Contributed by Raymond Hettinger.)
* *note list(): 262, *note tuple(): 47e, *note map(): c2d, *note
filter(): c2e, and *note zip(): c32. now run several times faster
with non-sequence arguments that supply a *note __len__(): dcb.
method. (Contributed by Raymond Hettinger.)
* The methods ‘list.__getitem__()’, ‘dict.__getitem__()’, and
‘dict.__contains__()’ are now implemented as ‘method_descriptor’
objects rather than ‘wrapper_descriptor’ objects. This form of
access doubles their performance and makes them more suitable for
use as arguments to functionals: ‘map(mydict.__getitem__,
keylist)’. (Contributed by Raymond Hettinger.)
* Added a new opcode, ‘LIST_APPEND’, that simplifies the generated
bytecode for list comprehensions and speeds them up by about a
third. (Contributed by Raymond Hettinger.)
* The peephole bytecode optimizer has been improved to produce
shorter, faster bytecode; remarkably, the resulting bytecode is
more readable. (Enhanced by Raymond Hettinger.)
* String concatenations in statements of the form ‘s = s + "abc"’ and
‘s += "abc"’ are now performed more efficiently in certain
circumstances. This optimization won’t be present in other Python
implementations such as Jython, so you shouldn’t rely on it; using
the ‘join()’ method of strings is still recommended when you want
to efficiently glue a large number of strings together.
(Contributed by Armin Rigo.)
The net result of the 2.4 optimizations is that Python 2.4 runs the
pystone benchmark around 5% faster than Python 2.3 and 35% faster than
Python 2.2. (pystone is not a particularly good benchmark, but it’s the
most commonly used measurement of Python’s performance. Your own
applications may show greater or smaller benefits from Python 2.4.)
File: python.info, Node: New Improved and Deprecated Modules<3>, Next: Build and C API Changes<11>, Prev: Other Language Changes<12>, Up: What’s New in Python 2 4
1.13.12 New, Improved, and Deprecated Modules
---------------------------------------------
As usual, Python’s standard library received a number of enhancements
and bug fixes. Here’s a partial list of the most notable changes,
sorted alphabetically by module name. Consult the ‘Misc/NEWS’ file in
the source tree for a more complete list of changes, or look through the
CVS logs for all the details.
* The *note asyncore: b. module’s ‘loop()’ function now has a `count'
parameter that lets you perform a limited number of passes through
the polling loop. The default is still to loop forever.
* The *note base64: e. module now has more complete RFC 3548(1)
support for Base64, Base32, and Base16 encoding and decoding,
including optional case folding and optional alternative alphabets.
(Contributed by Barry Warsaw.)
* The *note bisect: 12. module now has an underlying C implementation
for improved performance. (Contributed by Dmitry Vasiliev.)
* The CJKCodecs collections of East Asian codecs, maintained by
Hye-Shik Chang, was integrated into 2.4. The new encodings are:
* Chinese (PRC): gb2312, gbk, gb18030, big5hkscs, hz
* Chinese (ROC): big5, cp950
*
Japanese: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,
iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext,
iso-2022-jp-2004, shift-jis, shift-jisx0213, shift-jis-2004
* Korean: cp949, euc-kr, johab, iso-2022-kr
* Some other new encodings were added: HP Roman8, ISO_8859-11,
ISO_8859-16, PCTP-154, and TIS-620.
* The UTF-8 and UTF-16 codecs now cope better with receiving partial
input. Previously the ‘StreamReader’ class would try to read more
data, making it impossible to resume decoding from the stream. The
‘read()’ method will now return as much data as it can and future
calls will resume decoding where previous ones left off.
(Implemented by Walter Dörwald.)
* There is a new *note collections: 1e. module for various
specialized collection datatypes. Currently it contains just one
type, ‘deque’, a double-ended queue that supports efficiently
adding and removing elements from either end:
>>> from collections import deque
>>> d = deque('ghi') # make a new deque with three items
>>> d.append('j') # add a new entry to the right side
>>> d.appendleft('f') # add a new entry to the left side
>>> d # show the representation of the deque
deque(['f', 'g', 'h', 'i', 'j'])
>>> d.pop() # return and remove the rightmost item
'j'
>>> d.popleft() # return and remove the leftmost item
'f'
>>> list(d) # list the contents of the deque
['g', 'h', 'i']
>>> 'h' in d # search the deque
True
Several modules, such as the ‘Queue’ and *note threading: 109.
modules, now take advantage of *note collections.deque: 531. for
improved performance. (Contributed by Raymond Hettinger.)
* The ‘ConfigParser’ classes have been enhanced slightly. The
‘read()’ method now returns a list of the files that were
successfully parsed, and the *note set(): b6f. method raises *note
TypeError: 192. if passed a `value' argument that isn’t a string.
(Contributed by John Belmonte and David Goodger.)
* The *note curses: 2c. module now supports the ncurses extension
‘use_default_colors()’. On platforms where the terminal supports
transparency, this makes it possible to use a transparent
background. (Contributed by Jörg Lehmann.)
* The *note difflib: 37. module now includes an ‘HtmlDiff’ class that
creates an HTML table showing a side by side comparison of two
versions of a text. (Contributed by Dan Gass.)
* The *note email: 69. package was updated to version 3.0, which
dropped various deprecated APIs and removes support for Python
versions earlier than 2.3. The 3.0 version of the package uses a
new incremental parser for MIME messages, available in the
‘email.FeedParser’ module. The new parser doesn’t require reading
the entire message into memory, and doesn’t raise exceptions if a
message is malformed; instead it records any problems in the
‘defect’ attribute of the message. (Developed by Anthony Baxter,
Barry Warsaw, Thomas Wouters, and others.)
* The *note heapq: 8e. module has been converted to C. The resulting
tenfold improvement in speed makes the module suitable for handling
high volumes of data. In addition, the module has two new
functions ‘nlargest()’ and ‘nsmallest()’ that use heaps to find the
N largest or smallest values in a dataset without the expense of a
full sort. (Contributed by Raymond Hettinger.)
* The ‘httplib’ module now contains constants for HTTP status codes
defined in various HTTP-related RFC documents. Constants have
names such as ‘OK’, ‘CREATED’, ‘CONTINUE’, and ‘MOVED_PERMANENTLY’;
use pydoc to get a full list. (Contributed by Andrew Eland.)
* The *note imaplib: 98. module now supports IMAP’s THREAD command
(contributed by Yves Dionne) and new ‘deleteacl()’ and ‘myrights()’
methods (contributed by Arnaud Mazin).
* The *note itertools: a3. module gained a ‘groupby(iterable[,
*func*])’ function. `iterable' is something that can be iterated
over to return a stream of elements, and the optional `func'
parameter is a function that takes an element and returns a key
value; if omitted, the key is simply the element itself.
‘groupby()’ then groups the elements into subsequences which have
matching values of the key, and returns a series of 2-tuples
containing the key value and an iterator over the subsequence.
Here’s an example to make this clearer. The `key' function simply
returns whether a number is even or odd, so the result of
‘groupby()’ is to return consecutive runs of odd or even numbers.
>>> import itertools
>>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14]
>>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
... print key_val, list(it)
...
0 [2, 4, 6]
1 [7]
0 [8]
1 [9, 11]
0 [12, 14]
>>>
‘groupby()’ is typically used with sorted input. The logic for
‘groupby()’ is similar to the Unix ‘uniq’ filter which makes it
handy for eliminating, counting, or identifying duplicate elements:
>>> word = 'abracadabra'
>>> letters = sorted(word) # Turn string into a sorted list of letters
>>> letters
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
>>> for k, g in itertools.groupby(letters):
... print k, list(g)
...
a ['a', 'a', 'a', 'a', 'a']
b ['b', 'b']
c ['c']
d ['d']
r ['r', 'r']
>>> # List unique letters
>>> [k for k, g in groupby(letters)]
['a', 'b', 'c', 'd', 'r']
>>> # Count letter occurrences
>>> [(k, len(list(g))) for k, g in groupby(letters)]
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
(Contributed by Hye-Shik Chang.)
* *note itertools: a3. also gained a function named ‘tee(iterator,
N)’ that returns `N' independent iterators that replicate
`iterator'. If `N' is omitted, the default is 2.
>>> L = [1,2,3]
>>> i1, i2 = itertools.tee(L)
>>> i1,i2
(, )
>>> list(i1) # Run the first iterator to exhaustion
[1, 2, 3]
>>> list(i2) # Run the second iterator to exhaustion
[1, 2, 3]
Note that ‘tee()’ has to keep copies of the values returned by the
iterator; in the worst case, it may need to keep all of them. This
should therefore be used carefully if the leading iterator can run
far ahead of the trailing iterator in a long stream of inputs. If
the separation is large, then you might as well use *note list():
262. instead. When the iterators track closely with one another,
‘tee()’ is ideal. Possible applications include bookmarking,
windowing, or lookahead iterators. (Contributed by Raymond
Hettinger.)
* A number of functions were added to the *note locale: a9. module,
such as ‘bind_textdomain_codeset()’ to specify a particular
encoding and a family of ‘l*gettext()’ functions that return
messages in the chosen encoding. (Contributed by Gustavo
Niemeyer.)
* Some keyword arguments were added to the *note logging: aa.
package’s ‘basicConfig()’ function to simplify log configuration.
The default behavior is to log messages to standard error, but
various keyword arguments can be specified to log to a particular
file, change the logging format, or set the logging level. For
example:
import logging
logging.basicConfig(filename='/var/log/application.log',
level=0, # Log all messages
format='%(levelname):%(process):%(thread):%(message)')
Other additions to the *note logging: aa. package include a
‘log(level, msg)’ convenience method, as well as a
‘TimedRotatingFileHandler’ class that rotates its log files at a
timed interval. The module already had ‘RotatingFileHandler’,
which rotated logs once the file exceeded a certain size. Both
classes derive from a new ‘BaseRotatingHandler’ class that can be
used to implement other rotating handlers.
(Changes implemented by Vinay Sajip.)
* The *note marshal: b0. module now shares interned strings on
unpacking a data structure. This may shrink the size of certain
pickle strings, but the primary effect is to make ‘.pyc’ files
significantly smaller. (Contributed by Martin von Löwis.)
* The *note nntplib: c0. module’s ‘NNTP’ class gained ‘description()’
and ‘descriptions()’ methods to retrieve newsgroup descriptions for
a single group or for a range of groups. (Contributed by Jürgen A.
Erhard.)
* Two new functions were added to the *note operator: c2. module,
‘attrgetter(attr)’ and ‘itemgetter(index)’. Both functions return
callables that take a single argument and return the corresponding
attribute or item; these callables make excellent data extractors
when used with *note map(): c2d. or *note sorted(): 444. For
example:
>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
>>> map(operator.itemgetter(0), L)
['c', 'd', 'a', 'b']
>>> map(operator.itemgetter(1), L)
[2, 1, 4, 3]
>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
(Contributed by Raymond Hettinger.)
* The *note optparse: c3. module was updated in various ways. The
module now passes its messages through *note gettext.gettext():
dcd, making it possible to internationalize Optik’s help and error
messages. Help messages for options can now include the string
‘'%default'’, which will be replaced by the option’s default value.
(Contributed by Greg Ward.)
* The long-term plan is to deprecate the ‘rfc822’ module in some
future Python release in favor of the *note email: 69. package. To
this end, the ‘email.Utils.formatdate()’ function has been changed
to make it usable as a replacement for ‘rfc822.formatdate()’. You
may want to write new e-mail processing code with this in mind.
(Change implemented by Anthony Baxter.)
* A new ‘urandom(n)’ function was added to the *note os: c4. module,
returning a string containing `n' bytes of random data. This
function provides access to platform-specific sources of randomness
such as ‘/dev/urandom’ on Linux or the Windows CryptoAPI.
(Contributed by Trevor Perrin.)
* Another new function: ‘os.path.lexists(path)’ returns true if the
file specified by `path' exists, whether or not it’s a symbolic
link. This differs from the existing ‘os.path.exists(path)’
function, which returns false if `path' is a symlink that points to
a destination that doesn’t exist. (Contributed by Beni
Cherniavsky.)
* A new ‘getsid()’ function was added to the *note posix: d1. module
that underlies the *note os: c4. module. (Contributed by J.
Raynor.)
* The *note poplib: d0. module now supports POP over SSL.
(Contributed by Hector Urtubia.)
* The *note profile: d3. module can now profile C extension
functions. (Contributed by Nick Bastin.)
* The *note random: dc. module has a new method called
‘getrandbits(N)’ that returns a long integer `N' bits in length.
The existing ‘randrange()’ method now uses ‘getrandbits()’ where
appropriate, making generation of arbitrarily large random numbers
more efficient. (Contributed by Raymond Hettinger.)
* The regular expression language accepted by the *note re: dd.
module was extended with simple conditional expressions, written as
‘(?(group)A|B)’. `group' is either a numeric group ID or a group
name defined with ‘(?P...)’ earlier in the expression. If
the specified group matched, the regular expression pattern `A'
will be tested against the string; if the group didn’t match, the
pattern `B' will be used instead. (Contributed by Gustavo
Niemeyer.)
* The *note re: dd. module is also no longer recursive, thanks to a
massive amount of work by Gustavo Niemeyer. In a recursive regular
expression engine, certain patterns result in a large amount of C
stack space being consumed, and it was possible to overflow the
stack. For example, if you matched a 30000-byte string of ‘a’
characters against the expression ‘(a|b)+’, one stack frame was
consumed per character. Python 2.3 tried to check for stack
overflow and raise a *note RuntimeError: 2ba. exception, but
certain patterns could sidestep the checking and if you were
unlucky Python could segfault. Python 2.4’s regular expression
engine can match this pattern without problems.
* The *note signal: ea. module now performs tighter error-checking on
the parameters to the *note signal.signal(): a7d. function. For
example, you can’t set a handler on the ‘SIGKILL’ signal; previous
versions of Python would quietly accept this, but 2.4 will raise a
*note RuntimeError: 2ba. exception.
* Two new functions were added to the *note socket: ef. module.
‘socketpair()’ returns a pair of connected sockets and
‘getservbyport(port)’ looks up the service name for a given port
number. (Contributed by Dave Cole and Barry Warsaw.)
* The ‘sys.exitfunc()’ function has been deprecated. Code should be
using the existing *note atexit: c. module, which correctly handles
calling multiple exit functions. Eventually ‘sys.exitfunc()’ will
become a purely internal interface, accessed only by *note atexit:
c.
* The *note tarfile: 101. module now generates GNU-format tar files
by default. (Contributed by Lars Gustäbel.)
* The *note threading: 109. module now has an elegantly simple way to
support thread-local data. The module contains a ‘local’ class
whose attribute values are local to different threads.
import threading
data = threading.local()
data.number = 42
data.url = ('www.python.org', 80)
Other threads can assign and retrieve their own values for the
‘number’ and ‘url’ attributes. You can subclass ‘local’ to
initialize attributes or to add methods. (Contributed by Jim
Fulton.)
* The *note timeit: 10b. module now automatically disables periodic
garbage collection during the timing loop. This change makes
consecutive timings more comparable. (Contributed by Raymond
Hettinger.)
* The *note weakref: 128. module now supports a wider variety of
objects including Python functions, class instances, sets,
frozensets, deques, arrays, files, sockets, and regular expression
pattern objects. (Contributed by Raymond Hettinger.)
* The ‘xmlrpclib’ module now supports a multi-call extension for
transmitting multiple XML-RPC calls in a single HTTP operation.
(Contributed by Brian Quinlan.)
* The ‘mpz’, ‘rotor’, and ‘xreadlines’ modules have been removed.
* Menu:
* cookielib::
* doctest: doctest<3>.
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc3548.html
File: python.info, Node: cookielib, Next: doctest<3>, Up: New Improved and Deprecated Modules<3>
1.13.12.1 cookielib
...................
The ‘cookielib’ library supports client-side handling for HTTP cookies,
mirroring the ‘Cookie’ module’s server-side cookie support. Cookies are
stored in cookie jars; the library transparently stores cookies offered
by the web server in the cookie jar, and fetches the cookie from the jar
when connecting to the server. As in web browsers, policy objects
control whether cookies are accepted or not.
In order to store cookies across sessions, two implementations of cookie
jars are provided: one that stores cookies in the Netscape format so
applications can use the Mozilla or Lynx cookie files, and one that
stores cookies in the same format as the Perl libwww library.
‘urllib2’ has been changed to interact with ‘cookielib’:
‘HTTPCookieProcessor’ manages a cookie jar that is used when accessing
URLs.
This module was contributed by John J. Lee.
File: python.info, Node: doctest<3>, Prev: cookielib, Up: New Improved and Deprecated Modules<3>
1.13.12.2 doctest
.................
The *note doctest: 67. module underwent considerable refactoring thanks
to Edward Loper and Tim Peters. Testing can still be as simple as
running *note doctest.testmod(): dd0, but the refactorings allow
customizing the module’s operation in various ways
The new ‘DocTestFinder’ class extracts the tests from a given object’s
docstrings:
def f (x, y):
""">>> f(2,2)
4
>>> f(3,2)
6
"""
return x*y
finder = doctest.DocTestFinder()
# Get list of DocTest instances
tests = finder.find(f)
The new ‘DocTestRunner’ class then runs individual tests and can produce
a summary of the results:
runner = doctest.DocTestRunner()
for t in tests:
tried, failed = runner.run(t)
runner.summarize(verbose=1)
The above example produces the following output:
1 items passed all tests:
2 tests in f
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
‘DocTestRunner’ uses an instance of the ‘OutputChecker’ class to compare
the expected output with the actual output. This class takes a number
of different flags that customize its behaviour; ambitious users can
also write a completely new subclass of ‘OutputChecker’.
The default output checker provides a number of handy features. For
example, with the *note doctest.ELLIPSIS: dd1. option flag, an ellipsis
(‘...’) in the expected output matches any substring, making it easier
to accommodate outputs that vary in minor ways:
def o (n):
""">>> o(1)
<__main__.C instance at 0x...>
>>>
"""
Another special string, ‘’, matches a blank line:
def p (n):
""">>> p(1)
>>>
"""
Another new capability is producing a diff-style display of the output
by specifying the *note doctest.REPORT_UDIFF: dd2. (unified diffs),
*note doctest.REPORT_CDIFF: dd3. (context diffs), or *note
doctest.REPORT_NDIFF: dd4. (delta-style) option flags. For example:
def g (n):
""">>> g(4)
here
is
a
lengthy
>>>"""
L = 'here is a rather lengthy list of words'.split()
for word in L[:n]:
print word
Running the above function’s tests with *note doctest.REPORT_UDIFF: dd2.
specified, you get the following output:
**********************************************************************
File "t.py", line 15, in g
Failed example:
g(4)
Differences (unified diff with -expected +actual):
@@ -2,3 +2,3 @@
is
a
-lengthy
+rather
**********************************************************************
File: python.info, Node: Build and C API Changes<11>, Next: Porting to Python 2 4, Prev: New Improved and Deprecated Modules<3>, Up: What’s New in Python 2 4
1.13.13 Build and C API Changes
-------------------------------
Some of the changes to Python’s build process and to the C API are:
* Three new convenience macros were added for common return values
from extension functions: *note Py_RETURN_NONE: dd6, *note
Py_RETURN_TRUE: dd7, and *note Py_RETURN_FALSE: dd8. (Contributed
by Brett Cannon.)
* Another new macro, ‘Py_CLEAR(obj)’, decreases the reference count
of `obj' and sets `obj' to the null pointer. (Contributed by Jim
Fulton.)
* A new function, ‘PyTuple_Pack(N, obj1, obj2, ..., objN)’,
constructs tuples from a variable length argument list of Python
objects. (Contributed by Raymond Hettinger.)
* A new function, ‘PyDict_Contains(d, k)’, implements fast dictionary
lookups without masking exceptions raised during the look-up
process. (Contributed by Raymond Hettinger.)
* The ‘Py_IS_NAN(X)’ macro returns 1 if its float or double argument
`X' is a NaN. (Contributed by Tim Peters.)
* C code can avoid unnecessary locking by using the new *note
PyEval_ThreadsInitialized(): dd9. function to tell if any thread
operations have been performed. If this function returns false, no
lock operations are needed. (Contributed by Nick Coghlan.)
* A new function, *note PyArg_VaParseTupleAndKeywords(): dda, is the
same as *note PyArg_ParseTupleAndKeywords(): 5ca. but takes a
‘va_list’ instead of a number of arguments. (Contributed by Greg
Chapman.)
* A new method flag, ‘METH_COEXISTS’, allows a function defined in
slots to co-exist with a *note PyCFunction: ddb. having the same
name. This can halve the access time for a method such as
‘set.__contains__()’. (Contributed by Raymond Hettinger.)
* Python can now be built with additional profiling for the
interpreter itself, intended as an aid to people developing the
Python core. Providing ‘--enable-profiling’ to the ‘configure’
script will let you profile the interpreter with ‘gprof’, and
providing the ‘--with-tsc’ switch enables profiling using the
Pentium’s Time-Stamp-Counter register. Note that the ‘--with-tsc’
switch is slightly misnamed, because the profiling feature also
works on the PowerPC platform, though that processor architecture
doesn’t call that register “the TSC register”. (Contributed by
Jeremy Hylton.)
* The ‘tracebackobject’ type has been renamed to ‘PyTracebackObject’.
* Menu:
* Port-Specific Changes: Port-Specific Changes<2>.
File: python.info, Node: Port-Specific Changes<2>, Up: Build and C API Changes<11>
1.13.13.1 Port-Specific Changes
...............................
* The Windows port now builds under MSVC++ 7.1 as well as version 6.
(Contributed by Martin von Löwis.)
File: python.info, Node: Porting to Python 2 4, Next: Acknowledgements<4>, Prev: Build and C API Changes<11>, Up: What’s New in Python 2 4
1.13.14 Porting to Python 2.4
-----------------------------
This section lists previously described changes that may require changes
to your code:
* Left shifts and hexadecimal/octal constants that are too large no
longer trigger a *note FutureWarning: 325. and return a value
limited to 32 or 64 bits; instead they return a long integer.
* Integer operations will no longer trigger an ‘OverflowWarning’.
The ‘OverflowWarning’ warning will disappear in Python 2.5.
* The *note zip(): c32. built-in function and ‘itertools.izip()’ now
return an empty list instead of raising a *note TypeError: 192.
exception if called with no arguments.
* You can no longer compare the ‘date’ and *note datetime: 194.
instances provided by the *note datetime: 31. module. Two
instances of different classes will now always be unequal, and
relative comparisons (‘<’, ‘>’) will raise a *note TypeError: 192.
* ‘dircache.listdir()’ now passes exceptions to the caller instead of
returning empty lists.
* ‘LexicalHandler.startDTD()’ used to receive the public and system
IDs in the wrong order. This has been corrected; applications
relying on the wrong order need to be fixed.
* *note fcntl.ioctl(): dde. now warns if the `mutate' argument is
omitted and relevant.
* The *note tarfile: 101. module now generates GNU-format tar files
by default.
* Encountering a failure while importing a module no longer leaves a
partially-initialized module object in ‘sys.modules’.
* *note None: 157. is now a constant; code that binds a new value to
the name ‘None’ is now a syntax error.
* The ‘signals.signal()’ function now raises a *note RuntimeError:
2ba. exception for certain illegal values; previously these errors
would pass silently. For example, you can no longer set a handler
on the ‘SIGKILL’ signal.
File: python.info, Node: Acknowledgements<4>, Prev: Porting to Python 2 4, Up: What’s New in Python 2 4
1.13.15 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Koray Can, Hye-Shik Chang, Michael Dyck, Raymond Hettinger,
Brian Hurt, Hamish Lawson, Fredrik Lundh, Sean Reifschneider, Sadruddin
Rejeb.
File: python.info, Node: What’s New in Python 2 3, Next: What’s New in Python 2 2, Prev: What’s New in Python 2 4, Up: What’s New in Python
1.14 What’s New in Python 2.3
=============================
Author: A.M. Kuchling
This article explains the new features in Python 2.3. Python 2.3 was
released on July 29, 2003.
The main themes for Python 2.3 are polishing some of the features added
in 2.2, adding various small but useful enhancements to the core
language, and expanding the standard library. The new object model
introduced in the previous version has benefited from 18 months of
bugfixes and from optimization efforts that have improved the
performance of new-style classes. A few new built-in functions have
been added such as *note sum(): 1e5. and *note enumerate(): de3. The
*note in: 7a3. operator can now be used for substring searches (e.g.
‘"ab" in "abc"’ returns *note True: 499.).
Some of the many new library features include Boolean, set, heap, and
date/time data types, the ability to import modules from ZIP-format
archives, metadata support for the long-awaited Python catalog, an
updated version of IDLE, and modules for logging messages, wrapping
text, parsing CSV files, processing command-line options, using
BerkeleyDB databases… the list of new and enhanced modules is lengthy.
This article doesn’t attempt to provide a complete specification of the
new features, but instead provides a convenient overview. For full
details, you should refer to the documentation for Python 2.3, such as
the Python Library Reference and the Python Reference Manual. If you
want to understand the complete implementation and design rationale,
refer to the PEP for a particular new feature.
* Menu:
* PEP 218; A Standard Set Datatype: PEP 218 A Standard Set Datatype.
* PEP 255; Simple Generators: PEP 255 Simple Generators.
* PEP 263; Source Code Encodings: PEP 263 Source Code Encodings.
* PEP 273; Importing Modules from ZIP Archives: PEP 273 Importing Modules from ZIP Archives.
* PEP 277; Unicode file name support for Windows NT: PEP 277 Unicode file name support for Windows NT.
* PEP 278; Universal Newline Support: PEP 278 Universal Newline Support.
* PEP 279; enumerate(): PEP 279 enumerate.
* PEP 282; The logging Package: PEP 282 The logging Package.
* PEP 285; A Boolean Type: PEP 285 A Boolean Type.
* PEP 293; Codec Error Handling Callbacks: PEP 293 Codec Error Handling Callbacks.
* PEP 301; Package Index and Metadata for Distutils: PEP 301 Package Index and Metadata for Distutils.
* PEP 302; New Import Hooks: PEP 302 New Import Hooks.
* PEP 305; Comma-separated Files: PEP 305 Comma-separated Files.
* PEP 307; Pickle Enhancements: PEP 307 Pickle Enhancements.
* Extended Slices::
* Other Language Changes: Other Language Changes<13>.
* New, Improved, and Deprecated Modules: New Improved and Deprecated Modules<4>.
* Pymalloc; A Specialized Object Allocator: Pymalloc A Specialized Object Allocator.
* Build and C API Changes: Build and C API Changes<12>.
* Other Changes and Fixes: Other Changes and Fixes<2>.
* Porting to Python 2.3: Porting to Python 2 3.
* Acknowledgements: Acknowledgements<5>.
File: python.info, Node: PEP 218 A Standard Set Datatype, Next: PEP 255 Simple Generators, Up: What’s New in Python 2 3
1.14.1 PEP 218: A Standard Set Datatype
---------------------------------------
The new ‘sets’ module contains an implementation of a set datatype. The
‘Set’ class is for mutable sets, sets that can have members added and
removed. The ‘ImmutableSet’ class is for sets that can’t be modified,
and instances of ‘ImmutableSet’ can therefore be used as dictionary
keys. Sets are built on top of dictionaries, so the elements within a
set must be hashable.
Here’s a simple example:
>>> import sets
>>> S = sets.Set([1,2,3])
>>> S
Set([1, 2, 3])
>>> 1 in S
True
>>> 0 in S
False
>>> S.add(5)
>>> S.remove(3)
>>> S
Set([1, 2, 5])
>>>
The union and intersection of sets can be computed with the ‘union()’
and ‘intersection()’ methods; an alternative notation uses the bitwise
operators ‘&’ and ‘|’. Mutable sets also have in-place versions of
these methods, ‘union_update()’ and ‘intersection_update()’.
>>> S1 = sets.Set([1,2,3])
>>> S2 = sets.Set([4,5,6])
>>> S1.union(S2)
Set([1, 2, 3, 4, 5, 6])
>>> S1 | S2 # Alternative notation
Set([1, 2, 3, 4, 5, 6])
>>> S1.intersection(S2)
Set([])
>>> S1 & S2 # Alternative notation
Set([])
>>> S1.union_update(S2)
>>> S1
Set([1, 2, 3, 4, 5, 6])
>>>
It’s also possible to take the symmetric difference of two sets. This
is the set of all elements in the union that aren’t in the intersection.
Another way of putting it is that the symmetric difference contains all
elements that are in exactly one set. Again, there’s an alternative
notation (‘^’), and an in-place version with the ungainly name
‘symmetric_difference_update()’.
>>> S1 = sets.Set([1,2,3,4])
>>> S2 = sets.Set([3,4,5,6])
>>> S1.symmetric_difference(S2)
Set([1, 2, 5, 6])
>>> S1 ^ S2
Set([1, 2, 5, 6])
>>>
There are also ‘issubset()’ and ‘issuperset()’ methods for checking
whether one set is a subset or superset of another:
>>> S1 = sets.Set([1,2,3])
>>> S2 = sets.Set([2,3])
>>> S2.issubset(S1)
True
>>> S1.issubset(S2)
False
>>> S1.issuperset(S2)
True
>>>
See also
........
PEP 218(1) - Adding a Built-In Set Object Type
PEP written by Greg V. Wilson. Implemented by Greg V. Wilson, Alex
Martelli, and GvR.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0218
File: python.info, Node: PEP 255 Simple Generators, Next: PEP 263 Source Code Encodings, Prev: PEP 218 A Standard Set Datatype, Up: What’s New in Python 2 3
1.14.2 PEP 255: Simple Generators
---------------------------------
In Python 2.2, generators were added as an optional feature, to be
enabled by a ‘from __future__ import generators’ directive. In 2.3
generators no longer need to be specially enabled, and are now always
present; this means that *note yield: 18f. is now always a keyword. The
rest of this section is a copy of the description of generators from the
“What’s New in Python 2.2” document; if you read it back when Python 2.2
came out, you can skip the rest of this section.
You’re doubtless familiar with how function calls work in Python or C.
When you call a function, it gets a private namespace where its local
variables are created. When the function reaches a *note return: 190.
statement, the local variables are destroyed and the resulting value is
returned to the caller. A later call to the same function will get a
fresh new set of local variables. But, what if the local variables
weren’t thrown away on exiting a function? What if you could later
resume the function where it left off? This is what generators provide;
they can be thought of as resumable functions.
Here’s the simplest example of a generator function:
def generate_ints(N):
for i in range(N):
yield i
A new keyword, *note yield: 18f, was introduced for generators. Any
function containing a ‘yield’ statement is a generator function; this is
detected by Python’s bytecode compiler which compiles the function
specially as a result.
When you call a generator function, it doesn’t return a single value;
instead it returns a generator object that supports the iterator
protocol. On executing the *note yield: 18f. statement, the generator
outputs the value of ‘i’, similar to a *note return: 190. statement.
The big difference between ‘yield’ and a ‘return’ statement is that on
reaching a ‘yield’ the generator’s state of execution is suspended and
local variables are preserved. On the next call to the generator’s
‘.next()’ method, the function will resume executing immediately after
the ‘yield’ statement. (For complicated reasons, the ‘yield’ statement
isn’t allowed inside the *note try: d72. block of a ‘try’…‘finally’
statement; read PEP 255(1) for a full explanation of the interaction
between ‘yield’ and exceptions.)
Here’s a sample usage of the ‘generate_ints()’ generator:
>>> gen = generate_ints(3)
>>> gen
>>> gen.next()
0
>>> gen.next()
1
>>> gen.next()
2
>>> gen.next()
Traceback (most recent call last):
File "stdin", line 1, in ?
File "stdin", line 2, in generate_ints
StopIteration
You could equally write ‘for i in generate_ints(5)’, or ‘a,b,c =
generate_ints(3)’.
Inside a generator function, the *note return: 190. statement can only
be used without a value, and signals the end of the procession of
values; afterwards the generator cannot return any further values.
‘return’ with a value, such as ‘return 5’, is a syntax error inside a
generator function. The end of the generator’s results can also be
indicated by raising *note StopIteration: 486. manually, or by just
letting the flow of execution fall off the bottom of the function.
You could achieve the effect of generators manually by writing your own
class and storing all the local variables of the generator as instance
variables. For example, returning a list of integers could be done by
setting ‘self.count’ to 0, and having the *note next(): 682. method
increment ‘self.count’ and return it. However, for a moderately
complicated generator, writing a corresponding class would be much
messier. ‘Lib/test/test_generators.py’ contains a number of more
interesting examples. The simplest one implements an in-order traversal
of a tree using generators recursively.
# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
if t:
for x in inorder(t.left):
yield x
yield t.label
for x in inorder(t.right):
yield x
Two other examples in ‘Lib/test/test_generators.py’ produce solutions
for the N-Queens problem (placing $N$ queens on an $NxN$ chess board so
that no queen threatens another) and the Knight’s Tour (a route that
takes a knight to every square of an $NxN$ chessboard without visiting
any square twice).
The idea of generators comes from other programming languages,
especially Icon (‘https://www.cs.arizona.edu/icon/’), where the idea of
generators is central. In Icon, every expression and function call
behaves like a generator. One example from “An Overview of the Icon
Programming Language” at
‘https://www.cs.arizona.edu/icon/docs/ipd266.htm’ gives an idea of what
this looks like:
sentence := "Store it in the neighboring harbor"
if (i := find("or", sentence)) > 5 then write(i)
In Icon the ‘find()’ function returns the indexes at which the substring
“or” is found: 3, 23, 33. In the *note if: de7. statement, ‘i’ is first
assigned a value of 3, but 3 is less than 5, so the comparison fails,
and Icon retries it with the second value of 23. 23 is greater than 5,
so the comparison now succeeds, and the code prints the value 23 to the
screen.
Python doesn’t go nearly as far as Icon in adopting generators as a
central concept. Generators are considered part of the core Python
language, but learning or using them isn’t compulsory; if they don’t
solve any problems that you have, feel free to ignore them. One novel
feature of Python’s interface as compared to Icon’s is that a
generator’s state is represented as a concrete object (the iterator)
that can be passed around to other functions or stored in a data
structure.
See also
........
PEP 255(2) - Simple Generators
Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland.
Implemented mostly by Neil Schemenauer and Tim Peters, with other
fixes from the Python Labs crew.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0255
(2) https://www.python.org/dev/peps/pep-0255
File: python.info, Node: PEP 263 Source Code Encodings, Next: PEP 273 Importing Modules from ZIP Archives, Prev: PEP 255 Simple Generators, Up: What’s New in Python 2 3
1.14.3 PEP 263: Source Code Encodings
-------------------------------------
Python source files can now be declared as being in different character
set encodings. Encodings are declared by including a specially
formatted comment in the first or second line of the source file. For
example, a UTF-8 file can be declared with:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
Without such an encoding declaration, the default encoding used is 7-bit
ASCII. Executing or importing modules that contain string literals with
8-bit characters and have no encoding declaration will result in a *note
DeprecationWarning: 278. being signalled by Python 2.3; in 2.4 this will
be a syntax error.
The encoding declaration only affects Unicode string literals, which
will be converted to Unicode using the specified encoding. Note that
Python identifiers are still restricted to ASCII characters, so you
can’t have variable names that use characters outside of the usual
alphanumerics.
See also
........
PEP 263(1) - Defining Python Source Code Encodings
Written by Marc-André Lemburg and Martin von Löwis; implemented by
Suzuki Hisao and Martin von Löwis.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0263
File: python.info, Node: PEP 273 Importing Modules from ZIP Archives, Next: PEP 277 Unicode file name support for Windows NT, Prev: PEP 263 Source Code Encodings, Up: What’s New in Python 2 3
1.14.4 PEP 273: Importing Modules from ZIP Archives
---------------------------------------------------
The new *note zipimport: 143. module adds support for importing modules
from a ZIP-format archive. You don’t need to import the module
explicitly; it will be automatically imported if a ZIP archive’s
filename is added to ‘sys.path’. For example:
amk@nyman:~/src/python$ unzip -l /tmp/example.zip
Archive: /tmp/example.zip
Length Date Time Name
-------- ---- ---- ----
8467 11-26-02 22:30 jwzthreading.py
-------- -------
8467 1 file
amk@nyman:~/src/python$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
>>> import sys
>>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
>>> import jwzthreading
>>> jwzthreading.__file__
'/tmp/example.zip/jwzthreading.py'
>>>
An entry in ‘sys.path’ can now be the filename of a ZIP archive. The
ZIP archive can contain any kind of files, but only files named ‘*.py’,
‘*.pyc’, or ‘*.pyo’ can be imported. If an archive only contains ‘*.py’
files, Python will not attempt to modify the archive by adding the
corresponding ‘*.pyc’ file, meaning that if a ZIP archive doesn’t
contain ‘*.pyc’ files, importing may be rather slow.
A path within the archive can also be specified to only import from a
subdirectory; for example, the path ‘/tmp/example.zip/lib/’ would only
import from the ‘lib/’ subdirectory within the archive.
See also
........
PEP 273(1) - Import Modules from Zip Archives
Written by James C. Ahlstrom, who also provided an implementation.
Python 2.3 follows the specification in PEP 273(2), but uses an
implementation written by Just van Rossum that uses the import
hooks described in PEP 302(3). See section *note PEP 302; New
Import Hooks: deb. for a description of the new import hooks.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0273
(2) https://www.python.org/dev/peps/pep-0273
(3) https://www.python.org/dev/peps/pep-0302
File: python.info, Node: PEP 277 Unicode file name support for Windows NT, Next: PEP 278 Universal Newline Support, Prev: PEP 273 Importing Modules from ZIP Archives, Up: What’s New in Python 2 3
1.14.5 PEP 277: Unicode file name support for Windows NT
--------------------------------------------------------
On Windows NT, 2000, and XP, the system stores file names as Unicode
strings. Traditionally, Python has represented file names as byte
strings, which is inadequate because it renders some file names
inaccessible.
Python now allows using arbitrary Unicode strings (within the
limitations of the file system) for all functions that expect file
names, most notably the *note open(): 4f0. built-in function. If a
Unicode string is passed to *note os.listdir(): a41, Python now returns
a list of Unicode strings. A new function, ‘os.getcwdu()’, returns the
current directory as a Unicode string.
Byte strings still work as file names, and on Windows Python will
transparently convert them to Unicode using the ‘mbcs’ encoding.
Other systems also allow Unicode strings as file names but convert them
to byte strings before passing them to the system, which can cause a
*note UnicodeError: c3b. to be raised. Applications can test whether
arbitrary Unicode strings are supported as file names by checking *note
os.path.supports_unicode_filenames: ded, a Boolean value.
Under MacOS, *note os.listdir(): a41. may now return Unicode filenames.
See also
........
PEP 277(1) - Unicode file name support for Windows NT
Written by Neil Hodgson; implemented by Neil Hodgson, Martin von
Löwis, and Mark Hammond.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0277
File: python.info, Node: PEP 278 Universal Newline Support, Next: PEP 279 enumerate, Prev: PEP 277 Unicode file name support for Windows NT, Up: What’s New in Python 2 3
1.14.6 PEP 278: Universal Newline Support
-----------------------------------------
The three major operating systems used today are Microsoft Windows,
Apple’s Macintosh OS, and the various Unix derivatives. A minor
irritation of cross-platform work is that these three platforms all use
different characters to mark the ends of lines in text files. Unix uses
the linefeed (ASCII character 10), MacOS uses the carriage return (ASCII
character 13), and Windows uses a two-character sequence of a carriage
return plus a newline.
Python’s file objects can now support end of line conventions other than
the one followed by the platform on which Python is running. Opening a
file with the mode ‘'U'’ or ‘'rU'’ will open a file for reading in *note
universal newlines: 5f4. mode. All three line ending conventions will
be translated to a ‘'\n'’ in the strings returned by the various file
methods such as ‘read()’ and *note readline(): de.
Universal newline support is also used when importing modules and when
executing a file with the ‘execfile()’ function. This means that Python
modules can be shared between all three operating systems without
needing to convert the line-endings.
This feature can be disabled when compiling Python by specifying the
‘--without-universal-newlines’ switch when running Python’s ‘configure’
script.
See also
........
PEP 278(1) - Universal Newline Support
Written and implemented by Jack Jansen.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0278
File: python.info, Node: PEP 279 enumerate, Next: PEP 282 The logging Package, Prev: PEP 278 Universal Newline Support, Up: What’s New in Python 2 3
1.14.7 PEP 279: enumerate()
---------------------------
A new built-in function, *note enumerate(): de3, will make certain loops
a bit clearer. ‘enumerate(thing)’, where `thing' is either an iterator
or a sequence, returns an iterator that will return ‘(0, thing[0])’,
‘(1, thing[1])’, ‘(2, thing[2])’, and so forth.
A common idiom to change every element of a list looks like this:
for i in range(len(L)):
item = L[i]
# ... compute some result based on item ...
L[i] = result
This can be rewritten using *note enumerate(): de3. as:
for i, item in enumerate(L):
# ... compute some result based on item ...
L[i] = result
See also
........
PEP 279(1) - The enumerate() built-in function
Written and implemented by Raymond D. Hettinger.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0279
File: python.info, Node: PEP 282 The logging Package, Next: PEP 285 A Boolean Type, Prev: PEP 279 enumerate, Up: What’s New in Python 2 3
1.14.8 PEP 282: The logging Package
-----------------------------------
A standard package for writing logs, *note logging: aa, has been added
to Python 2.3. It provides a powerful and flexible mechanism for
generating logging output which can then be filtered and processed in
various ways. A configuration file written in a standard format can be
used to control the logging behavior of a program. Python includes
handlers that will write log records to standard error or to a file or
socket, send them to the system log, or even e-mail them to a particular
address; of course, it’s also possible to write your own handler
classes.
The ‘Logger’ class is the primary class. Most application code will
deal with one or more ‘Logger’ objects, each one used by a particular
subsystem of the application. Each ‘Logger’ is identified by a name,
and names are organized into a hierarchy using ‘.’ as the component
separator. For example, you might have ‘Logger’ instances named
‘server’, ‘server.auth’ and ‘server.network’. The latter two instances
are below ‘server’ in the hierarchy. This means that if you turn up the
verbosity for ‘server’ or direct ‘server’ messages to a different
handler, the changes will also apply to records logged to ‘server.auth’
and ‘server.network’. There’s also a root ‘Logger’ that’s the parent of
all other loggers.
For simple uses, the *note logging: aa. package contains some
convenience functions that always use the root log:
import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')
This produces the following output:
WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down
In the default configuration, informational and debugging messages are
suppressed and the output is sent to standard error. You can enable the
display of informational and debugging messages by calling the
‘setLevel()’ method on the root logger.
Notice the ‘warning()’ call’s use of string formatting operators; all of
the functions for logging messages take the arguments ‘(msg, arg1, arg2,
...)’ and log the string resulting from ‘msg % (arg1, arg2, ...)’.
There’s also an ‘exception()’ function that records the most recent
traceback. Any of the other functions will also record the traceback if
you specify a true value for the keyword argument `exc_info'.
def f():
try: 1/0
except: logging.exception('Problem recorded')
f()
This produces the following output:
ERROR:root:Problem recorded
Traceback (most recent call last):
File "t.py", line 6, in f
1/0
ZeroDivisionError: integer division or modulo by zero
Slightly more advanced programs will use a logger other than the root
logger. The ‘getLogger(name)’ function is used to get a particular log,
creating it if it doesn’t exist yet. ‘getLogger(None)’ returns the root
logger.
log = logging.getLogger('server')
...
log.info('Listening on port %i', port)
...
log.critical('Disk full')
...
Log records are usually propagated up the hierarchy, so a message logged
to ‘server.auth’ is also seen by ‘server’ and ‘root’, but a ‘Logger’ can
prevent this by setting its ‘propagate’ attribute to *note False: 388.
There are more classes provided by the *note logging: aa. package that
can be customized. When a ‘Logger’ instance is told to log a message,
it creates a ‘LogRecord’ instance that is sent to any number of
different ‘Handler’ instances. Loggers and handlers can also have an
attached list of filters, and each filter can cause the ‘LogRecord’ to
be ignored or can modify the record before passing it along. When
they’re finally output, ‘LogRecord’ instances are converted to text by a
‘Formatter’ class. All of these classes can be replaced by your own
specially-written classes.
With all of these features the *note logging: aa. package should provide
enough flexibility for even the most complicated applications. This is
only an incomplete overview of its features, so please see the package’s
reference documentation for all of the details. Reading PEP 282(1) will
also be helpful.
See also
........
PEP 282(2) - A Logging System
Written by Vinay Sajip and Trent Mick; implemented by Vinay Sajip.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0282
(2) https://www.python.org/dev/peps/pep-0282
File: python.info, Node: PEP 285 A Boolean Type, Next: PEP 293 Codec Error Handling Callbacks, Prev: PEP 282 The logging Package, Up: What’s New in Python 2 3
1.14.9 PEP 285: A Boolean Type
------------------------------
A Boolean type was added to Python 2.3. Two new constants were added to
the ‘__builtin__’ module, *note True: 499. and *note False: 388. (*note
True: 499. and *note False: 388. constants were added to the built-ins
in Python 2.2.1, but the 2.2.1 versions are simply set to integer values
of 1 and 0 and aren’t a different type.)
The type object for this new type is named *note bool: 183.; the
constructor for it takes any Python value and converts it to *note True:
499. or *note False: 388.
>>> bool(1)
True
>>> bool(0)
False
>>> bool([])
False
>>> bool( (1,) )
True
Most of the standard library modules and built-in functions have been
changed to return Booleans.
>>> obj = []
>>> hasattr(obj, 'append')
True
>>> isinstance(obj, list)
True
>>> isinstance(obj, tuple)
False
Python’s Booleans were added with the primary goal of making code
clearer. For example, if you’re reading a function and encounter the
statement ‘return 1’, you might wonder whether the ‘1’ represents a
Boolean truth value, an index, or a coefficient that multiplies some
other quantity. If the statement is ‘return True’, however, the meaning
of the return value is quite clear.
Python’s Booleans were `not' added for the sake of strict type-checking.
A very strict language such as Pascal would also prevent you performing
arithmetic with Booleans, and would require that the expression in an
*note if: de7. statement always evaluate to a Boolean result. Python is
not this strict and never will be, as PEP 285(1) explicitly says. This
means you can still use any expression in an ‘if’ statement, even ones
that evaluate to a list or tuple or some random object. The Boolean
type is a subclass of the *note int: 184. class so that arithmetic using
a Boolean still works.
>>> True + 1
2
>>> False + 1
1
>>> False * 75
0
>>> True * 75
75
To sum up *note True: 499. and *note False: 388. in a sentence: they’re
alternative ways to spell the integer values 1 and 0, with the single
difference that *note str(): 330. and *note repr(): 7cc. return the
strings ‘'True'’ and ‘'False'’ instead of ‘'1'’ and ‘'0'’.
See also
........
PEP 285(2) - Adding a bool type
Written and implemented by GvR.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0285
(2) https://www.python.org/dev/peps/pep-0285
File: python.info, Node: PEP 293 Codec Error Handling Callbacks, Next: PEP 301 Package Index and Metadata for Distutils, Prev: PEP 285 A Boolean Type, Up: What’s New in Python 2 3
1.14.10 PEP 293: Codec Error Handling Callbacks
-----------------------------------------------
When encoding a Unicode string into a byte string, unencodable
characters may be encountered. So far, Python has allowed specifying
the error processing as either “strict” (raising *note UnicodeError:
c3b.), “ignore” (skipping the character), or “replace” (using a question
mark in the output string), with “strict” being the default behavior.
It may be desirable to specify alternative processing of such errors,
such as inserting an XML character reference or HTML entity reference
into the converted string.
Python now has a flexible framework to add different processing
strategies. New error handlers can be added with *note
codecs.register_error(): df5, and codecs then can access the error
handler with *note codecs.lookup_error(): df6. An equivalent C API has
been added for codecs written in C. The error handler gets the necessary
state information such as the string being converted, the position in
the string where the error was detected, and the target encoding. The
handler can then either raise an exception or return a replacement
string.
Two additional error handlers have been implemented using this
framework: “backslashreplace” uses Python backslash quoting to represent
unencodable characters and “xmlcharrefreplace” emits XML character
references.
See also
........
PEP 293(1) - Codec Error Handling Callbacks
Written and implemented by Walter Dörwald.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0293
File: python.info, Node: PEP 301 Package Index and Metadata for Distutils, Next: PEP 302 New Import Hooks, Prev: PEP 293 Codec Error Handling Callbacks, Up: What’s New in Python 2 3
1.14.11 PEP 301: Package Index and Metadata for Distutils
---------------------------------------------------------
Support for the long-requested Python catalog makes its first appearance
in 2.3.
The heart of the catalog is the new Distutils ‘register’ command.
Running ‘python setup.py register’ will collect the metadata describing
a package, such as its name, version, maintainer, description, &c., and
send it to a central catalog server. The resulting catalog is available
from ‘https://pypi.org’.
To make the catalog a bit more useful, a new optional `classifiers'
keyword argument has been added to the Distutils ‘setup()’ function. A
list of Trove(1)-style strings can be supplied to help classify the
software.
Here’s an example ‘setup.py’ with classifiers, written to be compatible
with older versions of the Distutils:
from distutils import core
kw = {'name': "Quixote",
'version': "0.5.1",
'description': "A highly Pythonic Web application framework",
# ...
}
if (hasattr(core, 'setup_keywords') and
'classifiers' in core.setup_keywords):
kw['classifiers'] = \
['Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Environment :: No Input/Output (Daemon)',
'Intended Audience :: Developers'],
core.setup(**kw)
The full list of classifiers can be obtained by running ‘python setup.py
register --list-classifiers’.
See also
........
PEP 301(2) - Package Index and Metadata for Distutils
Written and implemented by Richard Jones.
---------- Footnotes ----------
(1) http://catb.org/~esr/trove/
(2) https://www.python.org/dev/peps/pep-0301
File: python.info, Node: PEP 302 New Import Hooks, Next: PEP 305 Comma-separated Files, Prev: PEP 301 Package Index and Metadata for Distutils, Up: What’s New in Python 2 3
1.14.12 PEP 302: New Import Hooks
---------------------------------
While it’s been possible to write custom import hooks ever since the
‘ihooks’ module was introduced in Python 1.3, no one has ever been
really happy with it because writing new import hooks is difficult and
messy. There have been various proposed alternatives such as the
‘imputil’ and ‘iu’ modules, but none of them has ever gained much
acceptance, and none of them were easily usable from C code.
PEP 302(1) borrows ideas from its predecessors, especially from Gordon
McMillan’s ‘iu’ module. Three new items are added to the *note sys: fd.
module:
* ‘sys.path_hooks’ is a list of callable objects; most often they’ll
be classes. Each callable takes a string containing a path and
either returns an importer object that will handle imports from
this path or raises an *note ImportError: 334. exception if it
can’t handle this path.
* ‘sys.path_importer_cache’ caches importer objects for each path, so
‘sys.path_hooks’ will only need to be traversed once for each path.
* ‘sys.meta_path’ is a list of importer objects that will be
traversed before ‘sys.path’ is checked. This list is initially
empty, but user code can add objects to it. Additional built-in
and frozen modules can be imported by an object added to this list.
Importer objects must have a single method, ‘find_module(fullname,
path=None)’. `fullname' will be a module or package name, e.g.
‘string’ or ‘distutils.core’. ‘find_module()’ must return a loader
object that has a single method, ‘load_module(fullname)’, that creates
and returns the corresponding module object.
Pseudo-code for Python’s new import logic, therefore, looks something
like this (simplified a bit; see PEP 302(2) for the full details):
for mp in sys.meta_path:
loader = mp(fullname)
if loader is not None:
= loader.load_module(fullname)
for path in sys.path:
for hook in sys.path_hooks:
try:
importer = hook(path)
except ImportError:
# ImportError, so try the other path hooks
pass
else:
loader = importer.find_module(fullname)
= loader.load_module(fullname)
# Not found!
raise ImportError
See also
........
PEP 302(3) - New Import Hooks
Written by Just van Rossum and Paul Moore. Implemented by Just van
Rossum.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0302
(2) https://www.python.org/dev/peps/pep-0302
(3) https://www.python.org/dev/peps/pep-0302
File: python.info, Node: PEP 305 Comma-separated Files, Next: PEP 307 Pickle Enhancements, Prev: PEP 302 New Import Hooks, Up: What’s New in Python 2 3
1.14.13 PEP 305: Comma-separated Files
--------------------------------------
Comma-separated files are a format frequently used for exporting data
from databases and spreadsheets. Python 2.3 adds a parser for
comma-separated files.
Comma-separated format is deceptively simple at first glance:
Costs,150,200,3.95
Read a line and call ‘line.split(',')’: what could be simpler? But toss
in string data that can contain commas, and things get more complicated:
"Costs",150,200,3.95,"Includes taxes, shipping, and sundry items"
A big ugly regular expression can parse this, but using the new *note
csv: 2a. package is much simpler:
import csv
input = open('datafile', 'rb')
reader = csv.reader(input)
for line in reader:
print line
The ‘reader()’ function takes a number of different options. The field
separator isn’t limited to the comma and can be changed to any
character, and so can the quoting and line-ending characters.
Different dialects of comma-separated files can be defined and
registered; currently there are two dialects, both used by Microsoft
Excel. A separate *note csv.writer: dfc. class will generate
comma-separated files from a succession of tuples or lists, quoting
strings that contain the delimiter.
See also
........
PEP 305(1) - CSV File API
Written and implemented by Kevin Altis, Dave Cole, Andrew McNamara,
Skip Montanaro, Cliff Wells.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0305
File: python.info, Node: PEP 307 Pickle Enhancements, Next: Extended Slices, Prev: PEP 305 Comma-separated Files, Up: What’s New in Python 2 3
1.14.14 PEP 307: Pickle Enhancements
------------------------------------
The *note pickle: ca. and ‘cPickle’ modules received some attention
during the 2.3 development cycle. In 2.2, new-style classes could be
pickled without difficulty, but they weren’t pickled very compactly; PEP
307(1) quotes a trivial example where a new-style class results in a
pickled string three times longer than that for a classic class.
The solution was to invent a new pickle protocol. The *note
pickle.dumps(): dff. function has supported a text-or-binary flag for a
long time. In 2.3, this flag is redefined from a Boolean to an integer:
0 is the old text-mode pickle format, 1 is the old binary format, and
now 2 is a new 2.3-specific format. A new constant, *note
pickle.HIGHEST_PROTOCOL: e00, can be used to select the fanciest
protocol available.
Unpickling is no longer considered a safe operation. 2.2’s *note
pickle: ca. provided hooks for trying to prevent unsafe classes from
being unpickled (specifically, a ‘__safe_for_unpickling__’ attribute),
but none of this code was ever audited and therefore it’s all been
ripped out in 2.3. You should not unpickle untrusted data in any
version of Python.
To reduce the pickling overhead for new-style classes, a new interface
for customizing pickling was added using three special methods: *note
__getstate__(): e01, *note __setstate__(): 19c, and *note
__getnewargs__(): 6ae. Consult PEP 307(2) for the full semantics of
these methods.
As a way to compress pickles yet further, it’s now possible to use
integer codes instead of long strings to identify pickled classes. The
Python Software Foundation will maintain a list of standardized codes;
there’s also a range of codes for private use. Currently no codes have
been specified.
See also
........
PEP 307(3) - Extensions to the pickle protocol
Written and implemented by Guido van Rossum and Tim Peters.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0307
(2) https://www.python.org/dev/peps/pep-0307
(3) https://www.python.org/dev/peps/pep-0307
File: python.info, Node: Extended Slices, Next: Other Language Changes<13>, Prev: PEP 307 Pickle Enhancements, Up: What’s New in Python 2 3
1.14.15 Extended Slices
-----------------------
Ever since Python 1.4, the slicing syntax has supported an optional
third “step” or “stride” argument. For example, these are all legal
Python syntax: ‘L[1:10:2]’, ‘L[:-1:1]’, ‘L[::-1]’. This was added to
Python at the request of the developers of Numerical Python, which uses
the third argument extensively. However, Python’s built-in list, tuple,
and string sequence types have never supported this feature, raising a
*note TypeError: 192. if you tried it. Michael Hudson contributed a
patch to fix this shortcoming.
For example, you can now easily extract the elements of a list that have
even indexes:
>>> L = range(10)
>>> L[::2]
[0, 2, 4, 6, 8]
Negative values also work to make a copy of the same list in reverse
order:
>>> L[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
This also works for tuples, arrays, and strings:
>>> s='abcd'
>>> s[::2]
'ac'
>>> s[::-1]
'dcba'
If you have a mutable sequence such as a list or an array you can assign
to or delete an extended slice, but there are some differences between
assignment to extended and regular slices. Assignment to a regular
slice can be used to change the length of the sequence:
>>> a = range(3)
>>> a
[0, 1, 2]
>>> a[1:3] = [4, 5, 6]
>>> a
[0, 4, 5, 6]
Extended slices aren’t this flexible. When assigning to an extended
slice, the list on the right hand side of the statement must contain the
same number of items as the slice it is replacing:
>>> a = range(4)
>>> a
[0, 1, 2, 3]
>>> a[::2]
[0, 2]
>>> a[::2] = [0, -1]
>>> a
[0, 1, -1, 3]
>>> a[::2] = [0,1,2]
Traceback (most recent call last):
File "", line 1, in ?
ValueError: attempt to assign sequence of size 3 to extended slice of size 2
Deletion is more straightforward:
>>> a = range(4)
>>> a
[0, 1, 2, 3]
>>> a[::2]
[0, 2]
>>> del a[::2]
>>> a
[1, 3]
One can also now pass slice objects to the *note __getitem__(): 27c.
methods of the built-in sequences:
>>> range(10).__getitem__(slice(0, 5, 2))
[0, 2, 4]
Or use slice objects directly in subscripts:
>>> range(10)[slice(0, 5, 2)]
[0, 2, 4]
To simplify implementing sequences that support extended slicing, slice
objects now have a method ‘indices(length)’ which, given the length of a
sequence, returns a ‘(start, stop, step)’ tuple that can be passed
directly to *note range(): 9be. ‘indices()’ handles omitted and
out-of-bounds indices in a manner consistent with regular slices (and
this innocuous phrase hides a welter of confusing details!). The method
is intended to be used like this:
class FakeSeq:
...
def calc_item(self, i):
...
def __getitem__(self, item):
if isinstance(item, slice):
indices = item.indices(len(self))
return FakeSeq([self.calc_item(i) for i in range(*indices)])
else:
return self.calc_item(i)
From this example you can also see that the built-in *note slice: e04.
object is now the type object for the slice type, and is no longer a
function. This is consistent with Python 2.2, where *note int: 184,
*note str: 330, etc., underwent the same change.
File: python.info, Node: Other Language Changes<13>, Next: New Improved and Deprecated Modules<4>, Prev: Extended Slices, Up: What’s New in Python 2 3
1.14.16 Other Language Changes
------------------------------
Here are all of the changes that Python 2.3 makes to the core Python
language.
* The *note yield: 18f. statement is now always a keyword, as
described in section *note PEP 255; Simple Generators: de6. of this
document.
* A new built-in function *note enumerate(): de3. was added, as
described in section *note PEP 279; enumerate(): df0. of this
document.
* Two new constants, *note True: 499. and *note False: 388. were
added along with the built-in *note bool: 183. type, as described
in section *note PEP 285; A Boolean Type: df3. of this document.
* The *note int(): 184. type constructor will now return a long
integer instead of raising an *note OverflowError: 960. when a
string or floating-point number is too large to fit into an
integer. This can lead to the paradoxical result that
‘isinstance(int(expression), int)’ is false, but that seems
unlikely to cause problems in practice.
* Built-in types now support the extended slicing syntax, as
described in section *note Extended Slices: e03. of this document.
* A new built-in function, ‘sum(iterable, start=0)’, adds up the
numeric items in the iterable object and returns their sum. *note
sum(): 1e5. only accepts numbers, meaning that you can’t use it to
concatenate a bunch of strings. (Contributed by Alex Martelli.)
* ‘list.insert(pos, value)’ used to insert `value' at the front of
the list when `pos' was negative. The behaviour has now been
changed to be consistent with slice indexing, so when `pos' is -1
the value will be inserted before the last element, and so forth.
* ‘list.index(value)’, which searches for `value' within the list and
returns its index, now takes optional `start' and `stop' arguments
to limit the search to only part of the list.
* Dictionaries have a new method, ‘pop(key[, *default*])’, that
returns the value corresponding to `key' and removes that key/value
pair from the dictionary. If the requested key isn’t present in
the dictionary, `default' is returned if it’s specified and *note
KeyError: 2c7. raised if it isn’t.
>>> d = {1:2}
>>> d
{1: 2}
>>> d.pop(4)
Traceback (most recent call last):
File "stdin", line 1, in ?
KeyError: 4
>>> d.pop(1)
2
>>> d.pop(1)
Traceback (most recent call last):
File "stdin", line 1, in ?
KeyError: 'pop(): dictionary is empty'
>>> d
{}
>>>
There’s also a new class method, ‘dict.fromkeys(iterable, value)’,
that creates a dictionary with keys taken from the supplied
iterator `iterable' and all values set to `value', defaulting to
‘None’.
(Patches contributed by Raymond Hettinger.)
Also, the *note dict(): 1b8. constructor now accepts keyword
arguments to simplify creating small dictionaries:
>>> dict(red=1, blue=2, green=3, black=4)
{'blue': 2, 'black': 4, 'green': 3, 'red': 1}
(Contributed by Just van Rossum.)
* The *note assert: e06. statement no longer checks the ‘__debug__’
flag, so you can no longer disable assertions by assigning to
‘__debug__’. Running Python with the *note -O: 68b. switch will
still generate code that doesn’t execute any assertions.
* Most type objects are now callable, so you can use them to create
new objects such as functions, classes, and modules. (This means
that the ‘new’ module can be deprecated in a future Python version,
because you can now use the type objects available in the *note
types: 118. module.) For example, you can create a new module
object with the following code:
>>> import types
>>> m = types.ModuleType('abc','docstring')
>>> m
>>> m.__doc__
'docstring'
* A new warning, *note PendingDeprecationWarning: 279. was added to
indicate features which are in the process of being deprecated.
The warning will `not' be printed by default. To check for use of
features that will be deprecated in the future, supply *note
-Walways;;PendingDeprecationWarning;;: 417. on the command line or
use *note warnings.filterwarnings(): e07.
* The process of deprecating string-based exceptions, as in ‘raise
"Error occurred"’, has begun. Raising a string will now trigger
*note PendingDeprecationWarning: 279.
* Using ‘None’ as a variable name will now result in a *note
SyntaxWarning: 191. warning. In a future version of Python, ‘None’
may finally become a keyword.
* The ‘xreadlines()’ method of file objects, introduced in Python
2.1, is no longer necessary because files now behave as their own
iterator. ‘xreadlines()’ was originally introduced as a faster way
to loop over all the lines in a file, but now you can simply write
‘for line in file_obj’. File objects also have a new read-only
‘encoding’ attribute that gives the encoding used by the file;
Unicode strings written to the file will be automatically converted
to bytes using the given encoding.
* The method resolution order used by new-style classes has changed,
though you’ll only notice the difference if you have a really
complicated inheritance hierarchy. Classic classes are unaffected
by this change. Python 2.2 originally used a topological sort of a
class’s ancestors, but 2.3 now uses the C3 algorithm as described
in the paper "A Monotonic Superclass Linearization for Dylan"(1).
To understand the motivation for this change, read Michele
Simionato’s article "Python 2.3 Method Resolution Order"(2), or
read the thread on python-dev starting with the message at
‘https://mail.python.org/pipermail/python-dev/2002-October/029035.html’.
Samuele Pedroni first pointed out the problem and also implemented
the fix by coding the C3 algorithm.
* Python runs multithreaded programs by switching between threads
after executing N bytecodes. The default value for N has been
increased from 10 to 100 bytecodes, speeding up single-threaded
applications by reducing the switching overhead. Some
multithreaded applications may suffer slower response time, but
that’s easily fixed by setting the limit back to a lower number
using ‘sys.setcheckinterval(N)’. The limit can be retrieved with
the new *note sys.getcheckinterval(): e08. function.
* One minor but far-reaching change is that the names of extension
types defined by the modules included with Python now contain the
module and a ‘'.'’ in front of the type name. For example, in
Python 2.2, if you created a socket and printed its ‘__class__’,
you’d get this output:
>>> s = socket.socket()
>>> s.__class__
In 2.3, you get this:
>>> s.__class__
* One of the noted incompatibilities between old- and new-style
classes has been removed: you can now assign to the *note __name__:
c67. and *note __bases__: e09. attributes of new-style classes.
There are some restrictions on what can be assigned to *note
__bases__: e09. along the lines of those relating to assigning to
an instance’s *note __class__: e0a. attribute.
* Menu:
* String Changes::
* Optimizations: Optimizations<12>.
---------- Footnotes ----------
(1) http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.19.3910
(2) http://www.phyast.pitt.edu/~micheles/mro.html
File: python.info, Node: String Changes, Next: Optimizations<12>, Up: Other Language Changes<13>
1.14.16.1 String Changes
........................
* The *note in: 7a3. operator now works differently for strings.
Previously, when evaluating ‘X in Y’ where `X' and `Y' are strings,
`X' could only be a single character. That’s now changed; `X' can
be a string of any length, and ‘X in Y’ will return *note True:
499. if `X' is a substring of `Y'. If `X' is the empty string, the
result is always *note True: 499.
>>> 'ab' in 'abcd'
True
>>> 'ad' in 'abcd'
False
>>> '' in 'abcd'
True
Note that this doesn’t tell you where the substring starts; if you
need that information, use the ‘find()’ string method.
* The ‘strip()’, ‘lstrip()’, and ‘rstrip()’ string methods now have
an optional argument for specifying the characters to strip. The
default is still to remove all whitespace characters:
>>> ' abc '.strip()
'abc'
>>> '><><><>'.strip('<>')
'abc'
>>> '><><><>\n'.strip('<>')
'abc<><><>\n'
>>> u'\u4000\u4001abc\u4000'.strip(u'\u4000')
u'\u4001abc'
>>>
(Suggested by Simon Brunning and implemented by Walter Dörwald.)
* The ‘startswith()’ and ‘endswith()’ string methods now accept
negative numbers for the `start' and `end' parameters.
* Another new string method is ‘zfill()’, originally a function in
the *note string: f6. module. ‘zfill()’ pads a numeric string with
zeros on the left until it’s the specified width. Note that the
‘%’ operator is still more flexible and powerful than ‘zfill()’.
>>> '45'.zfill(4)
'0045'
>>> '12345'.zfill(4)
'12345'
>>> 'goofy'.zfill(6)
'0goofy'
(Contributed by Walter Dörwald.)
* A new type object, ‘basestring’, has been added. Both 8-bit
strings and Unicode strings inherit from this type, so
‘isinstance(obj, basestring)’ will return *note True: 499. for
either kind of string. It’s a completely abstract type, so you
can’t create ‘basestring’ instances.
* Interned strings are no longer immortal and will now be
garbage-collected in the usual way when the only reference to them
is from the internal dictionary of interned strings. (Implemented
by Oren Tirosh.)
File: python.info, Node: Optimizations<12>, Prev: String Changes, Up: Other Language Changes<13>
1.14.16.2 Optimizations
.......................
* The creation of new-style class instances has been made much
faster; they’re now faster than classic classes!
* The ‘sort()’ method of list objects has been extensively rewritten
by Tim Peters, and the implementation is significantly faster.
* Multiplication of large long integers is now much faster thanks to
an implementation of Karatsuba multiplication, an algorithm that
scales better than the O(n*n) required for the grade-school
multiplication algorithm. (Original patch by Christopher A. Craig,
and significantly reworked by Tim Peters.)
* The ‘SET_LINENO’ opcode is now gone. This may provide a small
speed increase, depending on your compiler’s idiosyncrasies. See
section *note Other Changes and Fixes: e0d. for a longer
explanation. (Removed by Michael Hudson.)
* ‘xrange()’ objects now have their own iterator, making ‘for i in
xrange(n)’ slightly faster than ‘for i in range(n)’. (Patch by
Raymond Hettinger.)
* A number of small rearrangements have been made in various hotspots
to improve performance, such as inlining a function or removing
some code. (Implemented mostly by GvR, but lots of people have
contributed single changes.)
The net result of the 2.3 optimizations is that Python 2.3 runs the
pystone benchmark around 25% faster than Python 2.2.
File: python.info, Node: New Improved and Deprecated Modules<4>, Next: Pymalloc A Specialized Object Allocator, Prev: Other Language Changes<13>, Up: What’s New in Python 2 3
1.14.17 New, Improved, and Deprecated Modules
---------------------------------------------
As usual, Python’s standard library received a number of enhancements
and bug fixes. Here’s a partial list of the most notable changes,
sorted alphabetically by module name. Consult the ‘Misc/NEWS’ file in
the source tree for a more complete list of changes, or look through the
CVS logs for all the details.
* The *note array: 7. module now supports arrays of Unicode
characters using the ‘'u'’ format character. Arrays also now
support using the ‘+=’ assignment operator to add another array’s
contents, and the ‘*=’ assignment operator to repeat an array.
(Contributed by Jason Orendorff.)
* The ‘bsddb’ module has been replaced by version 4.1.6 of the
PyBSDDB(1) package, providing a more complete interface to the
transactional features of the BerkeleyDB library.
The old version of the module has been renamed to ‘bsddb185’ and is
no longer built automatically; you’ll have to edit ‘Modules/Setup’
to enable it. Note that the new ‘bsddb’ package is intended to be
compatible with the old module, so be sure to file bugs if you
discover any incompatibilities. When upgrading to Python 2.3, if
the new interpreter is compiled with a new version of the
underlying BerkeleyDB library, you will almost certainly have to
convert your database files to the new version. You can do this
fairly easily with the new scripts ‘db2pickle.py’ and
‘pickle2db.py’ which you will find in the distribution’s
‘Tools/scripts’ directory. If you’ve already been using the
PyBSDDB package and importing it as ‘bsddb3’, you will have to
change your ‘import’ statements to import it as ‘bsddb’.
* The new *note bz2: 14. module is an interface to the bz2 data
compression library. bz2-compressed data is usually smaller than
corresponding *note zlib: 144.-compressed data. (Contributed by
Gustavo Niemeyer.)
* A set of standard date/time types has been added in the new *note
datetime: 31. module. See the following section for more details.
* The Distutils ‘Extension’ class now supports an extra constructor
argument named `depends' for listing additional source files that
an extension depends on. This lets Distutils recompile the module
if any of the dependency files are modified. For example, if
‘sampmodule.c’ includes the header file ‘sample.h’, you would
create the ‘Extension’ object like this:
ext = Extension("samp",
sources=["sampmodule.c"],
depends=["sample.h"])
Modifying ‘sample.h’ would then cause the module to be recompiled.
(Contributed by Jeremy Hylton.)
* Other minor changes to Distutils: it now checks for the ‘CC’,
‘CFLAGS’, ‘CPP’, ‘LDFLAGS’, and ‘CPPFLAGS’ environment variables,
using them to override the settings in Python’s configuration
(contributed by Robert Weber).
* Previously the *note doctest: 67. module would only search the
docstrings of public methods and functions for test cases, but it
now also examines private ones as well. The ‘DocTestSuite()’
function creates a *note unittest.TestSuite: 6ce. object from a set
of *note doctest: 67. tests.
* The new ‘gc.get_referents(object)’ function returns a list of all
the objects referenced by `object'.
* The *note getopt: 87. module gained a new function, ‘gnu_getopt()’,
that supports the same arguments as the existing *note getopt():
87. function but uses GNU-style scanning mode. The existing *note
getopt(): 87. stops processing options as soon as a non-option
argument is encountered, but in GNU-style mode processing
continues, meaning that options and arguments can be mixed. For
example:
>>> getopt.getopt(['-f', 'filename', 'output', '-v'], 'f:v')
([('-f', 'filename')], ['output', '-v'])
>>> getopt.gnu_getopt(['-f', 'filename', 'output', '-v'], 'f:v')
([('-f', 'filename'), ('-v', '')], ['output'])
(Contributed by Peter Åstrand.)
* The *note grp: 8b, *note pwd: d6, and *note resource: e0. modules
now return enhanced tuples:
>>> import grp
>>> g = grp.getgrnam('amk')
>>> g.gr_name, g.gr_gid
('amk', 500)
* The *note gzip: 8c. module can now handle files exceeding 2 GiB.
* The new *note heapq: 8e. module contains an implementation of a
heap queue algorithm. A heap is an array-like data structure that
keeps items in a partially sorted order such that, for every index
`k', ‘heap[k] <= heap[2*k+1]’ and ‘heap[k] <= heap[2*k+2]’. This
makes it quick to remove the smallest item, and inserting a new
item while maintaining the heap property is O(lg n). (See
‘https://xlinux.nist.gov/dads//HTML/priorityque.html’ for more
information about the priority queue data structure.)
The *note heapq: 8e. module provides ‘heappush()’ and ‘heappop()’
functions for adding and removing items while maintaining the heap
property on top of some other mutable Python sequence type. Here’s
an example that uses a Python list:
>>> import heapq
>>> heap = []
>>> for item in [3, 7, 5, 11, 1]:
... heapq.heappush(heap, item)
...
>>> heap
[1, 3, 5, 11, 7]
>>> heapq.heappop(heap)
1
>>> heapq.heappop(heap)
3
>>> heap
[5, 7, 11]
(Contributed by Kevin O’Connor.)
* The IDLE integrated development environment has been updated using
the code from the IDLEfork project
(‘http://idlefork.sourceforge.net’). The most notable feature is
that the code being developed is now executed in a subprocess,
meaning that there’s no longer any need for manual ‘reload()’
operations. IDLE’s core code has been incorporated into the
standard library as the ‘idlelib’ package.
* The *note imaplib: 98. module now supports IMAP over SSL.
(Contributed by Piers Lauder and Tino Lange.)
* The *note itertools: a3. contains a number of useful functions for
use with iterators, inspired by various functions provided by the
ML and Haskell languages. For example,
‘itertools.ifilter(predicate, iterator)’ returns all elements in
the iterator for which the function ‘predicate()’ returns *note
True: 499, and ‘itertools.repeat(obj, N)’ returns ‘obj’ `N' times.
There are a number of other functions in the module; see the
package’s reference documentation for details. (Contributed by
Raymond Hettinger.)
* Two new functions in the *note math: b1. module, ‘degrees(rads)’
and ‘radians(degs)’, convert between radians and degrees. Other
functions in the *note math: b1. module such as *note math.sin():
e0f. and *note math.cos(): e10. have always required input values
measured in radians. Also, an optional `base' argument was added
to *note math.log(): e11. to make it easier to compute logarithms
for bases other than ‘e’ and ‘10’. (Contributed by Raymond
Hettinger.)
* Several new POSIX functions (‘getpgid()’, ‘killpg()’, ‘lchown()’,
‘loadavg()’, ‘major()’, ‘makedev()’, ‘minor()’, and ‘mknod()’) were
added to the *note posix: d1. module that underlies the *note os:
c4. module. (Contributed by Gustavo Niemeyer, Geert Jansen, and
Denis S. Otkidach.)
* In the *note os: c4. module, the ‘*stat()’ family of functions can
now report fractions of a second in a timestamp. Such time stamps
are represented as floats, similar to the value returned by *note
time.time(): 31d.
During testing, it was found that some applications will break if
time stamps are floats. For compatibility, when using the tuple
interface of the ‘stat_result’ time stamps will be represented as
integers. When using named fields (a feature first introduced in
Python 2.2), time stamps are still represented as integers, unless
‘os.stat_float_times()’ is invoked to enable float return values:
>>> os.stat("/tmp").st_mtime
1034791200
>>> os.stat_float_times(True)
>>> os.stat("/tmp").st_mtime
1034791200.6335014
In Python 2.4, the default will change to always returning floats.
Application developers should enable this feature only if all their
libraries work properly when confronted with floating point time
stamps, or if they use the tuple API. If used, the feature should
be activated on an application level instead of trying to enable it
on a per-use basis.
* The *note optparse: c3. module contains a new parser for
command-line arguments that can convert option values to a
particular Python type and will automatically generate a usage
message. See the following section for more details.
* The old and never-documented ‘linuxaudiodev’ module has been
deprecated, and a new version named *note ossaudiodev: c6. has been
added. The module was renamed because the OSS sound drivers can be
used on platforms other than Linux, and the interface has also been
tidied and brought up to date in various ways. (Contributed by
Greg Ward and Nicholas FitzRoy-Dale.)
* The new *note platform: ce. module contains a number of functions
that try to determine various properties of the platform you’re
running on. There are functions for getting the architecture, CPU
type, the Windows OS version, and even the Linux distribution
version. (Contributed by Marc-André Lemburg.)
* The parser objects provided by the ‘pyexpat’ module can now
optionally buffer character data, resulting in fewer calls to your
character data handler and therefore faster performance. Setting
the parser object’s ‘buffer_text’ attribute to *note True: 499.
will enable buffering.
* The ‘sample(population, k)’ function was added to the *note random:
dc. module. `population' is a sequence or ‘xrange’ object
containing the elements of a population, and ‘sample()’ chooses `k'
elements from the population without replacing chosen elements.
`k' can be any value up to ‘len(population)’. For example:
>>> days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'St', 'Sn']
>>> random.sample(days, 3) # Choose 3 elements
['St', 'Sn', 'Th']
>>> random.sample(days, 7) # Choose 7 elements
['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn']
>>> random.sample(days, 7) # Choose 7 again
['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th']
>>> random.sample(days, 8) # Can't choose eight
Traceback (most recent call last):
File "", line 1, in ?
File "random.py", line 414, in sample
raise ValueError, "sample larger than population"
ValueError: sample larger than population
>>> random.sample(xrange(1,10000,2), 10) # Choose ten odd nos. under 10000
[3407, 3805, 1505, 7023, 2401, 2267, 9733, 3151, 8083, 9195]
The *note random: dc. module now uses a new algorithm, the Mersenne
Twister, implemented in C. It’s faster and more extensively studied
than the previous algorithm.
(All changes contributed by Raymond Hettinger.)
* The *note readline: de. module also gained a number of new
functions: ‘get_history_item()’, ‘get_current_history_length()’,
and ‘redisplay()’.
* The ‘rexec’ and ‘Bastion’ modules have been declared dead, and
attempts to import them will fail with a *note RuntimeError: 2ba.
New-style classes provide new ways to break out of the restricted
execution environment provided by ‘rexec’, and no one has interest
in fixing them or time to do so. If you have applications using
‘rexec’, rewrite them to use something else.
(Sticking with Python 2.2 or 2.1 will not make your applications
any safer because there are known bugs in the ‘rexec’ module in
those versions. To repeat: if you’re using ‘rexec’, stop using it
immediately.)
* The ‘rotor’ module has been deprecated because the algorithm it
uses for encryption is not believed to be secure. If you need
encryption, use one of the several AES Python modules that are
available separately.
* The *note shutil: e9. module gained a ‘move(src, dest)’ function
that recursively moves a file or directory to a new location.
* Support for more advanced POSIX signal handling was added to the
*note signal: ea. but then removed again as it proved impossible to
make it work reliably across platforms.
* The *note socket: ef. module now supports timeouts. You can call
the ‘settimeout(t)’ method on a socket object to set a timeout of
`t' seconds. Subsequent socket operations that take longer than
`t' seconds to complete will abort and raise a *note
socket.timeout: c0e. exception.
The original timeout implementation was by Tim O’Malley. Michael
Gilfix integrated it into the Python *note socket: ef. module and
shepherded it through a lengthy review. After the code was checked
in, Guido van Rossum rewrote parts of it. (This is a good example
of a collaborative development process in action.)
* On Windows, the *note socket: ef. module now ships with Secure
Sockets Layer (SSL) support.
* The value of the C ‘PYTHON_API_VERSION’ macro is now exposed at the
Python level as ‘sys.api_version’. The current exception can be
cleared by calling the new ‘sys.exc_clear()’ function.
* The new *note tarfile: 101. module allows reading from and writing
to ‘tar’-format archive files. (Contributed by Lars Gustäbel.)
* The new *note textwrap: 108. module contains functions for wrapping
strings containing paragraphs of text. The ‘wrap(text, width)’
function takes a string and returns a list containing the text
split into lines of no more than the chosen width. The ‘fill(text,
width)’ function returns a single string, reformatted to fit into
lines no longer than the chosen width. (As you can guess, ‘fill()’
is built on top of ‘wrap()’. For example:
>>> import textwrap
>>> paragraph = "Not a whit, we defy augury: ... more text ..."
>>> textwrap.wrap(paragraph, 60)
["Not a whit, we defy augury: there's a special providence in",
"the fall of a sparrow. If it be now, 'tis not to come; if it",
...]
>>> print textwrap.fill(paragraph, 35)
Not a whit, we defy augury: there's
a special providence in the fall of
a sparrow. If it be now, 'tis not
to come; if it be not to come, it
will be now; if it be not now, yet
it will come: the readiness is all.
>>>
The module also contains a ‘TextWrapper’ class that actually
implements the text wrapping strategy. Both the ‘TextWrapper’
class and the ‘wrap()’ and ‘fill()’ functions support a number of
additional keyword arguments for fine-tuning the formatting;
consult the module’s documentation for details. (Contributed by
Greg Ward.)
* The ‘thread’ and *note threading: 109. modules now have companion
modules, ‘dummy_thread’ and *note dummy_threading: 68, that provide
a do-nothing implementation of the ‘thread’ module’s interface for
platforms where threads are not supported. The intention is to
simplify thread-aware modules (ones that `don’t' rely on threads to
run) by putting the following code at the top:
try:
import threading as _threading
except ImportError:
import dummy_threading as _threading
In this example, ‘_threading’ is used as the module name to make it
clear that the module being used is not necessarily the actual
*note threading: 109. module. Code can call functions and use
classes in ‘_threading’ whether or not threads are supported,
avoiding an *note if: de7. statement and making the code slightly
clearer. This module will not magically make multithreaded code
run without threads; code that waits for another thread to return
or to do something will simply hang forever.
* The *note time: 10a. module’s ‘strptime()’ function has long been
an annoyance because it uses the platform C library’s ‘strptime()’
implementation, and different platforms sometimes have odd bugs.
Brett Cannon contributed a portable implementation that’s written
in pure Python and should behave identically on all platforms.
* The new *note timeit: 10b. module helps measure how long snippets
of Python code take to execute. The ‘timeit.py’ file can be run
directly from the command line, or the module’s ‘Timer’ class can
be imported and used directly. Here’s a short example that figures
out whether it’s faster to convert an 8-bit string to Unicode by
appending an empty Unicode string to it or by using the ‘unicode()’
function:
import timeit
timer1 = timeit.Timer('unicode("abc")')
timer2 = timeit.Timer('"abc" + u""')
# Run three trials
print timer1.repeat(repeat=3, number=100000)
print timer2.repeat(repeat=3, number=100000)
# On my laptop this outputs:
# [0.36831796169281006, 0.37441694736480713, 0.35304892063140869]
# [0.17574405670166016, 0.18193507194519043, 0.17565798759460449]
* The ‘Tix’ module has received various bug fixes and updates for the
current version of the Tix package.
* The ‘Tkinter’ module now works with a thread-enabled version of
Tcl. Tcl’s threading model requires that widgets only be accessed
from the thread in which they’re created; accesses from another
thread can cause Tcl to panic. For certain Tcl interfaces,
‘Tkinter’ will now automatically avoid this when a widget is
accessed from a different thread by marshalling a command, passing
it to the correct thread, and waiting for the results. Other
interfaces can’t be handled automatically but ‘Tkinter’ will now
raise an exception on such an access so that you can at least find
out about the problem. See
‘https://mail.python.org/pipermail/python-dev/2002-December/031107.html’
for a more detailed explanation of this change. (Implemented by
Martin von Löwis.)
* Calling Tcl methods through ‘_tkinter’ no longer returns only
strings. Instead, if Tcl returns other objects those objects are
converted to their Python equivalent, if one exists, or wrapped
with a ‘_tkinter.Tcl_Obj’ object if no Python equivalent exists.
This behavior can be controlled through the ‘wantobjects()’ method
of ‘tkapp’ objects.
When using ‘_tkinter’ through the ‘Tkinter’ module (as most Tkinter
applications will), this feature is always activated. It should
not cause compatibility problems, since Tkinter would always
convert string results to Python types where possible.
If any incompatibilities are found, the old behavior can be
restored by setting the ‘wantobjects’ variable in the ‘Tkinter’
module to false before creating the first ‘tkapp’ object.
import Tkinter
Tkinter.wantobjects = 0
Any breakage caused by this change should be reported as a bug.
* The ‘UserDict’ module has a new ‘DictMixin’ class which defines all
dictionary methods for classes that already have a minimum mapping
interface. This greatly simplifies writing classes that need to be
substitutable for dictionaries, such as the classes in the *note
shelve: e7. module.
Adding the mix-in as a superclass provides the full dictionary
interface whenever the class defines *note __getitem__(): 27c,
*note __setitem__(): c62, *note __delitem__(): c63, and ‘keys()’.
For example:
>>> import UserDict
>>> class SeqDict(UserDict.DictMixin):
... """Dictionary lookalike implemented with lists."""
... def __init__(self):
... self.keylist = []
... self.valuelist = []
... def __getitem__(self, key):
... try:
... i = self.keylist.index(key)
... except ValueError:
... raise KeyError
... return self.valuelist[i]
... def __setitem__(self, key, value):
... try:
... i = self.keylist.index(key)
... self.valuelist[i] = value
... except ValueError:
... self.keylist.append(key)
... self.valuelist.append(value)
... def __delitem__(self, key):
... try:
... i = self.keylist.index(key)
... except ValueError:
... raise KeyError
... self.keylist.pop(i)
... self.valuelist.pop(i)
... def keys(self):
... return list(self.keylist)
...
>>> s = SeqDict()
>>> dir(s) # See that other dictionary methods are implemented
['__cmp__', '__contains__', '__delitem__', '__doc__', '__getitem__',
'__init__', '__iter__', '__len__', '__module__', '__repr__',
'__setitem__', 'clear', 'get', 'has_key', 'items', 'iteritems',
'iterkeys', 'itervalues', 'keylist', 'keys', 'pop', 'popitem',
'setdefault', 'update', 'valuelist', 'values']
(Contributed by Raymond Hettinger.)
* The DOM implementation in *note xml.dom.minidom: 135. can now
generate XML output in a particular encoding by providing an
optional encoding argument to the ‘toxml()’ and ‘toprettyxml()’
methods of DOM nodes.
* The ‘xmlrpclib’ module now supports an XML-RPC extension for
handling nil data values such as Python’s ‘None’. Nil values are
always supported on unmarshalling an XML-RPC response. To generate
requests containing ‘None’, you must supply a true value for the
`allow_none' parameter when creating a ‘Marshaller’ instance.
* The new ‘DocXMLRPCServer’ module allows writing self-documenting
XML-RPC servers. Run it in demo mode (as a program) to see it in
action. Pointing the Web browser to the RPC server produces
pydoc-style documentation; pointing xmlrpclib to the server allows
invoking the actual methods. (Contributed by Brian Quinlan.)
* Support for internationalized domain names (RFCs 3454, 3490, 3491,
and 3492) has been added. The “idna” encoding can be used to
convert between a Unicode domain name and the ASCII-compatible
encoding (ACE) of that name.
>{}>{}> u"www.Alliancefrançaise.nu".encode("idna")
'www.xn--alliancefranaise-npb.nu'
The *note socket: ef. module has also been extended to
transparently convert Unicode hostnames to the ACE version before
passing them to the C library. Modules that deal with hostnames
such as ‘httplib’ and *note ftplib: 84.) also support Unicode host
names; ‘httplib’ also sends HTTP ‘Host’ headers using the ACE
version of the domain name. *note urllib: 11d. supports Unicode
URLs with non-ASCII host names as long as the ‘path’ part of the
URL is ASCII only.
To implement this change, the *note stringprep: f7. module, the
‘mkstringprep’ tool and the ‘punycode’ encoding have been added.
* Menu:
* Date/Time Type::
* The optparse Module::
---------- Footnotes ----------
(1) http://pybsddb.sourceforge.net
File: python.info, Node: Date/Time Type, Next: The optparse Module, Up: New Improved and Deprecated Modules<4>
1.14.17.1 Date/Time Type
........................
Date and time types suitable for expressing timestamps were added as the
*note datetime: 31. module. The types don’t support different calendars
or many fancy features, and just stick to the basics of representing
time.
The three primary types are: ‘date’, representing a day, month, and
year; *note time: 4f3, consisting of hour, minute, and second; and *note
datetime: 194, which contains all the attributes of both ‘date’ and
*note time: 4f3. There’s also a ‘timedelta’ class representing
differences between two points in time, and time zone logic is
implemented by classes inheriting from the abstract ‘tzinfo’ class.
You can create instances of ‘date’ and *note time: 4f3. by either
supplying keyword arguments to the appropriate constructor, e.g.
‘datetime.date(year=1972, month=10, day=15)’, or by using one of a
number of class methods. For example, the ‘date.today()’ class method
returns the current local date.
Once created, instances of the date/time classes are all immutable.
There are a number of methods for producing formatted strings from
objects:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.isoformat()
'2002-12-30T21:27:03.994956'
>>> now.ctime() # Only available on date, datetime
'Mon Dec 30 21:27:03 2002'
>>> now.strftime('%Y %d %b')
'2002 30 Dec'
The ‘replace()’ method allows modifying one or more fields of a ‘date’
or *note datetime: 194. instance, returning a new instance:
>>> d = datetime.datetime.now()
>>> d
datetime.datetime(2002, 12, 30, 22, 15, 38, 827738)
>>> d.replace(year=2001, hour = 12)
datetime.datetime(2001, 12, 30, 12, 15, 38, 827738)
>>>
Instances can be compared, hashed, and converted to strings (the result
is the same as that of ‘isoformat()’). ‘date’ and *note datetime: 194.
instances can be subtracted from each other, and added to ‘timedelta’
instances. The largest missing feature is that there’s no standard
library support for parsing strings and getting back a ‘date’ or *note
datetime: 194.
For more information, refer to the module’s reference documentation.
(Contributed by Tim Peters.)
File: python.info, Node: The optparse Module, Prev: Date/Time Type, Up: New Improved and Deprecated Modules<4>
1.14.17.2 The optparse Module
.............................
The *note getopt: 87. module provides simple parsing of command-line
arguments. The new *note optparse: c3. module (originally named Optik)
provides more elaborate command-line parsing that follows the Unix
conventions, automatically creates the output for ‘--help’, and can
perform different actions for different options.
You start by creating an instance of ‘OptionParser’ and telling it what
your program’s options are.
import sys
from optparse import OptionParser
op = OptionParser()
op.add_option('-i', '--input',
action='store', type='string', dest='input',
help='set input filename')
op.add_option('-l', '--length',
action='store', type='int', dest='length',
help='set maximum length of output')
Parsing a command line is then done by calling the ‘parse_args()’
method.
options, args = op.parse_args(sys.argv[1:])
print options
print args
This returns an object containing all of the option values, and a list
of strings containing the remaining arguments.
Invoking the script with the various arguments now works as you’d expect
it to. Note that the length argument is automatically converted to an
integer.
$ ./python opt.py -i data arg1
['arg1']
$ ./python opt.py --input=data --length=4
[]
$
The help message is automatically generated for you:
$ ./python opt.py --help
usage: opt.py [options]
options:
-h, --help show this help message and exit
-iINPUT, --input=INPUT
set input filename
-lLENGTH, --length=LENGTH
set maximum length of output
$
See the module’s documentation for more details.
Optik was written by Greg Ward, with suggestions from the readers of the
Getopt SIG.
File: python.info, Node: Pymalloc A Specialized Object Allocator, Next: Build and C API Changes<12>, Prev: New Improved and Deprecated Modules<4>, Up: What’s New in Python 2 3
1.14.18 Pymalloc: A Specialized Object Allocator
------------------------------------------------
Pymalloc, a specialized object allocator written by Vladimir Marangozov,
was a feature added to Python 2.1. Pymalloc is intended to be faster
than the system ‘malloc()’ and to have less memory overhead for
allocation patterns typical of Python programs. The allocator uses C’s
‘malloc()’ function to get large pools of memory and then fulfills
smaller memory requests from these pools.
In 2.1 and 2.2, pymalloc was an experimental feature and wasn’t enabled
by default; you had to explicitly enable it when compiling Python by
providing the ‘--with-pymalloc’ option to the ‘configure’ script. In
2.3, pymalloc has had further enhancements and is now enabled by
default; you’ll have to supply ‘--without-pymalloc’ to disable it.
This change is transparent to code written in Python; however, pymalloc
may expose bugs in C extensions. Authors of C extension modules should
test their code with pymalloc enabled, because some incorrect code may
cause core dumps at runtime.
There’s one particularly common error that causes problems. There are a
number of memory allocation functions in Python’s C API that have
previously just been aliases for the C library’s ‘malloc()’ and
‘free()’, meaning that if you accidentally called mismatched functions
the error wouldn’t be noticeable. When the object allocator is enabled,
these functions aren’t aliases of ‘malloc()’ and ‘free()’ any more, and
calling the wrong function to free memory may get you a core dump. For
example, if memory was allocated using *note PyObject_Malloc(): 508, it
has to be freed using *note PyObject_Free(): 504, not ‘free()’. A few
modules included with Python fell afoul of this and had to be fixed;
doubtless there are more third-party modules that will have the same
problem.
As part of this change, the confusing multiple interfaces for allocating
memory have been consolidated down into two API families. Memory
allocated with one family must not be manipulated with functions from
the other family. There is one family for allocating chunks of memory
and another family of functions specifically for allocating Python
objects.
* To allocate and free an undistinguished chunk of memory use the
“raw memory” family: *note PyMem_Malloc(): 505, *note
PyMem_Realloc(): 96f, and *note PyMem_Free(): da9.
* The “object memory” family is the interface to the pymalloc
facility described above and is biased towards a large number of
“small” allocations: *note PyObject_Malloc(): 508, *note
PyObject_Realloc(): daa, and *note PyObject_Free(): 504.
* To allocate and free Python objects, use the “object” family *note
PyObject_New(): 2d2, *note PyObject_NewVar(): 2d3, and *note
PyObject_Del(): e16.
Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides
debugging features to catch memory overwrites and doubled frees in both
extension modules and in the interpreter itself. To enable this
support, compile a debugging version of the Python interpreter by
running ‘configure’ with ‘--with-pydebug’.
To aid extension writers, a header file ‘Misc/pymemcompat.h’ is
distributed with the source to Python 2.3 that allows Python extensions
to use the 2.3 interfaces to memory allocation while compiling against
any version of Python since 1.5.2. You would copy the file from
Python’s source distribution and bundle it with the source of your
extension.
See also
........
‘https://hg.python.org/cpython/file/default/Objects/obmalloc.c’
For the full details of the pymalloc implementation, see the
comments at the top of the file ‘Objects/obmalloc.c’ in the Python
source code. The above link points to the file within the
python.org SVN browser.
File: python.info, Node: Build and C API Changes<12>, Next: Other Changes and Fixes<2>, Prev: Pymalloc A Specialized Object Allocator, Up: What’s New in Python 2 3
1.14.19 Build and C API Changes
-------------------------------
Changes to Python’s build process and to the C API include:
* The cycle detection implementation used by the garbage collection
has proven to be stable, so it’s now been made mandatory. You can
no longer compile Python without it, and the ‘--with-cycle-gc’
switch to ‘configure’ has been removed.
* Python can now optionally be built as a shared library
(‘libpython2.3.so’) by supplying ‘--enable-shared’ when running
Python’s ‘configure’ script. (Contributed by Ondrej Palkovsky.)
* The ‘DL_EXPORT’ and ‘DL_IMPORT’ macros are now deprecated.
Initialization functions for Python extension modules should now be
declared using the new macro ‘PyMODINIT_FUNC’, while the Python
core will generally use the ‘PyAPI_FUNC’ and ‘PyAPI_DATA’ macros.
* The interpreter can be compiled without any docstrings for the
built-in functions and modules by supplying ‘--without-doc-strings’
to the ‘configure’ script. This makes the Python executable about
10% smaller, but will also mean that you can’t get help for
Python’s built-ins. (Contributed by Gustavo Niemeyer.)
* The ‘PyArg_NoArgs()’ macro is now deprecated, and code that uses it
should be changed. For Python 2.2 and later, the method definition
table can specify the *note METH_NOARGS: e18. flag, signalling that
there are no arguments, and the argument checking can then be
removed. If compatibility with pre-2.2 versions of Python is
important, the code could use ‘PyArg_ParseTuple(args, "")’ instead,
but this will be slower than using *note METH_NOARGS: e18.
* *note PyArg_ParseTuple(): 26a. accepts new format characters for
various sizes of unsigned integers: ‘B’ for ‘unsigned char’, ‘H’
for ‘unsigned short int’, ‘I’ for ‘unsigned int’, and ‘K’ for
‘unsigned long long’.
* A new function, ‘PyObject_DelItemString(mapping, char *key)’ was
added as shorthand for ‘PyObject_DelItem(mapping,
PyString_New(key))’.
* File objects now manage their internal string buffer differently,
increasing it exponentially when needed. This results in the
benchmark tests in ‘Lib/test/test_bufio.py’ speeding up
considerably (from 57 seconds to 1.7 seconds, according to one
measurement).
* It’s now possible to define class and static methods for a C
extension type by setting either the *note METH_CLASS: e19. or
*note METH_STATIC: e1a. flags in a method’s *note PyMethodDef: e1b.
structure.
* Python now includes a copy of the Expat XML parser’s source code,
removing any dependence on a system version or local installation
of Expat.
* If you dynamically allocate type objects in your extension, you
should be aware of a change in the rules relating to the
‘__module__’ and *note __name__: c67. attributes. In summary, you
will want to ensure the type’s dictionary contains a ‘'__module__'’
key; making the module name the part of the type name leading up to
the final period will no longer have the desired effect. For more
detail, read the API reference documentation or the source.
* Menu:
* Port-Specific Changes: Port-Specific Changes<3>.
File: python.info, Node: Port-Specific Changes<3>, Up: Build and C API Changes<12>
1.14.19.1 Port-Specific Changes
...............................
Support for a port to IBM’s OS/2 using the EMX runtime environment was
merged into the main Python source tree. EMX is a POSIX emulation layer
over the OS/2 system APIs. The Python port for EMX tries to support all
the POSIX-like capability exposed by the EMX runtime, and mostly
succeeds; ‘fork()’ and *note fcntl(): 7e. are restricted by the
limitations of the underlying emulation layer. The standard OS/2 port,
which uses IBM’s Visual Age compiler, also gained support for
case-sensitive import semantics as part of the integration of the EMX
port into CVS. (Contributed by Andrew MacIntyre.)
On MacOS, most toolbox modules have been weaklinked to improve backward
compatibility. This means that modules will no longer fail to load if a
single routine is missing on the current OS version. Instead calling
the missing routine will raise an exception. (Contributed by Jack
Jansen.)
The RPM spec files, found in the ‘Misc/RPM/’ directory in the Python
source distribution, were updated for 2.3. (Contributed by Sean
Reifschneider.)
Other new platforms now supported by Python include AtheOS
(‘http://www.atheos.cx/’), GNU/Hurd, and OpenVMS.
File: python.info, Node: Other Changes and Fixes<2>, Next: Porting to Python 2 3, Prev: Build and C API Changes<12>, Up: What’s New in Python 2 3
1.14.20 Other Changes and Fixes
-------------------------------
As usual, there were a bunch of other improvements and bugfixes
scattered throughout the source tree. A search through the CVS change
logs finds there were 523 patches applied and 514 bugs fixed between
Python 2.2 and 2.3. Both figures are likely to be underestimates.
Some of the more notable changes are:
* If the *note PYTHONINSPECT: e1e. environment variable is set, the
Python interpreter will enter the interactive prompt after running
a Python program, as if Python had been invoked with the *note -i:
e1f. option. The environment variable can be set before running
the Python interpreter, or it can be set by the Python program as
part of its execution.
* The ‘regrtest.py’ script now provides a way to allow “all resources
except `foo'.” A resource name passed to the ‘-u’ option can now be
prefixed with a hyphen (‘'-'’) to mean “remove this resource.” For
example, the option ‘‘-uall,-bsddb’’ could be used to enable the
use of all resources except ‘bsddb’.
* The tools used to build the documentation now work under Cygwin as
well as Unix.
* The ‘SET_LINENO’ opcode has been removed. Back in the mists of
time, this opcode was needed to produce line numbers in tracebacks
and support trace functions (for, e.g., *note pdb: c9.). Since
Python 1.5, the line numbers in tracebacks have been computed using
a different mechanism that works with “python -O”. For Python 2.3
Michael Hudson implemented a similar scheme to determine when to
call the trace function, removing the need for ‘SET_LINENO’
entirely.
It would be difficult to detect any resulting difference from
Python code, apart from a slight speed up when Python is run
without *note -O: 68b.
C extensions that access the ‘f_lineno’ field of frame objects
should instead call ‘PyCode_Addr2Line(f->f_code, f->f_lasti)’.
This will have the added effect of making the code work as desired
under “python -O” in earlier versions of Python.
A nifty new feature is that trace functions can now assign to the
‘f_lineno’ attribute of frame objects, changing the line that will
be executed next. A ‘jump’ command has been added to the *note
pdb: c9. debugger taking advantage of this new feature.
(Implemented by Richie Hindle.)
File: python.info, Node: Porting to Python 2 3, Next: Acknowledgements<5>, Prev: Other Changes and Fixes<2>, Up: What’s New in Python 2 3
1.14.21 Porting to Python 2.3
-----------------------------
This section lists previously described changes that may require changes
to your code:
* *note yield: 18f. is now always a keyword; if it’s used as a
variable name in your code, a different name must be chosen.
* For strings `X' and `Y', ‘X in Y’ now works if `X' is more than one
character long.
* The *note int(): 184. type constructor will now return a long
integer instead of raising an *note OverflowError: 960. when a
string or floating-point number is too large to fit into an
integer.
* If you have Unicode strings that contain 8-bit characters, you must
declare the file’s encoding (UTF-8, Latin-1, or whatever) by adding
a comment to the top of the file. See section *note PEP 263;
Source Code Encodings: de9. for more information.
* Calling Tcl methods through ‘_tkinter’ no longer returns only
strings. Instead, if Tcl returns other objects those objects are
converted to their Python equivalent, if one exists, or wrapped
with a ‘_tkinter.Tcl_Obj’ object if no Python equivalent exists.
* Large octal and hex literals such as ‘0xffffffff’ now trigger a
*note FutureWarning: 325. Currently they’re stored as 32-bit
numbers and result in a negative value, but in Python 2.4 they’ll
become positive long integers.
There are a few ways to fix this warning. If you really need a
positive number, just add an ‘L’ to the end of the literal. If
you’re trying to get a 32-bit integer with low bits set and have
previously used an expression such as ‘~(1 << 31)’, it’s probably
clearest to start with all bits set and clear the desired upper
bits. For example, to clear just the top bit (bit 31), you could
write ‘0xffffffffL &~(1L<<31)’.
* You can no longer disable assertions by assigning to ‘__debug__’.
* The Distutils ‘setup()’ function has gained various new keyword
arguments such as `depends'. Old versions of the Distutils will
abort if passed unknown keywords. A solution is to check for the
presence of the new ‘get_distutil_options()’ function in your
‘setup.py’ and only uses the new keywords with a version of the
Distutils that supports them:
from distutils import core
kw = {'sources': 'foo.c', ...}
if hasattr(core, 'get_distutil_options'):
kw['depends'] = ['foo.h']
ext = Extension(**kw)
* Using ‘None’ as a variable name will now result in a *note
SyntaxWarning: 191. warning.
* Names of extension types defined by the modules included with
Python now contain the module and a ‘'.'’ in front of the type
name.
File: python.info, Node: Acknowledgements<5>, Prev: Porting to Python 2 3, Up: What’s New in Python 2 3
1.14.22 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Jeff Bauer, Simon Brunning, Brett Cannon, Michael Chermside,
Andrew Dalke, Scott David Daniels, Fred L. Drake, Jr., David Fraser,
Kelly Gerber, Raymond Hettinger, Michael Hudson, Chris Lambert, Detlef
Lannert, Martin von Löwis, Andrew MacIntyre, Lalo Martins, Chad Netzer,
Gustavo Niemeyer, Neal Norwitz, Hans Nowak, Chris Reedy, Francesco
Ricciardi, Vinay Sajip, Neil Schemenauer, Roman Suzi, Jason Tishler,
Just van Rossum.
File: python.info, Node: What’s New in Python 2 2, Next: What’s New in Python 2 1, Prev: What’s New in Python 2 3, Up: What’s New in Python
1.15 What’s New in Python 2.2
=============================
Author: A.M. Kuchling
* Menu:
* Introduction::
* PEPs 252 and 253; Type and Class Changes: PEPs 252 and 253 Type and Class Changes.
* PEP 234; Iterators: PEP 234 Iterators.
* PEP 255; Simple Generators: PEP 255 Simple Generators<2>.
* PEP 237; Unifying Long Integers and Integers: PEP 237 Unifying Long Integers and Integers<2>.
* PEP 238; Changing the Division Operator: PEP 238 Changing the Division Operator.
* Unicode Changes::
* PEP 227; Nested Scopes: PEP 227 Nested Scopes.
* New and Improved Modules: New and Improved Modules<3>.
* Interpreter Changes and Fixes::
* Other Changes and Fixes: Other Changes and Fixes<3>.
* Acknowledgements: Acknowledgements<6>.
File: python.info, Node: Introduction, Next: PEPs 252 and 253 Type and Class Changes, Up: What’s New in Python 2 2
1.15.1 Introduction
-------------------
This article explains the new features in Python 2.2.2, released on
October 14, 2002. Python 2.2.2 is a bugfix release of Python 2.2,
originally released on December 21, 2001.
Python 2.2 can be thought of as the “cleanup release”. There are some
features such as generators and iterators that are completely new, but
most of the changes, significant and far-reaching though they may be,
are aimed at cleaning up irregularities and dark corners of the language
design.
This article doesn’t attempt to provide a complete specification of the
new features, but instead provides a convenient overview. For full
details, you should refer to the documentation for Python 2.2, such as
the Python Library Reference(1) and the Python Reference Manual(2). If
you want to understand the complete implementation and design rationale
for a change, refer to the PEP for a particular new feature.
---------- Footnotes ----------
(1) https://docs.python.org/2.2/lib/lib.html
(2) https://docs.python.org/2.2/ref/ref.html
File: python.info, Node: PEPs 252 and 253 Type and Class Changes, Next: PEP 234 Iterators, Prev: Introduction, Up: What’s New in Python 2 2
1.15.2 PEPs 252 and 253: Type and Class Changes
-----------------------------------------------
The largest and most far-reaching changes in Python 2.2 are to Python’s
model of objects and classes. The changes should be backward
compatible, so it’s likely that your code will continue to run
unchanged, but the changes provide some amazing new capabilities.
Before beginning this, the longest and most complicated section of this
article, I’ll provide an overview of the changes and offer some
comments.
A long time ago I wrote a Web page listing flaws in Python’s design.
One of the most significant flaws was that it’s impossible to subclass
Python types implemented in C. In particular, it’s not possible to
subclass built-in types, so you can’t just subclass, say, lists in order
to add a single useful method to them. The ‘UserList’ module provides a
class that supports all of the methods of lists and that can be
subclassed further, but there’s lots of C code that expects a regular
Python list and won’t accept a ‘UserList’ instance.
Python 2.2 fixes this, and in the process adds some exciting new
capabilities. A brief summary:
* You can subclass built-in types such as lists and even integers,
and your subclasses should work in every place that requires the
original type.
* It’s now possible to define static and class methods, in addition
to the instance methods available in previous versions of Python.
* It’s also possible to automatically call methods on accessing or
setting an instance attribute by using a new mechanism called
`properties'. Many uses of *note __getattr__(): 31a. can be
rewritten to use properties instead, making the resulting code
simpler and faster. As a small side benefit, attributes can now
have docstrings, too.
* The list of legal attributes for an instance can be limited to a
particular set using `slots', making it possible to safeguard
against typos and perhaps make more optimizations possible in
future versions of Python.
Some users have voiced concern about all these changes. Sure, they say,
the new features are neat and lend themselves to all sorts of tricks
that weren’t possible in previous versions of Python, but they also make
the language more complicated. Some people have said that they’ve
always recommended Python for its simplicity, and feel that its
simplicity is being lost.
Personally, I think there’s no need to worry. Many of the new features
are quite esoteric, and you can write a lot of Python code without ever
needed to be aware of them. Writing a simple class is no more difficult
than it ever was, so you don’t need to bother learning or teaching them
unless they’re actually needed. Some very complicated tasks that were
previously only possible from C will now be possible in pure Python, and
to my mind that’s all for the better.
I’m not going to attempt to cover every single corner case and small
change that were required to make the new features work. Instead this
section will paint only the broad strokes. See section *note Related
Links: e27, “Related Links”, for further sources of information about
Python 2.2’s new object model.
* Menu:
* Old and New Classes::
* Descriptors::
* Multiple Inheritance; The Diamond Rule: Multiple Inheritance The Diamond Rule.
* Attribute Access::
* Related Links::
File: python.info, Node: Old and New Classes, Next: Descriptors, Up: PEPs 252 and 253 Type and Class Changes
1.15.2.1 Old and New Classes
............................
First, you should know that Python 2.2 really has two kinds of classes:
classic or old-style classes, and new-style classes. The old-style
class model is exactly the same as the class model in earlier versions
of Python. All the new features described in this section apply only to
new-style classes. This divergence isn’t intended to last forever;
eventually old-style classes will be dropped, possibly in Python 3.0.
So how do you define a new-style class? You do it by subclassing an
existing new-style class. Most of Python’s built-in types, such as
integers, lists, dictionaries, and even files, are new-style classes
now. A new-style class named *note object: 2b0, the base class for all
built-in types, has also been added so if no built-in type is suitable,
you can just subclass *note object: 2b0.:
class C(object):
def __init__ (self):
...
...
This means that *note class: c6a. statements that don’t have any base
classes are always classic classes in Python 2.2. (Actually you can
also change this by setting a module-level variable named
‘__metaclass__’ — see PEP 253(1) for the details — but it’s easier to
just subclass *note object: 2b0.)
The type objects for the built-in types are available as built-ins,
named using a clever trick. Python has always had built-in functions
named *note int(): 184, *note float(): 187, and *note str(): 330. In
2.2, they aren’t functions any more, but type objects that behave as
factories when called.
>>> int
>>> int('123')
123
To make the set of types complete, new type objects such as *note
dict(): 1b8. and ‘file()’ have been added. Here’s a more interesting
example, adding a ‘lock()’ method to file objects:
class LockableFile(file):
def lock (self, operation, length=0, start=0, whence=0):
import fcntl
return fcntl.lockf(self.fileno(), operation,
length, start, whence)
The now-obsolete ‘posixfile’ module contained a class that emulated all
of a file object’s methods and also added a ‘lock()’ method, but this
class couldn’t be passed to internal functions that expected a built-in
file, something which is possible with our new ‘LockableFile’.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0253
File: python.info, Node: Descriptors, Next: Multiple Inheritance The Diamond Rule, Prev: Old and New Classes, Up: PEPs 252 and 253 Type and Class Changes
1.15.2.2 Descriptors
....................
In previous versions of Python, there was no consistent way to discover
what attributes and methods were supported by an object. There were
some informal conventions, such as defining ‘__members__’ and
‘__methods__’ attributes that were lists of names, but often the author
of an extension type or a class wouldn’t bother to define them. You
could fall back on inspecting the *note __dict__: 4fc. of an object, but
when class inheritance or an arbitrary *note __getattr__(): 31a. hook
were in use this could still be inaccurate.
The one big idea underlying the new class model is that an API for
describing the attributes of an object using `descriptors' has been
formalized. Descriptors specify the value of an attribute, stating
whether it’s a method or a field. With the descriptor API, static
methods and class methods become possible, as well as more exotic
constructs.
Attribute descriptors are objects that live inside class objects, and
have a few attributes of their own:
* *note __name__: c67. is the attribute’s name.
* ‘__doc__’ is the attribute’s docstring.
* ‘__get__(object)’ is a method that retrieves the attribute value
from `object'.
* ‘__set__(object, value)’ sets the attribute on `object' to `value'.
* ‘__delete__(object, value)’ deletes the `value' attribute of
`object'.
For example, when you write ‘obj.x’, the steps that Python actually
performs are:
descriptor = obj.__class__.x
descriptor.__get__(obj)
For methods, ‘descriptor.__get__()’ returns a temporary object that’s
callable, and wraps up the instance and the method to be called on it.
This is also why static methods and class methods are now possible; they
have descriptors that wrap up just the method, or the method and the
class. As a brief explanation of these new kinds of methods, static
methods aren’t passed the instance, and therefore resemble regular
functions. Class methods are passed the class of the object, but not
the object itself. Static and class methods are defined like this:
class C(object):
def f(arg1, arg2):
...
f = staticmethod(f)
def g(cls, arg1, arg2):
...
g = classmethod(g)
The *note staticmethod(): 1d9. function takes the function ‘f()’, and
returns it wrapped up in a descriptor so it can be stored in the class
object. You might expect there to be special syntax for creating such
methods (‘def static f’, ‘defstatic f()’, or something like that) but no
such syntax has been defined yet; that’s been left for future versions
of Python.
More new features, such as slots and properties, are also implemented as
new kinds of descriptors, and it’s not difficult to write a descriptor
class that does something novel. For example, it would be possible to
write a descriptor class that made it possible to write Eiffel-style
preconditions and postconditions for a method. A class that used this
feature might be defined like this:
from eiffel import eiffelmethod
class C(object):
def f(self, arg1, arg2):
# The actual function
...
def pre_f(self):
# Check preconditions
...
def post_f(self):
# Check postconditions
...
f = eiffelmethod(f, pre_f, post_f)
Note that a person using the new ‘eiffelmethod()’ doesn’t have to
understand anything about descriptors. This is why I think the new
features don’t increase the basic complexity of the language. There
will be a few wizards who need to know about it in order to write
‘eiffelmethod()’ or the ZODB or whatever, but most users will just write
code on top of the resulting libraries and ignore the implementation
details.
File: python.info, Node: Multiple Inheritance The Diamond Rule, Next: Attribute Access, Prev: Descriptors, Up: PEPs 252 and 253 Type and Class Changes
1.15.2.3 Multiple Inheritance: The Diamond Rule
...............................................
Multiple inheritance has also been made more useful through changing the
rules under which names are resolved. Consider this set of classes
(diagram taken from PEP 253(1) by Guido van Rossum):
class A:
^ ^ def save(self): ...
/ \
/ \
/ \
/ \
class B class C:
^ ^ def save(self): ...
\ /
\ /
\ /
\ /
class D
The lookup rule for classic classes is simple but not very smart; the
base classes are searched depth-first, going from left to right. A
reference to ‘D.save()’ will search the classes ‘D’, ‘B’, and then ‘A’,
where ‘save()’ would be found and returned. ‘C.save()’ would never be
found at all. This is bad, because if ‘C’’s ‘save()’ method is saving
some internal state specific to ‘C’, not calling it will result in that
state never getting saved.
New-style classes follow a different algorithm that’s a bit more
complicated to explain, but does the right thing in this situation.
(Note that Python 2.3 changes this algorithm to one that produces the
same results in most cases, but produces more useful results for really
complicated inheritance graphs.)
1. List all the base classes, following the classic lookup rule and
include a class multiple times if it’s visited repeatedly. In the
above example, the list of visited classes is [‘D’, ‘B’, ‘A’, ‘C’,
‘A’].
2. Scan the list for duplicated classes. If any are found, remove all
but one occurrence, leaving the `last' one in the list. In the
above example, the list becomes [‘D’, ‘B’, ‘C’, ‘A’] after dropping
duplicates.
Following this rule, referring to ‘D.save()’ will return ‘C.save()’,
which is the behaviour we’re after. This lookup rule is the same as the
one followed by Common Lisp. A new built-in function, *note super():
4e2, provides a way to get at a class’s superclasses without having to
reimplement Python’s algorithm. The most commonly used form will be
‘super(class, obj)’, which returns a bound superclass object (not the
actual class object). This form will be used in methods to call a
method in the superclass; for example, ‘D’’s ‘save()’ method would look
like this:
class D (B,C):
def save (self):
# Call superclass .save()
super(D, self).save()
# Save D's private information here
...
*note super(): 4e2. can also return unbound superclass objects when
called as ‘super(class)’ or ‘super(class1, class2)’, but this probably
won’t often be useful.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0253
File: python.info, Node: Attribute Access, Next: Related Links, Prev: Multiple Inheritance The Diamond Rule, Up: PEPs 252 and 253 Type and Class Changes
1.15.2.4 Attribute Access
.........................
A fair number of sophisticated Python classes define hooks for attribute
access using *note __getattr__(): 31a.; most commonly this is done for
convenience, to make code more readable by automatically mapping an
attribute access such as ‘obj.parent’ into a method call such as
‘obj.get_parent’. Python 2.2 adds some new ways of controlling
attribute access.
First, ‘__getattr__(attr_name)’ is still supported by new-style classes,
and nothing about it has changed. As before, it will be called when an
attempt is made to access ‘obj.foo’ and no attribute named ‘foo’ is
found in the instance’s dictionary.
New-style classes also support a new method,
‘__getattribute__(attr_name)’. The difference between the two methods
is that *note __getattribute__(): 449. is `always' called whenever any
attribute is accessed, while the old *note __getattr__(): 31a. is only
called if ‘foo’ isn’t found in the instance’s dictionary.
However, Python 2.2’s support for `properties' will often be a simpler
way to trap attribute references. Writing a *note __getattr__(): 31a.
method is complicated because to avoid recursion you can’t use regular
attribute accesses inside them, and instead have to mess around with the
contents of *note __dict__: 4fc. *note __getattr__(): 31a. methods also
end up being called by Python when it checks for other methods such as
*note __repr__(): 33e. or ‘__coerce__()’, and so have to be written with
this in mind. Finally, calling a function on every attribute access
results in a sizable performance loss.
*note property: 1d7. is a new built-in type that packages up three
functions that get, set, or delete an attribute, and a docstring. For
example, if you want to define a ‘size’ attribute that’s computed, but
also settable, you could write:
class C(object):
def get_size (self):
result = ... computation ...
return result
def set_size (self, size):
... compute something based on the size
and set internal state appropriately ...
# Define a property. The 'delete this attribute'
# method is defined as None, so the attribute
# can't be deleted.
size = property(get_size, set_size,
None,
"Storage size of this instance")
That is certainly clearer and easier to write than a pair of *note
__getattr__(): 31a./*note __setattr__(): e2c. methods that check for the
‘size’ attribute and handle it specially while retrieving all other
attributes from the instance’s *note __dict__: 4fc. Accesses to ‘size’
are also the only ones which have to perform the work of calling a
function, so references to other attributes run at their usual speed.
Finally, it’s possible to constrain the list of attributes that can be
referenced on an object using the new *note __slots__: e2d. class
attribute. Python objects are usually very dynamic; at any time it’s
possible to define a new attribute on an instance by just doing
‘obj.new_attr=1’. A new-style class can define a class attribute named
*note __slots__: e2d. to limit the legal attributes to a particular set
of names. An example will make this clear:
>>> class C(object):
... __slots__ = ('template', 'name')
...
>>> obj = C()
>>> print obj.template
None
>>> obj.template = 'Test'
>>> print obj.template
Test
>>> obj.newattr = None
Traceback (most recent call last):
File "", line 1, in ?
AttributeError: 'C' object has no attribute 'newattr'
Note how you get an *note AttributeError: 39b. on the attempt to assign
to an attribute not listed in *note __slots__: e2d.
File: python.info, Node: Related Links, Prev: Attribute Access, Up: PEPs 252 and 253 Type and Class Changes
1.15.2.5 Related Links
......................
This section has just been a quick overview of the new features, giving
enough of an explanation to start you programming, but many details have
been simplified or ignored. Where should you go to get a more complete
picture?
‘https://docs.python.org/dev/howto/descriptor.html’ is a lengthy
tutorial introduction to the descriptor features, written by Guido van
Rossum. If my description has whetted your appetite, go read this
tutorial next, because it goes into much more detail about the new
features while still remaining quite easy to read.
Next, there are two relevant PEPs, PEP 252(1) and PEP 253(2). PEP
252(3) is titled “Making Types Look More Like Classes”, and covers the
descriptor API. PEP 253(4) is titled “Subtyping Built-in Types”, and
describes the changes to type objects that make it possible to subtype
built-in objects. PEP 253(5) is the more complicated PEP of the two,
and at a few points the necessary explanations of types and meta-types
may cause your head to explode. Both PEPs were written and implemented
by Guido van Rossum, with substantial assistance from the rest of the
Zope Corp. team.
Finally, there’s the ultimate authority: the source code. Most of the
machinery for the type handling is in ‘Objects/typeobject.c’, but you
should only resort to it after all other avenues have been exhausted,
including posting a question to python-list or python-dev.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0252
(2) https://www.python.org/dev/peps/pep-0253
(3) https://www.python.org/dev/peps/pep-0252
(4) https://www.python.org/dev/peps/pep-0253
(5) https://www.python.org/dev/peps/pep-0253
File: python.info, Node: PEP 234 Iterators, Next: PEP 255 Simple Generators<2>, Prev: PEPs 252 and 253 Type and Class Changes, Up: What’s New in Python 2 2
1.15.3 PEP 234: Iterators
-------------------------
Another significant addition to 2.2 is an iteration interface at both
the C and Python levels. Objects can define how they can be looped over
by callers.
In Python versions up to 2.1, the usual way to make ‘for item in obj’
work is to define a *note __getitem__(): 27c. method that looks
something like this:
def __getitem__(self, index):
return
*note __getitem__(): 27c. is more properly used to define an indexing
operation on an object so that you can write ‘obj[5]’ to retrieve the
sixth element. It’s a bit misleading when you’re using this only to
support *note for: c30. loops. Consider some file-like object that
wants to be looped over; the `index' parameter is essentially
meaningless, as the class probably assumes that a series of *note
__getitem__(): 27c. calls will be made with `index' incrementing by one
each time. In other words, the presence of the *note __getitem__():
27c. method doesn’t mean that using ‘file[5]’ to randomly access the
sixth element will work, though it really should.
In Python 2.2, iteration can be implemented separately, and *note
__getitem__(): 27c. methods can be limited to classes that really do
support random access. The basic idea of iterators is simple. A new
built-in function, ‘iter(obj)’ or ‘iter(C, sentinel)’, is used to get an
iterator. ‘iter(obj)’ returns an iterator for the object `obj', while
‘iter(C, sentinel)’ returns an iterator that will invoke the callable
object `C' until it returns `sentinel' to signal that the iterator is
done.
Python classes can define an *note __iter__(): 50f. method, which should
create and return a new iterator for the object; if the object is its
own iterator, this method can just return ‘self’. In particular,
iterators will usually be their own iterators. Extension types
implemented in C can implement a *note tp_iter: e30. function in order
to return an iterator, and extension types that want to behave as
iterators can define a *note tp_iternext: e31. function.
So, after all this, what do iterators actually do? They have one
required method, *note next(): 682, which takes no arguments and returns
the next value. When there are no more values to be returned, calling
*note next(): 682. should raise the *note StopIteration: 486. exception.
>>> L = [1,2,3]
>>> i = iter(L)
>>> print i
>>> i.next()
1
>>> i.next()
2
>>> i.next()
3
>>> i.next()
Traceback (most recent call last):
File "", line 1, in ?
StopIteration
>>>
In 2.2, Python’s *note for: c30. statement no longer expects a sequence;
it expects something for which *note iter(): d27. will return an
iterator. For backward compatibility and convenience, an iterator is
automatically constructed for sequences that don’t implement *note
__iter__(): 50f. or a *note tp_iter: e30. slot, so ‘for i in [1,2,3]’
will still work. Wherever the Python interpreter loops over a sequence,
it’s been changed to use the iterator protocol. This means you can do
things like this:
>>> L = [1,2,3]
>>> i = iter(L)
>>> a,b,c = i
>>> a,b,c
(1, 2, 3)
Iterator support has been added to some of Python’s basic types.
Calling *note iter(): d27. on a dictionary will return an iterator which
loops over its keys:
>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
>>> for key in m: print key, m[key]
...
Mar 3
Feb 2
Aug 8
Sep 9
May 5
Jun 6
Jul 7
Jan 1
Apr 4
Nov 11
Dec 12
Oct 10
That’s just the default behaviour. If you want to iterate over keys,
values, or key/value pairs, you can explicitly call the ‘iterkeys()’,
‘itervalues()’, or ‘iteritems()’ methods to get an appropriate iterator.
In a minor related change, the *note in: 7a3. operator now works on
dictionaries, so ‘key in dict’ is now equivalent to ‘dict.has_key(key)’.
Files also provide an iterator, which calls the *note readline(): de.
method until there are no more lines in the file. This means you can
now read each line of a file using code like this:
for line in file:
# do something for each line
...
Note that you can only go forward in an iterator; there’s no way to get
the previous element, reset the iterator, or make a copy of it. An
iterator object could provide such additional capabilities, but the
iterator protocol only requires a *note next(): 682. method.
See also
........
PEP 234(1) - Iterators
Written by Ka-Ping Yee and GvR; implemented by the Python Labs
crew, mostly by GvR and Tim Peters.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0234
File: python.info, Node: PEP 255 Simple Generators<2>, Next: PEP 237 Unifying Long Integers and Integers<2>, Prev: PEP 234 Iterators, Up: What’s New in Python 2 2
1.15.4 PEP 255: Simple Generators
---------------------------------
Generators are another new feature, one that interacts with the
introduction of iterators.
You’re doubtless familiar with how function calls work in Python or C.
When you call a function, it gets a private namespace where its local
variables are created. When the function reaches a *note return: 190.
statement, the local variables are destroyed and the resulting value is
returned to the caller. A later call to the same function will get a
fresh new set of local variables. But, what if the local variables
weren’t thrown away on exiting a function? What if you could later
resume the function where it left off? This is what generators provide;
they can be thought of as resumable functions.
Here’s the simplest example of a generator function:
def generate_ints(N):
for i in range(N):
yield i
A new keyword, *note yield: 18f, was introduced for generators. Any
function containing a ‘yield’ statement is a generator function; this is
detected by Python’s bytecode compiler which compiles the function
specially as a result. Because a new keyword was introduced, generators
must be explicitly enabled in a module by including a ‘from __future__
import generators’ statement near the top of the module’s source code.
In Python 2.3 this statement will become unnecessary.
When you call a generator function, it doesn’t return a single value;
instead it returns a generator object that supports the iterator
protocol. On executing the *note yield: 18f. statement, the generator
outputs the value of ‘i’, similar to a *note return: 190. statement.
The big difference between ‘yield’ and a ‘return’ statement is that on
reaching a ‘yield’ the generator’s state of execution is suspended and
local variables are preserved. On the next call to the generator’s
‘next()’ method, the function will resume executing immediately after
the ‘yield’ statement. (For complicated reasons, the ‘yield’ statement
isn’t allowed inside the ‘try’ block of a *note try: d72.…*note finally:
182. statement; read PEP 255(1) for a full explanation of the
interaction between ‘yield’ and exceptions.)
Here’s a sample usage of the ‘generate_ints()’ generator:
>>> gen = generate_ints(3)
>>> gen
>>> gen.next()
0
>>> gen.next()
1
>>> gen.next()
2
>>> gen.next()
Traceback (most recent call last):
File "", line 1, in ?
File "", line 2, in generate_ints
StopIteration
You could equally write ‘for i in generate_ints(5)’, or ‘a,b,c =
generate_ints(3)’.
Inside a generator function, the *note return: 190. statement can only
be used without a value, and signals the end of the procession of
values; afterwards the generator cannot return any further values.
‘return’ with a value, such as ‘return 5’, is a syntax error inside a
generator function. The end of the generator’s results can also be
indicated by raising *note StopIteration: 486. manually, or by just
letting the flow of execution fall off the bottom of the function.
You could achieve the effect of generators manually by writing your own
class and storing all the local variables of the generator as instance
variables. For example, returning a list of integers could be done by
setting ‘self.count’ to 0, and having the *note next(): 682. method
increment ‘self.count’ and return it. However, for a moderately
complicated generator, writing a corresponding class would be much
messier. ‘Lib/test/test_generators.py’ contains a number of more
interesting examples. The simplest one implements an in-order traversal
of a tree using generators recursively.
# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
if t:
for x in inorder(t.left):
yield x
yield t.label
for x in inorder(t.right):
yield x
Two other examples in ‘Lib/test/test_generators.py’ produce solutions
for the N-Queens problem (placing $N$ queens on an $NxN$ chess board so
that no queen threatens another) and the Knight’s Tour (a route that
takes a knight to every square of an $NxN$ chessboard without visiting
any square twice).
The idea of generators comes from other programming languages,
especially Icon (‘https://www.cs.arizona.edu/icon/’), where the idea of
generators is central. In Icon, every expression and function call
behaves like a generator. One example from “An Overview of the Icon
Programming Language” at
‘https://www.cs.arizona.edu/icon/docs/ipd266.htm’ gives an idea of what
this looks like:
sentence := "Store it in the neighboring harbor"
if (i := find("or", sentence)) > 5 then write(i)
In Icon the ‘find()’ function returns the indexes at which the substring
“or” is found: 3, 23, 33. In the *note if: de7. statement, ‘i’ is first
assigned a value of 3, but 3 is less than 5, so the comparison fails,
and Icon retries it with the second value of 23. 23 is greater than 5,
so the comparison now succeeds, and the code prints the value 23 to the
screen.
Python doesn’t go nearly as far as Icon in adopting generators as a
central concept. Generators are considered a new part of the core
Python language, but learning or using them isn’t compulsory; if they
don’t solve any problems that you have, feel free to ignore them. One
novel feature of Python’s interface as compared to Icon’s is that a
generator’s state is represented as a concrete object (the iterator)
that can be passed around to other functions or stored in a data
structure.
See also
........
PEP 255(2) - Simple Generators
Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland.
Implemented mostly by Neil Schemenauer and Tim Peters, with other
fixes from the Python Labs crew.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0255
(2) https://www.python.org/dev/peps/pep-0255
File: python.info, Node: PEP 237 Unifying Long Integers and Integers<2>, Next: PEP 238 Changing the Division Operator, Prev: PEP 255 Simple Generators<2>, Up: What’s New in Python 2 2
1.15.5 PEP 237: Unifying Long Integers and Integers
---------------------------------------------------
In recent versions, the distinction between regular integers, which are
32-bit values on most machines, and long integers, which can be of
arbitrary size, was becoming an annoyance. For example, on platforms
that support files larger than ‘2**32’ bytes, the ‘tell()’ method of
file objects has to return a long integer. However, there were various
bits of Python that expected plain integers and would raise an error if
a long integer was provided instead. For example, in Python 1.5, only
regular integers could be used as a slice index, and ‘'abc'[1L:]’ would
raise a *note TypeError: 192. exception with the message ‘slice index
must be int’.
Python 2.2 will shift values from short to long integers as required.
The ‘L’ suffix is no longer needed to indicate a long integer literal,
as now the compiler will choose the appropriate type. (Using the ‘L’
suffix will be discouraged in future 2.x versions of Python, triggering
a warning in Python 2.4, and probably dropped in Python 3.0.) Many
operations that used to raise an *note OverflowError: 960. will now
return a long integer as their result. For example:
>>> 1234567890123
1234567890123L
>>> 2 ** 64
18446744073709551616L
In most cases, integers and long integers will now be treated
identically. You can still distinguish them with the *note type(): 608.
built-in function, but that’s rarely needed.
See also
........
PEP 237(1) - Unifying Long Integers and Integers
Written by Moshe Zadka and Guido van Rossum. Implemented mostly by
Guido van Rossum.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0237
File: python.info, Node: PEP 238 Changing the Division Operator, Next: Unicode Changes, Prev: PEP 237 Unifying Long Integers and Integers<2>, Up: What’s New in Python 2 2
1.15.6 PEP 238: Changing the Division Operator
----------------------------------------------
The most controversial change in Python 2.2 heralds the start of an
effort to fix an old design flaw that’s been in Python from the
beginning. Currently Python’s division operator, ‘/’, behaves like C’s
division operator when presented with two integer arguments: it returns
an integer result that’s truncated down when there would be a fractional
part. For example, ‘3/2’ is 1, not 1.5, and ‘(-1)/2’ is -1, not -0.5.
This means that the results of division can vary unexpectedly depending
on the type of the two operands and because Python is dynamically typed,
it can be difficult to determine the possible types of the operands.
(The controversy is over whether this is `really' a design flaw, and
whether it’s worth breaking existing code to fix this. It’s caused
endless discussions on python-dev, and in July 2001 erupted into a storm
of acidly sarcastic postings on ‘comp.lang.python’. I won’t argue for
either side here and will stick to describing what’s implemented in 2.2.
Read PEP 238(1) for a summary of arguments and counter-arguments.)
Because this change might break code, it’s being introduced very
gradually. Python 2.2 begins the transition, but the switch won’t be
complete until Python 3.0.
First, I’ll borrow some terminology from PEP 238(2). “True division” is
the division that most non-programmers are familiar with: 3/2 is 1.5,
1/4 is 0.25, and so forth. “Floor division” is what Python’s ‘/’
operator currently does when given integer operands; the result is the
floor of the value returned by true division. “Classic division” is the
current mixed behaviour of ‘/’; it returns the result of floor division
when the operands are integers, and returns the result of true division
when one of the operands is a floating-point number.
Here are the changes 2.2 introduces:
* A new operator, ‘//’, is the floor division operator. (Yes, we
know it looks like C++’s comment symbol.) ‘//’ `always' performs
floor division no matter what the types of its operands are, so ‘1
// 2’ is 0 and ‘1.0 // 2.0’ is also 0.0.
‘//’ is always available in Python 2.2; you don’t need to enable it
using a ‘__future__’ statement.
* By including a ‘from __future__ import division’ in a module, the
‘/’ operator will be changed to return the result of true division,
so ‘1/2’ is 0.5. Without the ‘__future__’ statement, ‘/’ still
means classic division. The default meaning of ‘/’ will not change
until Python 3.0.
* Classes can define methods called *note __truediv__(): 786. and
*note __floordiv__(): e35. to overload the two division operators.
At the C level, there are also slots in the *note PyNumberMethods:
d81. structure so extension types can define the two operators.
* Python 2.2 supports some command-line arguments for testing whether
code will work with the changed division semantics. Running python
with ‘-Q warn’ will cause a warning to be issued whenever division
is applied to two integers. You can use this to find code that’s
affected by the change and fix it. By default, Python 2.2 will
simply perform classic division without a warning; the warning will
be turned on by default in Python 2.3.
See also
........
PEP 238(3) - Changing the Division Operator
Written by Moshe Zadka and Guido van Rossum. Implemented by Guido
van Rossum..
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0238
(2) https://www.python.org/dev/peps/pep-0238
(3) https://www.python.org/dev/peps/pep-0238
File: python.info, Node: Unicode Changes, Next: PEP 227 Nested Scopes, Prev: PEP 238 Changing the Division Operator, Up: What’s New in Python 2 2
1.15.7 Unicode Changes
----------------------
Python’s Unicode support has been enhanced a bit in 2.2. Unicode
strings are usually stored as UCS-2, as 16-bit unsigned integers.
Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned integers,
as its internal encoding by supplying ‘--enable-unicode=ucs4’ to the
configure script. (It’s also possible to specify ‘--disable-unicode’ to
completely disable Unicode support.)
When built to use UCS-4 (a “wide Python”), the interpreter can natively
handle Unicode characters from U+000000 to U+110000, so the range of
legal values for the ‘unichr()’ function is expanded accordingly. Using
an interpreter compiled to use UCS-2 (a “narrow Python”), values greater
than 65535 will still cause ‘unichr()’ to raise a *note ValueError: 1fb.
exception. This is all described in PEP 261(1), “Support for ‘wide’
Unicode characters”; consult it for further details.
Another change is simpler to explain. Since their introduction, Unicode
strings have supported an ‘encode()’ method to convert the string to a
selected encoding such as UTF-8 or Latin-1. A symmetric
‘decode([*encoding*])’ method has been added to 8-bit strings (though
not to Unicode strings) in 2.2. ‘decode()’ assumes that the string is
in the specified encoding and decodes it, returning whatever is returned
by the codec.
Using this new feature, codecs have been added for tasks not directly
related to Unicode. For example, codecs have been added for
uu-encoding, MIME’s base64 encoding, and compression with the *note
zlib: 144. module:
>>> s = """Here is a lengthy piece of redundant, overly verbose,
... and repetitive text.
... """
>>> data = s.encode('zlib')
>>> data
'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...'
>>> data.decode('zlib')
'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n'
>>> print s.encode('uu')
begin 666
M2&5R92!I=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X*
end
>>> "sheesh".encode('rot-13')
'furrfu'
To convert a class instance to Unicode, a ‘__unicode__()’ method can be
defined by a class, analogous to *note __str__(): e37.
‘encode()’, ‘decode()’, and ‘__unicode__()’ were implemented by
Marc-André Lemburg. The changes to support using UCS-4 internally were
implemented by Fredrik Lundh and Martin von Löwis.
See also
........
PEP 261(2) - Support for ‘wide’ Unicode characters
Written by Paul Prescod.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0261
(2) https://www.python.org/dev/peps/pep-0261
File: python.info, Node: PEP 227 Nested Scopes, Next: New and Improved Modules<3>, Prev: Unicode Changes, Up: What’s New in Python 2 2
1.15.8 PEP 227: Nested Scopes
-----------------------------
In Python 2.1, statically nested scopes were added as an optional
feature, to be enabled by a ‘from __future__ import nested_scopes’
directive. In 2.2 nested scopes no longer need to be specially enabled,
and are now always present. The rest of this section is a copy of the
description of nested scopes from my “What’s New in Python 2.1”
document; if you read it when 2.1 came out, you can skip the rest of
this section.
The largest change introduced in Python 2.1, and made complete in 2.2,
is to Python’s scoping rules. In Python 2.0, at any given time there
are at most three namespaces used to look up variable names: local,
module-level, and the built-in namespace. This often surprised people
because it didn’t match their intuitive expectations. For example, a
nested recursive function definition doesn’t work:
def f():
...
def g(value):
...
return g(value-1) + 1
...
The function ‘g()’ will always raise a *note NameError: d7b. exception,
because the binding of the name ‘g’ isn’t in either its local namespace
or in the module-level namespace. This isn’t much of a problem in
practice (how often do you recursively define interior functions like
this?), but this also made using the *note lambda: c2f. expression
clumsier, and this was a problem in practice. In code which uses
‘lambda’ you can often find local variables being copied by passing them
as the default values of arguments.
def find(self, name):
"Return list of any entries equal to 'name'"
L = filter(lambda x, name=name: x == name,
self.list_attribute)
return L
The readability of Python code written in a strongly functional style
suffers greatly as a result.
The most significant change to Python 2.2 is that static scoping has
been added to the language to fix this problem. As a first effect, the
‘name=name’ default argument is now unnecessary in the above example.
Put simply, when a given variable name is not assigned a value within a
function (by an assignment, or the *note def: dbe, *note class: c6a, or
*note import: c1d. statements), references to the variable will be
looked up in the local namespace of the enclosing scope. A more
detailed explanation of the rules, and a dissection of the
implementation, can be found in the PEP.
This change may cause some compatibility problems for code where the
same variable name is used both at the module level and as a local
variable within a function that contains further function definitions.
This seems rather unlikely though, since such code would have been
pretty confusing to read in the first place.
One side effect of the change is that the ‘from module import *’ and
‘exec’ statements have been made illegal inside a function scope under
certain conditions. The Python reference manual has said all along that
‘from module import *’ is only legal at the top level of a module, but
the CPython interpreter has never enforced this before. As part of the
implementation of nested scopes, the compiler which turns Python source
into bytecodes has to generate different code to access variables in a
containing scope. ‘from module import *’ and ‘exec’ make it impossible
for the compiler to figure this out, because they add names to the local
namespace that are unknowable at compile time. Therefore, if a function
contains function definitions or *note lambda: c2f. expressions with
free variables, the compiler will flag this by raising a *note
SyntaxError: 458. exception.
To make the preceding explanation a bit clearer, here’s an example:
x = 1
def f():
# The next line is a syntax error
exec 'x=2'
def g():
return x
Line 4 containing the ‘exec’ statement is a syntax error, since ‘exec’
would define a new local variable named ‘x’ whose value should be
accessed by ‘g()’.
This shouldn’t be much of a limitation, since ‘exec’ is rarely used in
most Python code (and when it is used, it’s often a sign of a poor
design anyway).
See also
........
PEP 227(1) - Statically Nested Scopes
Written and implemented by Jeremy Hylton.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0227
File: python.info, Node: New and Improved Modules<3>, Next: Interpreter Changes and Fixes, Prev: PEP 227 Nested Scopes, Up: What’s New in Python 2 2
1.15.9 New and Improved Modules
-------------------------------
* The ‘xmlrpclib’ module was contributed to the standard library by
Fredrik Lundh, providing support for writing XML-RPC clients.
XML-RPC is a simple remote procedure call protocol built on top of
HTTP and XML. For example, the following snippet retrieves a list
of RSS channels from the O’Reilly Network, and then lists the
recent headlines for one channel:
import xmlrpclib
s = xmlrpclib.Server(
'http://www.oreillynet.com/meerkat/xml-rpc/server.php')
channels = s.meerkat.getChannels()
# channels is a list of dictionaries, like this:
# [{'id': 4, 'title': 'Freshmeat Daily News'}
# {'id': 190, 'title': '32Bits Online'},
# {'id': 4549, 'title': '3DGamers'}, ... ]
# Get the items for one channel
items = s.meerkat.getItems( {'channel': 4} )
# 'items' is another list of dictionaries, like this:
# [{'link': 'http://freshmeat.net/releases/52719/',
# 'description': 'A utility which converts HTML to XSL FO.',
# 'title': 'html2fo 0.3 (Default)'}, ... ]
The ‘SimpleXMLRPCServer’ module makes it easy to create
straightforward XML-RPC servers. See
‘http://xmlrpc.scripting.com/’ for more information about XML-RPC.
* The new *note hmac: 8f. module implements the HMAC algorithm
described by RFC 2104(1). (Contributed by Gerhard Häring.)
* Several functions that originally returned lengthy tuples now
return pseudo-sequences that still behave like tuples but also have
mnemonic attributes such as memberst_mtime or ‘tm_year’. The
enhanced functions include *note stat(): f4, ‘fstat()’,
‘statvfs()’, and ‘fstatvfs()’ in the *note os: c4. module, and
‘localtime()’, ‘gmtime()’, and ‘strptime()’ in the *note time: 10a.
module.
For example, to obtain a file’s size using the old tuples, you’d
end up writing something like ‘file_size =
os.stat(filename)[stat.ST_SIZE]’, but now this can be written more
clearly as ‘file_size = os.stat(filename).st_size’.
The original patch for this feature was contributed by Nick
Mathewson.
* The Python profiler has been extensively reworked and various
errors in its output have been corrected. (Contributed by Fred L.
Drake, Jr. and Tim Peters.)
* The *note socket: ef. module can be compiled to support IPv6;
specify the ‘--enable-ipv6’ option to Python’s configure script.
(Contributed by Jun-ichiro “itojun” Hagino.)
* Two new format characters were added to the *note struct: f8.
module for 64-bit integers on platforms that support the C ‘long
long’ type. ‘q’ is for a signed 64-bit integer, and ‘Q’ is for an
unsigned one. The value is returned in Python’s long integer type.
(Contributed by Tim Peters.)
* In the interpreter’s interactive mode, there’s a new built-in
function *note help(): 572. that uses the *note pydoc: d9. module
introduced in Python 2.1 to provide interactive help.
‘help(object)’ displays any available help text about `object'.
*note help(): 572. with no argument puts you in an online help
utility, where you can enter the names of functions, classes, or
modules to read their help text. (Contributed by Guido van Rossum,
using Ka-Ping Yee’s *note pydoc: d9. module.)
* Various bugfixes and performance improvements have been made to the
SRE engine underlying the *note re: dd. module. For example, the
*note re.sub(): 47b. and *note re.split(): 3ca. functions have been
rewritten in C. Another contributed patch speeds up certain Unicode
character ranges by a factor of two, and a new ‘finditer()’ method
that returns an iterator over all the non-overlapping matches in a
given string. (SRE is maintained by Fredrik Lundh. The BIGCHARSET
patch was contributed by Martin von Löwis.)
* The *note smtplib: ed. module now supports RFC 2487(2), “Secure
SMTP over TLS”, so it’s now possible to encrypt the SMTP traffic
between a Python program and the mail transport agent being handed
a message. *note smtplib: ed. also supports SMTP authentication.
(Contributed by Gerhard Häring.)
* The *note imaplib: 98. module, maintained by Piers Lauder, has
support for several new extensions: the NAMESPACE extension defined
in RFC 2342(3), SORT, GETACL and SETACL. (Contributed by Anthony
Baxter and Michel Pelletier.)
* The ‘rfc822’ module’s parsing of email addresses is now compliant
with RFC 2822(4), an update to RFC 822(5). (The module’s name is
`not' going to be changed to ‘rfc2822’.) A new package, *note
email: 69, has also been added for parsing and generating e-mail
messages. (Contributed by Barry Warsaw, and arising out of his
work on Mailman.)
* The *note difflib: 37. module now contains a new ‘Differ’ class for
producing human-readable lists of changes (a “delta”) between two
sequences of lines of text. There are also two generator
functions, ‘ndiff()’ and ‘restore()’, which respectively return a
delta from two sequences, or one of the original sequences from a
delta. (Grunt work contributed by David Goodger, from ndiff.py
code by Tim Peters who then did the generatorization.)
* New constants ‘ascii_letters’, ‘ascii_lowercase’, and
‘ascii_uppercase’ were added to the *note string: f6. module.
There were several modules in the standard library that used
‘string.letters’ to mean the ranges A-Za-z, but that assumption is
incorrect when locales are in use, because ‘string.letters’ varies
depending on the set of legal characters defined by the current
locale. The buggy modules have all been fixed to use
‘ascii_letters’ instead. (Reported by an unknown person; fixed by
Fred L. Drake, Jr.)
* The *note mimetypes: b2. module now makes it easier to use
alternative MIME-type databases by the addition of a ‘MimeTypes’
class, which takes a list of filenames to be parsed. (Contributed
by Fred L. Drake, Jr.)
* A ‘Timer’ class was added to the *note threading: 109. module that
allows scheduling an activity to happen at some future time.
(Contributed by Itamar Shtull-Trauring.)
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc2104.html
(2) https://tools.ietf.org/html/rfc2487.html
(3) https://tools.ietf.org/html/rfc2342.html
(4) https://tools.ietf.org/html/rfc2822.html
(5) https://tools.ietf.org/html/rfc822.html
File: python.info, Node: Interpreter Changes and Fixes, Next: Other Changes and Fixes<3>, Prev: New and Improved Modules<3>, Up: What’s New in Python 2 2
1.15.10 Interpreter Changes and Fixes
-------------------------------------
Some of the changes only affect people who deal with the Python
interpreter at the C level because they’re writing Python extension
modules, embedding the interpreter, or just hacking on the interpreter
itself. If you only write Python code, none of the changes described
here will affect you very much.
* Profiling and tracing functions can now be implemented in C, which
can operate at much higher speeds than Python-based functions and
should reduce the overhead of profiling and tracing. This will be
of interest to authors of development environments for Python. Two
new C functions were added to Python’s API, *note
PyEval_SetProfile(): e3b. and *note PyEval_SetTrace(): e3c. The
existing *note sys.setprofile(): e3d. and *note sys.settrace():
e3e. functions still exist, and have simply been changed to use the
new C-level interface. (Contributed by Fred L. Drake, Jr.)
* Another low-level API, primarily of interest to implementors of
Python debuggers and development tools, was added. *note
PyInterpreterState_Head(): e3f. and *note
PyInterpreterState_Next(): e40. let a caller walk through all the
existing interpreter objects; *note
PyInterpreterState_ThreadHead(): e41. and *note
PyThreadState_Next(): e42. allow looping over all the thread states
for a given interpreter. (Contributed by David Beazley.)
* The C-level interface to the garbage collector has been changed to
make it easier to write extension types that support garbage
collection and to debug misuses of the functions. Various
functions have slightly different semantics, so a bunch of
functions had to be renamed. Extensions that use the old API will
still compile but will `not' participate in garbage collection, so
updating them for 2.2 should be considered fairly high priority.
To upgrade an extension module to the new API, perform the
following steps:
* Rename ‘Py_TPFLAGS_GC()’ to ‘PyTPFLAGS_HAVE_GC()’.
*
Use *note PyObject_GC_New(): 2d4. or *note PyObject_GC_NewVar(): 2d5. to allocate
objects, and *note PyObject_GC_Del(): e43. to deallocate them.
*
Rename ‘PyObject_GC_Init()’ to *note PyObject_GC_Track(): e44. and
‘PyObject_GC_Fini()’ to *note PyObject_GC_UnTrack(): e45.
* Remove ‘PyGC_HEAD_SIZE()’ from object size calculations.
* Remove calls to ‘PyObject_AS_GC()’ and ‘PyObject_FROM_GC()’.
* A new ‘et’ format sequence was added to *note PyArg_ParseTuple():
26a.; ‘et’ takes both a parameter and an encoding name, and
converts the parameter to the given encoding if the parameter turns
out to be a Unicode string, or leaves it alone if it’s an 8-bit
string, assuming it to already be in the desired encoding. This
differs from the ‘es’ format character, which assumes that 8-bit
strings are in Python’s default ASCII encoding and converts them to
the specified new encoding. (Contributed by M.-A. Lemburg, and
used for the MBCS support on Windows described in the following
section.)
* A different argument parsing function, *note PyArg_UnpackTuple():
e46, has been added that’s simpler and presumably faster. Instead
of specifying a format string, the caller simply gives the minimum
and maximum number of arguments expected, and a set of pointers to
*note PyObject*: 4ba. variables that will be filled in with
argument values.
* Two new flags *note METH_NOARGS: e18. and *note METH_O: e47. are
available in method definition tables to simplify implementation of
methods with no arguments or a single untyped argument. Calling
such methods is more efficient than calling a corresponding method
that uses *note METH_VARARGS: e48. Also, the old ‘METH_OLDARGS’
style of writing C methods is now officially deprecated.
* Two new wrapper functions, *note PyOS_snprintf(): e49. and *note
PyOS_vsnprintf(): e4a. were added to provide cross-platform
implementations for the relatively new ‘snprintf()’ and
‘vsnprintf()’ C lib APIs. In contrast to the standard ‘sprintf()’
and ‘vsprintf()’ functions, the Python versions check the bounds of
the buffer used to protect against buffer overruns. (Contributed
by M.-A. Lemburg.)
* The *note _PyTuple_Resize(): e4b. function has lost an unused
parameter, so now it takes 2 parameters instead of 3. The third
argument was never used, and can simply be discarded when porting
code from earlier versions to Python 2.2.
File: python.info, Node: Other Changes and Fixes<3>, Next: Acknowledgements<6>, Prev: Interpreter Changes and Fixes, Up: What’s New in Python 2 2
1.15.11 Other Changes and Fixes
-------------------------------
As usual there were a bunch of other improvements and bugfixes scattered
throughout the source tree. A search through the CVS change logs finds
there were 527 patches applied and 683 bugs fixed between Python 2.1 and
2.2; 2.2.1 applied 139 patches and fixed 143 bugs; 2.2.2 applied 106
patches and fixed 82 bugs. These figures are likely to be
underestimates.
Some of the more notable changes are:
* The code for the MacOS port for Python, maintained by Jack Jansen,
is now kept in the main Python CVS tree, and many changes have been
made to support MacOS X.
The most significant change is the ability to build Python as a
framework, enabled by supplying the ‘--enable-framework’ option to
the configure script when compiling Python. According to Jack
Jansen, “This installs a self-contained Python installation plus
the OS X framework “glue” into
‘/Library/Frameworks/Python.framework’ (or another location of
choice). For now there is little immediate added benefit to this
(actually, there is the disadvantage that you have to change your
PATH to be able to find Python), but it is the basis for creating a
full-blown Python application, porting the MacPython IDE, possibly
using Python as a standard OSA scripting language and much more.”
Most of the MacPython toolbox modules, which interface to MacOS
APIs such as windowing, QuickTime, scripting, etc. have been
ported to OS X, but they’ve been left commented out in ‘setup.py’.
People who want to experiment with these modules can uncomment them
manually.
* Keyword arguments passed to built-in functions that don’t take them
now cause a *note TypeError: 192. exception to be raised, with the
message “`function' takes no keyword arguments”.
* Weak references, added in Python 2.1 as an extension module, are
now part of the core because they’re used in the implementation of
new-style classes. The *note ReferenceError: e4d. exception has
therefore moved from the *note weakref: 128. module to become a
built-in exception.
* A new script, ‘Tools/scripts/cleanfuture.py’ by Tim Peters,
automatically removes obsolete ‘__future__’ statements from Python
source code.
* An additional `flags' argument has been added to the built-in
function *note compile(): 1b4, so the behaviour of ‘__future__’
statements can now be correctly observed in simulated shells, such
as those presented by IDLE and other development environments.
This is described in PEP 264(1). (Contributed by Michael Hudson.)
* The new license introduced with Python 1.6 wasn’t GPL-compatible.
This is fixed by some minor textual changes to the 2.2 license, so
it’s now legal to embed Python inside a GPLed program again. Note
that Python itself is not GPLed, but instead is under a license
that’s essentially equivalent to the BSD license, same as it always
was. The license changes were also applied to the Python 2.0.1 and
2.1.1 releases.
* When presented with a Unicode filename on Windows, Python will now
convert it to an MBCS encoded string, as used by the Microsoft file
APIs. As MBCS is explicitly used by the file APIs, Python’s choice
of ASCII as the default encoding turns out to be an annoyance. On
Unix, the locale’s character set is used if
‘locale.nl_langinfo(CODESET)’ is available. (Windows support was
contributed by Mark Hammond with assistance from Marc-André
Lemburg. Unix support was added by Martin von Löwis.)
* Large file support is now enabled on Windows. (Contributed by Tim
Peters.)
* The ‘Tools/scripts/ftpmirror.py’ script now parses a ‘.netrc’ file,
if you have one. (Contributed by Mike Romberg.)
* Some features of the object returned by the ‘xrange()’ function are
now deprecated, and trigger warnings when they’re accessed; they’ll
disappear in Python 2.3. ‘xrange’ objects tried to pretend they
were full sequence types by supporting slicing, sequence
multiplication, and the *note in: 7a3. operator, but these features
were rarely used and therefore buggy. The ‘tolist()’ method and
the ‘start’, ‘stop’, and ‘step’ attributes are also being
deprecated. At the C level, the fourth argument to the
‘PyRange_New()’ function, ‘repeat’, has also been deprecated.
* There were a bunch of patches to the dictionary implementation,
mostly to fix potential core dumps if a dictionary contains objects
that sneakily changed their hash value, or mutated the dictionary
they were contained in. For a while python-dev fell into a gentle
rhythm of Michael Hudson finding a case that dumped core, Tim
Peters fixing the bug, Michael finding another case, and round and
round it went.
* On Windows, Python can now be compiled with Borland C thanks to a
number of patches contributed by Stephen Hansen, though the result
isn’t fully functional yet. (But this `is' progress…)
* Another Windows enhancement: Wise Solutions generously offered
PythonLabs use of their InstallerMaster 8.1 system. Earlier
PythonLabs Windows installers used Wise 5.0a, which was beginning
to show its age. (Packaged up by Tim Peters.)
* Files ending in ‘.pyw’ can now be imported on Windows. ‘.pyw’ is a
Windows-only thing, used to indicate that a script needs to be run
using PYTHONW.EXE instead of PYTHON.EXE in order to prevent a DOS
console from popping up to display the output. This patch makes it
possible to import such scripts, in case they’re also usable as
modules. (Implemented by David Bolen.)
* On platforms where Python uses the C ‘dlopen()’ function to load
extension modules, it’s now possible to set the flags used by
‘dlopen()’ using the *note sys.getdlopenflags(): e4e. and *note
sys.setdlopenflags(): a66. functions. (Contributed by Bram Stolk.)
* The *note pow(): 19a. built-in function no longer supports 3
arguments when floating-point numbers are supplied. ‘pow(x, y, z)’
returns ‘(x**y) % z’, but this is never useful for floating point
numbers, and the final result varies unpredictably depending on the
platform. A call such as ‘pow(2.0, 8.0, 7.0)’ will now raise a
*note TypeError: 192. exception.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0264
File: python.info, Node: Acknowledgements<6>, Prev: Other Changes and Fixes<3>, Up: What’s New in Python 2 2
1.15.12 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Fred Bremmer, Keith Briggs, Andrew Dalke, Fred L. Drake, Jr.,
Carel Fellinger, David Goodger, Mark Hammond, Stephen Hansen, Michael
Hudson, Jack Jansen, Marc-André Lemburg, Martin von Löwis, Fredrik
Lundh, Michael McLay, Nick Mathewson, Paul Moore, Gustavo Niemeyer, Don
O’Donnell, Joonas Paalasma, Tim Peters, Jens Quade, Tom Reinhardt, Neil
Schemenauer, Guido van Rossum, Greg Ward, Edward Welbourne.
File: python.info, Node: What’s New in Python 2 1, Next: What’s New in Python 2 0, Prev: What’s New in Python 2 2, Up: What’s New in Python
1.16 What’s New in Python 2.1
=============================
Author: A.M. Kuchling
* Menu:
* Introduction: Introduction<2>.
* PEP 227; Nested Scopes: PEP 227 Nested Scopes<2>.
* PEP 236; __future__ Directives: PEP 236 __future__ Directives.
* PEP 207; Rich Comparisons: PEP 207 Rich Comparisons.
* PEP 230; Warning Framework: PEP 230 Warning Framework.
* PEP 229; New Build System: PEP 229 New Build System.
* PEP 205; Weak References: PEP 205 Weak References.
* PEP 232; Function Attributes: PEP 232 Function Attributes.
* PEP 235; Importing Modules on Case-Insensitive Platforms: PEP 235 Importing Modules on Case-Insensitive Platforms.
* PEP 217; Interactive Display Hook: PEP 217 Interactive Display Hook.
* PEP 208; New Coercion Model: PEP 208 New Coercion Model.
* PEP 241; Metadata in Python Packages: PEP 241 Metadata in Python Packages.
* New and Improved Modules: New and Improved Modules<4>.
* Other Changes and Fixes: Other Changes and Fixes<4>.
* Acknowledgements: Acknowledgements<7>.
File: python.info, Node: Introduction<2>, Next: PEP 227 Nested Scopes<2>, Up: What’s New in Python 2 1
1.16.1 Introduction
-------------------
This article explains the new features in Python 2.1. While there
aren’t as many changes in 2.1 as there were in Python 2.0, there are
still some pleasant surprises in store. 2.1 is the first release to be
steered through the use of Python Enhancement Proposals, or PEPs, so
most of the sizable changes have accompanying PEPs that provide more
complete documentation and a design rationale for the change. This
article doesn’t attempt to document the new features completely, but
simply provides an overview of the new features for Python programmers.
Refer to the Python 2.1 documentation, or to the specific PEP, for more
details about any new feature that particularly interests you.
One recent goal of the Python development team has been to accelerate
the pace of new releases, with a new release coming every 6 to 9 months.
2.1 is the first release to come out at this faster pace, with the first
alpha appearing in January, 3 months after the final version of 2.0 was
released.
The final release of Python 2.1 was made on April 17, 2001.
File: python.info, Node: PEP 227 Nested Scopes<2>, Next: PEP 236 __future__ Directives, Prev: Introduction<2>, Up: What’s New in Python 2 1
1.16.2 PEP 227: Nested Scopes
-----------------------------
The largest change in Python 2.1 is to Python’s scoping rules. In
Python 2.0, at any given time there are at most three namespaces used to
look up variable names: local, module-level, and the built-in namespace.
This often surprised people because it didn’t match their intuitive
expectations. For example, a nested recursive function definition
doesn’t work:
def f():
...
def g(value):
...
return g(value-1) + 1
...
The function ‘g()’ will always raise a *note NameError: d7b. exception,
because the binding of the name ‘g’ isn’t in either its local namespace
or in the module-level namespace. This isn’t much of a problem in
practice (how often do you recursively define interior functions like
this?), but this also made using the *note lambda: c2f. expression
clumsier, and this was a problem in practice. In code which uses *note
lambda: c2f. you can often find local variables being copied by passing
them as the default values of arguments.
def find(self, name):
"Return list of any entries equal to 'name'"
L = filter(lambda x, name=name: x == name,
self.list_attribute)
return L
The readability of Python code written in a strongly functional style
suffers greatly as a result.
The most significant change to Python 2.1 is that static scoping has
been added to the language to fix this problem. As a first effect, the
‘name=name’ default argument is now unnecessary in the above example.
Put simply, when a given variable name is not assigned a value within a
function (by an assignment, or the *note def: dbe, *note class: c6a, or
*note import: c1d. statements), references to the variable will be
looked up in the local namespace of the enclosing scope. A more
detailed explanation of the rules, and a dissection of the
implementation, can be found in the PEP.
This change may cause some compatibility problems for code where the
same variable name is used both at the module level and as a local
variable within a function that contains further function definitions.
This seems rather unlikely though, since such code would have been
pretty confusing to read in the first place.
One side effect of the change is that the ‘from module import *’ and
‘exec’ statements have been made illegal inside a function scope under
certain conditions. The Python reference manual has said all along that
‘from module import *’ is only legal at the top level of a module, but
the CPython interpreter has never enforced this before. As part of the
implementation of nested scopes, the compiler which turns Python source
into bytecodes has to generate different code to access variables in a
containing scope. ‘from module import *’ and ‘exec’ make it impossible
for the compiler to figure this out, because they add names to the local
namespace that are unknowable at compile time. Therefore, if a function
contains function definitions or *note lambda: c2f. expressions with
free variables, the compiler will flag this by raising a *note
SyntaxError: 458. exception.
To make the preceding explanation a bit clearer, here’s an example:
x = 1
def f():
# The next line is a syntax error
exec 'x=2'
def g():
return x
Line 4 containing the ‘exec’ statement is a syntax error, since ‘exec’
would define a new local variable named ‘x’ whose value should be
accessed by ‘g()’.
This shouldn’t be much of a limitation, since ‘exec’ is rarely used in
most Python code (and when it is used, it’s often a sign of a poor
design anyway).
Compatibility concerns have led to nested scopes being introduced
gradually; in Python 2.1, they aren’t enabled by default, but can be
turned on within a module by using a future statement as described in
PEP 236(1). (See the following section for further discussion of PEP
236(2).) In Python 2.2, nested scopes will become the default and there
will be no way to turn them off, but users will have had all of 2.1’s
lifetime to fix any breakage resulting from their introduction.
See also
........
PEP 227(3) - Statically Nested Scopes
Written and implemented by Jeremy Hylton.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0236
(2) https://www.python.org/dev/peps/pep-0236
(3) https://www.python.org/dev/peps/pep-0227
File: python.info, Node: PEP 236 __future__ Directives, Next: PEP 207 Rich Comparisons, Prev: PEP 227 Nested Scopes<2>, Up: What’s New in Python 2 1
1.16.3 PEP 236: __future__ Directives
-------------------------------------
The reaction to nested scopes was widespread concern about the dangers
of breaking code with the 2.1 release, and it was strong enough to make
the Pythoneers take a more conservative approach. This approach
consists of introducing a convention for enabling optional functionality
in release N that will become compulsory in release N+1.
The syntax uses a ‘from...import’ statement using the reserved module
name *note __future__: 0. Nested scopes can be enabled by the following
statement:
from __future__ import nested_scopes
While it looks like a normal *note import: c1d. statement, it’s not;
there are strict rules on where such a future statement can be put.
They can only be at the top of a module, and must precede any Python
code or regular ‘import’ statements. This is because such statements
can affect how the Python bytecode compiler parses code and generates
bytecode, so they must precede any statement that will result in
bytecodes being produced.
See also
........
PEP 236(1) - Back to the *note __future__: 0.
Written by Tim Peters, and primarily implemented by Jeremy Hylton.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0236
File: python.info, Node: PEP 207 Rich Comparisons, Next: PEP 230 Warning Framework, Prev: PEP 236 __future__ Directives, Up: What’s New in Python 2 1
1.16.4 PEP 207: Rich Comparisons
--------------------------------
In earlier versions, Python’s support for implementing comparisons on
user-defined classes and extension types was quite simple. Classes
could implement a ‘__cmp__()’ method that was given two instances of a
class, and could only return 0 if they were equal or +1 or -1 if they
weren’t; the method couldn’t raise an exception or return anything other
than a Boolean value. Users of Numeric Python often found this model
too weak and restrictive, because in the number-crunching programs that
numeric Python is used for, it would be more useful to be able to
perform elementwise comparisons of two matrices, returning a matrix
containing the results of a given comparison for each element. If the
two matrices are of different sizes, then the compare has to be able to
raise an exception to signal the error.
In Python 2.1, rich comparisons were added in order to support this
need. Python classes can now individually overload each of the ‘<’,
‘<=’, ‘>’, ‘>=’, ‘==’, and ‘!=’ operations. The new magic method names
are:
Operation Method name
-------------------------------------
‘<’ *note __lt__(): c34.
‘<=’ *note __le__(): ca0.
‘>’ *note __gt__(): ca1.
‘>=’ *note __ge__(): ca2.
‘==’ *note __eq__(): 33f.
‘!=’ *note __ne__(): e56.
(The magic methods are named after the corresponding Fortran operators
‘.LT.’. ‘.LE.’, &c. Numeric programmers are almost certainly quite
familiar with these names and will find them easy to remember.)
Each of these magic methods is of the form ‘method(self, other)’, where
‘self’ will be the object on the left-hand side of the operator, while
‘other’ will be the object on the right-hand side. For example, the
expression ‘A < B’ will cause ‘A.__lt__(B)’ to be called.
Each of these magic methods can return anything at all: a Boolean, a
matrix, a list, or any other Python object. Alternatively they can
raise an exception if the comparison is impossible, inconsistent, or
otherwise meaningless.
The built-in ‘cmp(A,B)’ function can use the rich comparison machinery,
and now accepts an optional argument specifying which comparison
operation to use; this is given as one of the strings ‘"<"’, ‘"<="’,
‘">"’, ‘">="’, ‘"=="’, or ‘"!="’. If called without the optional third
argument, ‘cmp()’ will only return -1, 0, or +1 as in previous versions
of Python; otherwise it will call the appropriate method and can return
any Python object.
There are also corresponding changes of interest to C programmers;
there’s a new slot ‘tp_richcmp’ in type objects and an API for
performing a given rich comparison. I won’t cover the C API here, but
will refer you to PEP 207(1), or to 2.1’s C API documentation, for the
full list of related functions.
See also
........
PEP 207(2) - Rich Comparisons
Written by Guido van Rossum, heavily based on earlier work by David
Ascher, and implemented by Guido van Rossum.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0207
(2) https://www.python.org/dev/peps/pep-0207
File: python.info, Node: PEP 230 Warning Framework, Next: PEP 229 New Build System, Prev: PEP 207 Rich Comparisons, Up: What’s New in Python 2 1
1.16.5 PEP 230: Warning Framework
---------------------------------
Over its 10 years of existence, Python has accumulated a certain number
of obsolete modules and features along the way. It’s difficult to know
when a feature is safe to remove, since there’s no way of knowing how
much code uses it — perhaps no programs depend on the feature, or
perhaps many do. To enable removing old features in a more structured
way, a warning framework was added. When the Python developers want to
get rid of a feature, it will first trigger a warning in the next
version of Python. The following Python version can then drop the
feature, and users will have had a full release cycle to remove uses of
the old feature.
Python 2.1 adds the warning framework to be used in this scheme. It
adds a *note warnings: 126. module that provide functions to issue
warnings, and to filter out warnings that you don’t want to be
displayed. Third-party modules can also use this framework to deprecate
old features that they no longer wish to support.
For example, in Python 2.1 the ‘regex’ module is deprecated, so
importing it causes a warning to be printed:
>>> import regex
__main__:1: DeprecationWarning: the regex module
is deprecated; please use the re module
>>>
Warnings can be issued by calling the *note warnings.warn(): e58.
function:
warnings.warn("feature X no longer supported")
The first parameter is the warning message; an additional optional
parameters can be used to specify a particular warning category.
Filters can be added to disable certain warnings; a regular expression
pattern can be applied to the message or to the module name in order to
suppress a warning. For example, you may have a program that uses the
‘regex’ module and not want to spare the time to convert it to use the
*note re: dd. module right now. The warning can be suppressed by
calling
import warnings
warnings.filterwarnings(action = 'ignore',
message='.*regex module is deprecated',
category=DeprecationWarning,
module = '__main__')
This adds a filter that will apply only to warnings of the class *note
DeprecationWarning: 278. triggered in the *note __main__: 1. module, and
applies a regular expression to only match the message about the ‘regex’
module being deprecated, and will cause such warnings to be ignored.
Warnings can also be printed only once, printed every time the offending
code is executed, or turned into exceptions that will cause the program
to stop (unless the exceptions are caught in the usual way, of course).
Functions were also added to Python’s C API for issuing warnings; refer
to PEP 230 or to Python’s API documentation for the details.
See also
........
PEP 5(1) - Guidelines for Language Evolution
Written by Paul Prescod, to specify procedures to be followed when
removing old features from Python. The policy described in this
PEP hasn’t been officially adopted, but the eventual policy
probably won’t be too different from Prescod’s proposal.
PEP 230(2) - Warning Framework
Written and implemented by Guido van Rossum.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0005
(2) https://www.python.org/dev/peps/pep-0230
File: python.info, Node: PEP 229 New Build System, Next: PEP 205 Weak References, Prev: PEP 230 Warning Framework, Up: What’s New in Python 2 1
1.16.6 PEP 229: New Build System
--------------------------------
When compiling Python, the user had to go in and edit the
‘Modules/Setup’ file in order to enable various additional modules; the
default set is relatively small and limited to modules that compile on
most Unix platforms. This means that on Unix platforms with many more
features, most notably Linux, Python installations often don’t contain
all useful modules they could.
Python 2.0 added the Distutils, a set of modules for distributing and
installing extensions. In Python 2.1, the Distutils are used to compile
much of the standard library of extension modules, autodetecting which
ones are supported on the current machine. It’s hoped that this will
make Python installations easier and more featureful.
Instead of having to edit the ‘Modules/Setup’ file in order to enable
modules, a ‘setup.py’ script in the top directory of the Python source
distribution is run at build time, and attempts to discover which
modules can be enabled by examining the modules and header files on the
system. If a module is configured in ‘Modules/Setup’, the ‘setup.py’
script won’t attempt to compile that module and will defer to the
‘Modules/Setup’ file’s contents. This provides a way to specific any
strange command-line flags or libraries that are required for a specific
platform.
In another far-reaching change to the build mechanism, Neil Schemenauer
restructured things so Python now uses a single makefile that isn’t
recursive, instead of makefiles in the top directory and in each of the
‘Python/’, ‘Parser/’, ‘Objects/’, and ‘Modules/’ subdirectories. This
makes building Python faster and also makes hacking the Makefiles
clearer and simpler.
See also
........
PEP 229(1) - Using Distutils to Build Python
Written and implemented by A.M. Kuchling.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0229
File: python.info, Node: PEP 205 Weak References, Next: PEP 232 Function Attributes, Prev: PEP 229 New Build System, Up: What’s New in Python 2 1
1.16.7 PEP 205: Weak References
-------------------------------
Weak references, available through the *note weakref: 128. module, are a
minor but useful new data type in the Python programmer’s toolbox.
Storing a reference to an object (say, in a dictionary or a list) has
the side effect of keeping that object alive forever. There are a few
specific cases where this behaviour is undesirable, object caches being
the most common one, and another being circular references in data
structures such as trees.
For example, consider a memoizing function that caches the results of
another function ‘f(x)’ by storing the function’s argument and its
result in a dictionary:
_cache = {}
def memoize(x):
if _cache.has_key(x):
return _cache[x]
retval = f(x)
# Cache the returned object
_cache[x] = retval
return retval
This version works for simple things such as integers, but it has a side
effect; the ‘_cache’ dictionary holds a reference to the return values,
so they’ll never be deallocated until the Python process exits and
cleans up. This isn’t very noticeable for integers, but if ‘f()’
returns an object, or a data structure that takes up a lot of memory,
this can be a problem.
Weak references provide a way to implement a cache that won’t keep
objects alive beyond their time. If an object is only accessible
through weak references, the object will be deallocated and the weak
references will now indicate that the object it referred to no longer
exists. A weak reference to an object `obj' is created by calling ‘wr =
weakref.ref(obj)’. The object being referred to is returned by calling
the weak reference as if it were a function: ‘wr()’. It will return the
referenced object, or ‘None’ if the object no longer exists.
This makes it possible to write a ‘memoize()’ function whose cache
doesn’t keep objects alive, by storing weak references in the cache.
_cache = {}
def memoize(x):
if _cache.has_key(x):
obj = _cache[x]()
# If weak reference object still exists,
# return it
if obj is not None: return obj
retval = f(x)
# Cache a weak reference
_cache[x] = weakref.ref(retval)
return retval
The *note weakref: 128. module also allows creating proxy objects which
behave like weak references — an object referenced only by proxy objects
is deallocated – but instead of requiring an explicit call to retrieve
the object, the proxy transparently forwards all operations to the
object as long as the object still exists. If the object is
deallocated, attempting to use a proxy will cause a
‘weakref.ReferenceError’ exception to be raised.
proxy = weakref.proxy(obj)
proxy.attr # Equivalent to obj.attr
proxy.meth() # Equivalent to obj.meth()
del obj
proxy.attr # raises weakref.ReferenceError
See also
........
PEP 205(1) - Weak References
Written and implemented by Fred L. Drake, Jr.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0205
File: python.info, Node: PEP 232 Function Attributes, Next: PEP 235 Importing Modules on Case-Insensitive Platforms, Prev: PEP 205 Weak References, Up: What’s New in Python 2 1
1.16.8 PEP 232: Function Attributes
-----------------------------------
In Python 2.1, functions can now have arbitrary information attached to
them. People were often using docstrings to hold information about
functions and methods, because the ‘__doc__’ attribute was the only way
of attaching any information to a function. For example, in the Zope
Web application server, functions are marked as safe for public access
by having a docstring, and in John Aycock’s SPARK parsing framework,
docstrings hold parts of the BNF grammar to be parsed. This overloading
is unfortunate, since docstrings are really intended to hold a
function’s documentation; for example, it means you can’t properly
document functions intended for private use in Zope.
Arbitrary attributes can now be set and retrieved on functions using the
regular Python syntax:
def f(): pass
f.publish = 1
f.secure = 1
f.grammar = "A ::= B (C D)*"
The dictionary containing attributes can be accessed as the function’s
*note __dict__: 4fc. Unlike the *note __dict__: 4fc. attribute of class
instances, in functions you can actually assign a new dictionary to
*note __dict__: 4fc, though the new value is restricted to a regular
Python dictionary; you `can’t' be tricky and set it to a ‘UserDict’
instance, or any other random object that behaves like a mapping.
See also
........
PEP 232(1) - Function Attributes
Written and implemented by Barry Warsaw.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0232
File: python.info, Node: PEP 235 Importing Modules on Case-Insensitive Platforms, Next: PEP 217 Interactive Display Hook, Prev: PEP 232 Function Attributes, Up: What’s New in Python 2 1
1.16.9 PEP 235: Importing Modules on Case-Insensitive Platforms
---------------------------------------------------------------
Some operating systems have filesystems that are case-insensitive, MacOS
and Windows being the primary examples; on these systems, it’s
impossible to distinguish the filenames ‘FILE.PY’ and ‘file.py’, even
though they do store the file’s name in its original case (they’re
case-preserving, too).
In Python 2.1, the *note import: c1d. statement will work to simulate
case-sensitivity on case-insensitive platforms. Python will now search
for the first case-sensitive match by default, raising an *note
ImportError: 334. if no such file is found, so ‘import file’ will not
import a module named ‘FILE.PY’. Case-insensitive matching can be
requested by setting the *note PYTHONCASEOK: e5d. environment variable
before starting the Python interpreter.
File: python.info, Node: PEP 217 Interactive Display Hook, Next: PEP 208 New Coercion Model, Prev: PEP 235 Importing Modules on Case-Insensitive Platforms, Up: What’s New in Python 2 1
1.16.10 PEP 217: Interactive Display Hook
-----------------------------------------
When using the Python interpreter interactively, the output of commands
is displayed using the built-in *note repr(): 7cc. function. In Python
2.1, the variable *note sys.displayhook(): e5f. can be set to a callable
object which will be called instead of *note repr(): 7cc. For example,
you can set it to a special pretty-printing function:
>>> # Create a recursive data structure
... L = [1,2,3]
>>> L.append(L)
>>> L # Show Python's default output
[1, 2, 3, [...]]
>>> # Use pprint.pprint() as the display function
... import sys, pprint
>>> sys.displayhook = pprint.pprint
>>> L
[1, 2, 3, ]
>>>
See also
........
PEP 217(1) - Display Hook for Interactive Use
Written and implemented by Moshe Zadka.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0217
File: python.info, Node: PEP 208 New Coercion Model, Next: PEP 241 Metadata in Python Packages, Prev: PEP 217 Interactive Display Hook, Up: What’s New in Python 2 1
1.16.11 PEP 208: New Coercion Model
-----------------------------------
How numeric coercion is done at the C level was significantly modified.
This will only affect the authors of C extensions to Python, allowing
them more flexibility in writing extension types that support numeric
operations.
Extension types can now set the type flag ‘Py_TPFLAGS_CHECKTYPES’ in
their ‘PyTypeObject’ structure to indicate that they support the new
coercion model. In such extension types, the numeric slot functions can
no longer assume that they’ll be passed two arguments of the same type;
instead they may be passed two arguments of differing types, and can
then perform their own internal coercion. If the slot function is
passed a type it can’t handle, it can indicate the failure by returning
a reference to the ‘Py_NotImplemented’ singleton value. The numeric
functions of the other type will then be tried, and perhaps they can
handle the operation; if the other type also returns
‘Py_NotImplemented’, then a *note TypeError: 192. will be raised.
Numeric methods written in Python can also return ‘Py_NotImplemented’,
causing the interpreter to act as if the method did not exist (perhaps
raising a *note TypeError: 192, perhaps trying another object’s numeric
methods).
See also
........
PEP 208(1) - Reworking the Coercion Model
Written and implemented by Neil Schemenauer, heavily based upon
earlier work by Marc-André Lemburg. Read this to understand the
fine points of how numeric operations will now be processed at the
C level.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0208
File: python.info, Node: PEP 241 Metadata in Python Packages, Next: New and Improved Modules<4>, Prev: PEP 208 New Coercion Model, Up: What’s New in Python 2 1
1.16.12 PEP 241: Metadata in Python Packages
--------------------------------------------
A common complaint from Python users is that there’s no single catalog
of all the Python modules in existence. T. Middleton’s Vaults of
Parnassus at ‘http://www.vex.net/parnassus/’ are the largest catalog of
Python modules, but registering software at the Vaults is optional, and
many people don’t bother.
As a first small step toward fixing the problem, Python software
packaged using the Distutils ‘sdist’ command will include a file named
‘PKG-INFO’ containing information about the package such as its name,
version, and author (metadata, in cataloguing terminology). PEP 241(1)
contains the full list of fields that can be present in the ‘PKG-INFO’
file. As people began to package their software using Python 2.1, more
and more packages will include metadata, making it possible to build
automated cataloguing systems and experiment with them. With the result
experience, perhaps it’ll be possible to design a really good catalog
and then build support for it into Python 2.2. For example, the
Distutils ‘sdist’ and ‘bdist_*’ commands could support an ‘upload’
option that would automatically upload your package to a catalog server.
You can start creating packages containing ‘PKG-INFO’ even if you’re not
using Python 2.1, since a new release of the Distutils will be made for
users of earlier Python versions. Version 1.0.2 of the Distutils
includes the changes described in PEP 241(2), as well as various
bugfixes and enhancements. It will be available from the Distutils SIG
at ‘https://www.python.org/community/sigs/current/distutils-sig/’.
See also
........
PEP 241(3) - Metadata for Python Software Packages
Written and implemented by A.M. Kuchling.
PEP 243(4) - Module Repository Upload Mechanism
Written by Sean Reifschneider, this draft PEP describes a proposed
mechanism for uploading Python packages to a central server.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0241
(2) https://www.python.org/dev/peps/pep-0241
(3) https://www.python.org/dev/peps/pep-0241
(4) https://www.python.org/dev/peps/pep-0243
File: python.info, Node: New and Improved Modules<4>, Next: Other Changes and Fixes<4>, Prev: PEP 241 Metadata in Python Packages, Up: What’s New in Python 2 1
1.16.13 New and Improved Modules
--------------------------------
* Ka-Ping Yee contributed two new modules: ‘inspect.py’, a module for
getting information about live Python code, and ‘pydoc.py’, a
module for interactively converting docstrings to HTML or text. As
a bonus, ‘Tools/scripts/pydoc’, which is now automatically
installed, uses ‘pydoc.py’ to display documentation given a Python
module, package, or class name. For example, ‘pydoc xml.dom’
displays the following:
Python Library Documentation: package xml.dom in xml
NAME
xml.dom - W3C Document Object Model implementation for Python.
FILE
/usr/local/lib/python2.1/xml/dom/__init__.pyc
DESCRIPTION
The Python mapping of the Document Object Model is documented in the
Python Library Reference in the section on the xml.dom package.
This package contains the following modules:
...
‘pydoc’ also includes a Tk-based interactive help browser. ‘pydoc’
quickly becomes addictive; try it out!
* Two different modules for unit testing were added to the standard
library. The *note doctest: 67. module, contributed by Tim Peters,
provides a testing framework based on running embedded examples in
docstrings and comparing the results against the expected output.
PyUnit, contributed by Steve Purcell, is a unit testing framework
inspired by JUnit, which was in turn an adaptation of Kent Beck’s
Smalltalk testing framework. See ‘http://pyunit.sourceforge.net/’
for more information about PyUnit.
* The *note difflib: 37. module contains a class, ‘SequenceMatcher’,
which compares two sequences and computes the changes required to
transform one sequence into the other. For example, this module
can be used to write a tool similar to the Unix ‘diff’ program, and
in fact the sample program ‘Tools/scripts/ndiff.py’ demonstrates
how to write such a script.
* *note curses.panel: 2e, a wrapper for the panel library, part of
ncurses and of SYSV curses, was contributed by Thomas Gellekum.
The panel library provides windows with the additional feature of
depth. Windows can be moved higher or lower in the depth ordering,
and the panel library figures out where panels overlap and which
sections are visible.
* The PyXML package has gone through a few releases since Python 2.0,
and Python 2.1 includes an updated version of the *note xml: 133.
package. Some of the noteworthy changes include support for Expat
1.2 and later versions, the ability for Expat parsers to handle
files in any encoding supported by Python, and various bugfixes for
SAX, DOM, and the ‘minidom’ module.
* Ping also contributed another hook for handling uncaught
exceptions. *note sys.excepthook(): e63. can be set to a callable
object. When an exception isn’t caught by any *note try:
d72.…*note except: b3e. blocks, the exception will be passed to
*note sys.excepthook(): e63, which can then do whatever it likes.
At the Ninth Python Conference, Ping demonstrated an application
for this hook: printing an extended traceback that not only lists
the stack frames, but also lists the function arguments and the
local variables for each frame.
* Various functions in the *note time: 10a. module, such as
‘asctime()’ and ‘localtime()’, require a floating point argument
containing the time in seconds since the epoch. The most common
use of these functions is to work with the current time, so the
floating point argument has been made optional; when a value isn’t
provided, the current time will be used. For example, log file
entries usually need a string containing the current time; in
Python 2.1, ‘time.asctime()’ can be used, instead of the lengthier
‘time.asctime(time.localtime(time.time()))’ that was previously
required.
This change was proposed and implemented by Thomas Wouters.
* The *note ftplib: 84. module now defaults to retrieving files in
passive mode, because passive mode is more likely to work from
behind a firewall. This request came from the Debian bug tracking
system, since other Debian packages use *note ftplib: 84. to
retrieve files and then don’t work from behind a firewall. It’s
deemed unlikely that this will cause problems for anyone, because
Netscape defaults to passive mode and few people complain, but if
passive mode is unsuitable for your application or network setup,
call ‘set_pasv(0)’ on FTP objects to disable passive mode.
* Support for raw socket access has been added to the *note socket:
ef. module, contributed by Grant Edwards.
* The *note pstats: d4. module now contains a simple interactive
statistics browser for displaying timing profiles for Python
programs, invoked when the module is run as a script. Contributed
by Eric S. Raymond.
* A new implementation-dependent function, ‘sys._getframe([depth])’,
has been added to return a given frame object from the current call
stack. *note sys._getframe(): e64. returns the frame at the top of
the call stack; if the optional integer argument `depth' is
supplied, the function returns the frame that is `depth' calls
below the top of the stack. For example, ‘sys._getframe(1)’
returns the caller’s frame object.
This function is only present in CPython, not in Jython or the .NET
implementation. Use it for debugging, and resist the temptation to
put it into production code.
File: python.info, Node: Other Changes and Fixes<4>, Next: Acknowledgements<7>, Prev: New and Improved Modules<4>, Up: What’s New in Python 2 1
1.16.14 Other Changes and Fixes
-------------------------------
There were relatively few smaller changes made in Python 2.1 due to the
shorter release cycle. A search through the CVS change logs turns up
117 patches applied, and 136 bugs fixed; both figures are likely to be
underestimates. Some of the more notable changes are:
* A specialized object allocator is now optionally available, that
should be faster than the system ‘malloc()’ and have less memory
overhead. The allocator uses C’s ‘malloc()’ function to get large
pools of memory, and then fulfills smaller memory requests from
these pools. It can be enabled by providing the ‘--with-pymalloc’
option to the ‘configure’ script; see ‘Objects/obmalloc.c’ for the
implementation details.
Authors of C extension modules should test their code with the
object allocator enabled, because some incorrect code may break,
causing core dumps at runtime. There are a bunch of memory
allocation functions in Python’s C API that have previously been
just aliases for the C library’s ‘malloc()’ and ‘free()’, meaning
that if you accidentally called mismatched functions, the error
wouldn’t be noticeable. When the object allocator is enabled,
these functions aren’t aliases of ‘malloc()’ and ‘free()’ any more,
and calling the wrong function to free memory will get you a core
dump. For example, if memory was allocated using ‘PyMem_New()’, it
has to be freed using ‘PyMem_Del()’, not ‘free()’. A few modules
included with Python fell afoul of this and had to be fixed;
doubtless there are more third-party modules that will have the
same problem.
The object allocator was contributed by Vladimir Marangozov.
* The speed of line-oriented file I/O has been improved because
people often complain about its lack of speed, and because it’s
often been used as a naïve benchmark. The *note readline(): de.
method of file objects has therefore been rewritten to be much
faster. The exact amount of the speedup will vary from platform to
platform depending on how slow the C library’s ‘getc()’ was, but is
around 66%, and potentially much faster on some particular
operating systems. Tim Peters did much of the benchmarking and
coding for this change, motivated by a discussion in
comp.lang.python.
A new module and method for file objects was also added,
contributed by Jeff Epler. The new method, ‘xreadlines()’, is
similar to the existing ‘xrange()’ built-in. ‘xreadlines()’
returns an opaque sequence object that only supports being iterated
over, reading a line on every iteration but not reading the entire
file into memory as the existing ‘readlines()’ method does. You’d
use it like this:
for line in sys.stdin.xreadlines():
# ... do something for each line ...
...
For a fuller discussion of the line I/O changes, see the python-dev
summary for January 1–15, 2001 at
‘https://mail.python.org/pipermail/python-dev/2001-January/’.
* A new method, ‘popitem()’, was added to dictionaries to enable
destructively iterating through the contents of a dictionary; this
can be faster for large dictionaries because there’s no need to
construct a list containing all the keys or values. ‘D.popitem()’
removes a random ‘(key, value)’ pair from the dictionary ‘D’ and
returns it as a 2-tuple. This was implemented mostly by Tim Peters
and Guido van Rossum, after a suggestion and preliminary patch by
Moshe Zadka.
* Modules can now control which names are imported when ‘from module
import *’ is used, by defining an ‘__all__’ attribute containing a
list of names that will be imported. One common complaint is that
if the module imports other modules such as *note sys: fd. or *note
string: f6, ‘from module import *’ will add them to the importing
module’s namespace. To fix this, simply list the public names in
‘__all__’:
# List public names
__all__ = ['Database', 'open']
A stricter version of this patch was first suggested and
implemented by Ben Wolfson, but after some python-dev discussion, a
weaker final version was checked in.
* Applying *note repr(): 7cc. to strings previously used octal
escapes for non-printable characters; for example, a newline was
‘'\012'’. This was a vestigial trace of Python’s C ancestry, but
today octal is of very little practical use. Ka-Ping Yee suggested
using hex escapes instead of octal ones, and using the ‘\n’, ‘\t’,
‘\r’ escapes for the appropriate characters, and implemented this
new formatting.
* Syntax errors detected at compile-time can now raise exceptions
containing the filename and line number of the error, a pleasant
side effect of the compiler reorganization done by Jeremy Hylton.
* C extensions which import other modules have been changed to use
‘PyImport_ImportModule()’, which means that they will use any
import hooks that have been installed. This is also encouraged for
third-party extensions that need to import some other module from C
code.
* The size of the Unicode character database was shrunk by another
340K thanks to Fredrik Lundh.
* Some new ports were contributed: MacOS X (by Steven Majewski),
Cygwin (by Jason Tishler); RISCOS (by Dietmar Schwertberger);
Unixware 7 (by Billy G. Allie).
And there’s the usual list of minor bugfixes, minor memory leaks,
docstring edits, and other tweaks, too lengthy to be worth itemizing;
see the CVS logs for the full details if you want them.
File: python.info, Node: Acknowledgements<7>, Prev: Other Changes and Fixes<4>, Up: What’s New in Python 2 1
1.16.15 Acknowledgements
------------------------
The author would like to thank the following people for offering
suggestions on various drafts of this article: Graeme Cross, David
Goodger, Jay Graves, Michael Hudson, Marc-André Lemburg, Fredrik Lundh,
Neil Schemenauer, Thomas Wouters.
File: python.info, Node: What’s New in Python 2 0, Next: Changelog, Prev: What’s New in Python 2 1, Up: What’s New in Python
1.17 What’s New in Python 2.0
=============================
Author: A.M. Kuchling and Moshe Zadka
* Menu:
* Introduction: Introduction<3>.
* What About Python 1.6?: What About Python 1 6?.
* New Development Process::
* Unicode: Unicode<2>.
* List Comprehensions::
* Augmented Assignment::
* String Methods::
* Garbage Collection of Cycles::
* Other Core Changes::
* Porting to 2.0: Porting to 2 0.
* Extending/Embedding Changes::
* Distutils; Making Modules Easy to Install: Distutils Making Modules Easy to Install.
* XML Modules::
* Module changes::
* New modules::
* IDLE Improvements::
* Deleted and Deprecated Modules::
* Acknowledgements: Acknowledgements<8>.
File: python.info, Node: Introduction<3>, Next: What About Python 1 6?, Up: What’s New in Python 2 0
1.17.1 Introduction
-------------------
A new release of Python, version 2.0, was released on October 16, 2000.
This article covers the exciting new features in 2.0, highlights some
other useful changes, and points out a few incompatible changes that may
require rewriting code.
Python’s development never completely stops between releases, and a
steady flow of bug fixes and improvements are always being submitted. A
host of minor fixes, a few optimizations, additional docstrings, and
better error messages went into 2.0; to list them all would be
impossible, but they’re certainly significant. Consult the
publicly-available CVS logs if you want to see the full list. This
progress is due to the five developers working for PythonLabs are now
getting paid to spend their days fixing bugs, and also due to the
improved communication resulting from moving to SourceForge.
File: python.info, Node: What About Python 1 6?, Next: New Development Process, Prev: Introduction<3>, Up: What’s New in Python 2 0
1.17.2 What About Python 1.6?
-----------------------------
Python 1.6 can be thought of as the Contractual Obligations Python
release. After the core development team left CNRI in May 2000, CNRI
requested that a 1.6 release be created, containing all the work on
Python that had been performed at CNRI. Python 1.6 therefore represents
the state of the CVS tree as of May 2000, with the most significant new
feature being Unicode support. Development continued after May, of
course, so the 1.6 tree received a few fixes to ensure that it’s
forward-compatible with Python 2.0. 1.6 is therefore part of Python’s
evolution, and not a side branch.
So, should you take much interest in Python 1.6? Probably not. The
1.6final and 2.0beta1 releases were made on the same day (September 5,
2000), the plan being to finalize Python 2.0 within a month or so. If
you have applications to maintain, there seems little point in breaking
things by moving to 1.6, fixing them, and then having another round of
breakage within a month by moving to 2.0; you’re better off just going
straight to 2.0. Most of the really interesting features described in
this document are only in 2.0, because a lot of work was done between
May and September.
File: python.info, Node: New Development Process, Next: Unicode<2>, Prev: What About Python 1 6?, Up: What’s New in Python 2 0
1.17.3 New Development Process
------------------------------
The most important change in Python 2.0 may not be to the code at all,
but to how Python is developed: in May 2000 the Python developers began
using the tools made available by SourceForge for storing source code,
tracking bug reports, and managing the queue of patch submissions. To
report bugs or submit patches for Python 2.0, use the bug tracking and
patch manager tools available from Python’s project page, located at
‘https://sourceforge.net/projects/python/’.
The most important of the services now hosted at SourceForge is the
Python CVS tree, the version-controlled repository containing the source
code for Python. Previously, there were roughly 7 or so people who had
write access to the CVS tree, and all patches had to be inspected and
checked in by one of the people on this short list. Obviously, this
wasn’t very scalable. By moving the CVS tree to SourceForge, it became
possible to grant write access to more people; as of September 2000
there were 27 people able to check in changes, a fourfold increase.
This makes possible large-scale changes that wouldn’t be attempted if
they’d have to be filtered through the small group of core developers.
For example, one day Peter Schneider-Kamp took it into his head to drop
K&R C compatibility and convert the C source for Python to ANSI C. After
getting approval on the python-dev mailing list, he launched into a
flurry of checkins that lasted about a week, other developers joined in
to help, and the job was done. If there were only 5 people with write
access, probably that task would have been viewed as “nice, but not
worth the time and effort needed” and it would never have gotten done.
The shift to using SourceForge’s services has resulted in a remarkable
increase in the speed of development. Patches now get submitted,
commented on, revised by people other than the original submitter, and
bounced back and forth between people until the patch is deemed worth
checking in. Bugs are tracked in one central location and can be
assigned to a specific person for fixing, and we can count the number of
open bugs to measure progress. This didn’t come without a cost:
developers now have more e-mail to deal with, more mailing lists to
follow, and special tools had to be written for the new environment.
For example, SourceForge sends default patch and bug notification e-mail
messages that are completely unhelpful, so Ka-Ping Yee wrote an HTML
screen-scraper that sends more useful messages.
The ease of adding code caused a few initial growing pains, such as code
was checked in before it was ready or without getting clear agreement
from the developer group. The approval process that has emerged is
somewhat similar to that used by the Apache group. Developers can vote
+1, +0, -0, or -1 on a patch; +1 and -1 denote acceptance or rejection,
while +0 and -0 mean the developer is mostly indifferent to the change,
though with a slight positive or negative slant. The most significant
change from the Apache model is that the voting is essentially advisory,
letting Guido van Rossum, who has Benevolent Dictator For Life status,
know what the general opinion is. He can still ignore the result of a
vote, and approve or reject a change even if the community disagrees
with him.
Producing an actual patch is the last step in adding a new feature, and
is usually easy compared to the earlier task of coming up with a good
design. Discussions of new features can often explode into lengthy
mailing list threads, making the discussion hard to follow, and no one
can read every posting to python-dev. Therefore, a relatively formal
process has been set up to write Python Enhancement Proposals (PEPs),
modelled on the Internet RFC process. PEPs are draft documents that
describe a proposed new feature, and are continually revised until the
community reaches a consensus, either accepting or rejecting the
proposal. Quoting from the introduction to PEP 1(1), “PEP Purpose and
Guidelines”:
PEP stands for Python Enhancement Proposal. A PEP is a design
document providing information to the Python community, or
describing a new feature for Python. The PEP should provide a
concise technical specification of the feature and a rationale for
the feature.
We intend PEPs to be the primary mechanisms for proposing new
features, for collecting community input on an issue, and for
documenting the design decisions that have gone into Python. The
PEP author is responsible for building consensus within the
community and documenting dissenting opinions.
Read the rest of PEP 1(2) for the details of the PEP editorial process,
style, and format. PEPs are kept in the Python CVS tree on SourceForge,
though they’re not part of the Python 2.0 distribution, and are also
available in HTML form from ‘https://www.python.org/dev/peps/’. As of
September 2000, there are 25 PEPS, ranging from PEP 201(3), “Lockstep
Iteration”, to PEP 225, “Elementwise/Objectwise Operators”.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0001
(2) https://www.python.org/dev/peps/pep-0001
(3) https://www.python.org/dev/peps/pep-0201
File: python.info, Node: Unicode<2>, Next: List Comprehensions, Prev: New Development Process, Up: What’s New in Python 2 0
1.17.4 Unicode
--------------
The largest new feature in Python 2.0 is a new fundamental data type:
Unicode strings. Unicode uses 16-bit numbers to represent characters
instead of the 8-bit number used by ASCII, meaning that 65,536 distinct
characters can be supported.
The final interface for Unicode support was arrived at through countless
often-stormy discussions on the python-dev mailing list, and mostly
implemented by Marc-André Lemburg, based on a Unicode string type
implementation by Fredrik Lundh. A detailed explanation of the
interface was written up as PEP 100(1), “Python Unicode Integration”.
This article will simply cover the most significant points about the
Unicode interfaces.
In Python source code, Unicode strings are written as ‘u"string"’.
Arbitrary Unicode characters can be written using a new escape sequence,
‘\uHHHH’, where `HHHH' is a 4-digit hexadecimal number from 0000 to
FFFF. The existing ‘\xHHHH’ escape sequence can also be used, and octal
escapes can be used for characters up to U+01FF, which is represented by
‘\777’.
Unicode strings, just like regular strings, are an immutable sequence
type. They can be indexed and sliced, but not modified in place.
Unicode strings have an ‘encode( [encoding] )’ method that returns an
8-bit string in the desired encoding. Encodings are named by strings,
such as ‘'ascii'’, ‘'utf-8'’, ‘'iso-8859-1'’, or whatever. A codec API
is defined for implementing and registering new encodings that are then
available throughout a Python program. If an encoding isn’t specified,
the default encoding is usually 7-bit ASCII, though it can be changed
for your Python installation by calling the
‘sys.setdefaultencoding(encoding)’ function in a customized version of
‘site.py’.
Combining 8-bit and Unicode strings always coerces to Unicode, using the
default ASCII encoding; the result of ‘'a' + u'bc'’ is ‘u'abc'’.
New built-in functions have been added, and existing built-ins modified
to support Unicode:
* ‘unichr(ch)’ returns a Unicode string 1 character long, containing
the character `ch'.
* ‘ord(u)’, where `u' is a 1-character regular or Unicode string,
returns the number of the character as an integer.
* ‘unicode(string [, encoding] [, errors] )’ creates a Unicode string
from an 8-bit string. ‘encoding’ is a string naming the encoding
to use. The ‘errors’ parameter specifies the treatment of
characters that are invalid for the current encoding; passing
‘'strict'’ as the value causes an exception to be raised on any
encoding error, while ‘'ignore'’ causes errors to be silently
ignored and ‘'replace'’ uses U+FFFD, the official replacement
character, in case of any problems.
* The ‘exec’ statement, and various built-ins such as ‘eval()’,
‘getattr()’, and ‘setattr()’ will also accept Unicode strings as
well as regular strings. (It’s possible that the process of fixing
this missed some built-ins; if you find a built-in function that
accepts strings but doesn’t accept Unicode strings at all, please
report it as a bug.)
A new module, *note unicodedata: 11a, provides an interface to Unicode
character properties. For example, ‘unicodedata.category(u'A')’ returns
the 2-character string ‘Lu’, the ‘L’ denoting it’s a letter, and ‘u’
meaning that it’s uppercase. ‘unicodedata.bidirectional(u'\u0660')’
returns ‘AN’, meaning that U+0660 is an Arabic number.
The *note codecs: 1c. module contains functions to look up existing
encodings and register new ones. Unless you want to implement a new
encoding, you’ll most often use the ‘codecs.lookup(encoding)’ function,
which returns a 4-element tuple: ‘(encode_func, decode_func,
stream_reader, stream_writer)’.
* `encode_func' is a function that takes a Unicode string, and
returns a 2-tuple ‘(string, length)’. `string' is an 8-bit string
containing a portion (perhaps all) of the Unicode string converted
into the given encoding, and `length' tells you how much of the
Unicode string was converted.
* `decode_func' is the opposite of `encode_func', taking an 8-bit
string and returning a 2-tuple ‘(ustring, length)’, consisting of
the resulting Unicode string `ustring' and the integer `length'
telling how much of the 8-bit string was consumed.
* `stream_reader' is a class that supports decoding input from a
stream. `stream_reader(file_obj)' returns an object that supports
the ‘read()’, *note readline(): de, and ‘readlines()’ methods.
These methods will all translate from the given encoding and return
Unicode strings.
* `stream_writer', similarly, is a class that supports encoding
output to a stream. `stream_writer(file_obj)' returns an object
that supports the ‘write()’ and ‘writelines()’ methods. These
methods expect Unicode strings, translating them to the given
encoding on output.
For example, the following code writes a Unicode string into a file,
encoding it as UTF-8:
import codecs
unistr = u'\u0660\u2000ab ...'
(UTF8_encode, UTF8_decode,
UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8')
output = UTF8_streamwriter( open( '/tmp/output', 'wb') )
output.write( unistr )
output.close()
The following code would then read UTF-8 input from the file:
input = UTF8_streamreader( open( '/tmp/output', 'rb') )
print repr(input.read())
input.close()
Unicode-aware regular expressions are available through the *note re:
dd. module, which has a new underlying implementation called SRE written
by Fredrik Lundh of Secret Labs AB.
A ‘-U’ command line option was added which causes the Python compiler to
interpret all string literals as Unicode string literals. This is
intended to be used in testing and future-proofing your Python code,
since some future version of Python may drop support for 8-bit strings
and provide only Unicode strings.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0100
File: python.info, Node: List Comprehensions, Next: Augmented Assignment, Prev: Unicode<2>, Up: What’s New in Python 2 0
1.17.5 List Comprehensions
--------------------------
Lists are a workhorse data type in Python, and many programs manipulate
a list at some point. Two common operations on lists are to loop over
them, and either pick out the elements that meet a certain criterion, or
apply some function to each element. For example, given a list of
strings, you might want to pull out all the strings containing a given
substring, or strip off trailing whitespace from each line.
The existing *note map(): c2d. and *note filter(): c2e. functions can be
used for this purpose, but they require a function as one of their
arguments. This is fine if there’s an existing built-in function that
can be passed directly, but if there isn’t, you have to create a little
function to do the required work, and Python’s scoping rules make the
result ugly if the little function needs additional information. Take
the first example in the previous paragraph, finding all the strings in
the list containing a given substring. You could write the following to
do it:
# Given the list L, make a list of all strings
# containing the substring S.
sublist = filter( lambda s, substring=S:
string.find(s, substring) != -1,
L)
Because of Python’s scoping rules, a default argument is used so that
the anonymous function created by the *note lambda: c2f. expression
knows what substring is being searched for. List comprehensions make
this cleaner:
sublist = [ s for s in L if string.find(s, S) != -1 ]
List comprehensions have the form:
[ expression for expr in sequence1
for expr2 in sequence2 ...
for exprN in sequenceN
if condition ]
The ‘for’…‘in’ clauses contain the sequences to be iterated over. The
sequences do not have to be the same length, because they are `not'
iterated over in parallel, but from left to right; this is explained
more clearly in the following paragraphs. The elements of the generated
list will be the successive values of `expression'. The final ‘if’
clause is optional; if present, `expression' is only evaluated and added
to the result if `condition' is true.
To make the semantics very clear, a list comprehension is equivalent to
the following Python code:
for expr1 in sequence1:
for expr2 in sequence2:
...
for exprN in sequenceN:
if (condition):
# Append the value of
# the expression to the
# resulting list.
This means that when there are multiple ‘for’…‘in’ clauses, the
resulting list will be equal to the product of the lengths of all the
sequences. If you have two lists of length 3, the output list is 9
elements long:
seq1 = 'abc'
seq2 = (1,2,3)
>>> [ (x,y) for x in seq1 for y in seq2]
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1),
('c', 2), ('c', 3)]
To avoid introducing an ambiguity into Python’s grammar, if `expression'
is creating a tuple, it must be surrounded with parentheses. The first
list comprehension below is a syntax error, while the second one is
correct:
# Syntax error
[ x,y for x in seq1 for y in seq2]
# Correct
[ (x,y) for x in seq1 for y in seq2]
The idea of list comprehensions originally comes from the functional
programming language Haskell (‘https://www.haskell.org’). Greg Ewing
argued most effectively for adding them to Python and wrote the initial
list comprehension patch, which was then discussed for a seemingly
endless time on the python-dev mailing list and kept up-to-date by Skip
Montanaro.
File: python.info, Node: Augmented Assignment, Next: String Methods, Prev: List Comprehensions, Up: What’s New in Python 2 0
1.17.6 Augmented Assignment
---------------------------
Augmented assignment operators, another long-requested feature, have
been added to Python 2.0. Augmented assignment operators include ‘+=’,
‘-=’, ‘*=’, and so forth. For example, the statement ‘a += 2’
increments the value of the variable ‘a’ by 2, equivalent to the
slightly lengthier ‘a = a + 2’.
The full list of supported assignment operators is ‘+=’, ‘-=’, ‘*=’,
‘/=’, ‘%=’, ‘**=’, ‘&=’, ‘|=’, ‘^=’, ‘>>=’, and ‘<<=’. Python classes
can override the augmented assignment operators by defining methods
named *note __iadd__(): e6f, *note __isub__(): e70, etc. For example,
the following ‘Number’ class stores a number and supports using += to
create a new instance with an incremented value.
class Number:
def __init__(self, value):
self.value = value
def __iadd__(self, increment):
return Number( self.value + increment)
n = Number(5)
n += 3
print n.value
The *note __iadd__(): e6f. special method is called with the value of
the increment, and should return a new instance with an appropriately
modified value; this return value is bound as the new value of the
variable on the left-hand side.
Augmented assignment operators were first introduced in the C
programming language, and most C-derived languages, such as ‘awk’, C++,
Java, Perl, and PHP also support them. The augmented assignment patch
was implemented by Thomas Wouters.
File: python.info, Node: String Methods, Next: Garbage Collection of Cycles, Prev: Augmented Assignment, Up: What’s New in Python 2 0
1.17.7 String Methods
---------------------
Until now string-manipulation functionality was in the *note string: f6.
module, which was usually a front-end for the ‘strop’ module written in
C. The addition of Unicode posed a difficulty for the ‘strop’ module,
because the functions would all need to be rewritten in order to accept
either 8-bit or Unicode strings. For functions such as
‘string.replace()’, which takes 3 string arguments, that means eight
possible permutations, and correspondingly complicated code.
Instead, Python 2.0 pushes the problem onto the string type, making
string manipulation functionality available through methods on both
8-bit strings and Unicode strings.
>>> 'andrew'.capitalize()
'Andrew'
>>> 'hostname'.replace('os', 'linux')
'hlinuxtname'
>>> 'moshe'.find('sh')
2
One thing that hasn’t changed, a noteworthy April Fools’ joke
notwithstanding, is that Python strings are immutable. Thus, the string
methods return new strings, and do not modify the string on which they
operate.
The old *note string: f6. module is still around for backwards
compatibility, but it mostly acts as a front-end to the new string
methods.
Two methods which have no parallel in pre-2.0 versions, although they
did exist in JPython for quite some time, are ‘startswith()’ and
‘endswith()’. ‘s.startswith(t)’ is equivalent to ‘s[:len(t)] == t’,
while ‘s.endswith(t)’ is equivalent to ‘s[-len(t):] == t’.
One other method which deserves special mention is ‘join()’. The
‘join()’ method of a string receives one parameter, a sequence of
strings, and is equivalent to the ‘string.join()’ function from the old
*note string: f6. module, with the arguments reversed. In other words,
‘s.join(seq)’ is equivalent to the old ‘string.join(seq, s)’.
File: python.info, Node: Garbage Collection of Cycles, Next: Other Core Changes, Prev: String Methods, Up: What’s New in Python 2 0
1.17.8 Garbage Collection of Cycles
-----------------------------------
The C implementation of Python uses reference counting to implement
garbage collection. Every Python object maintains a count of the number
of references pointing to itself, and adjusts the count as references
are created or destroyed. Once the reference count reaches zero, the
object is no longer accessible, since you need to have a reference to an
object to access it, and if the count is zero, no references exist any
longer.
Reference counting has some pleasant properties: it’s easy to understand
and implement, and the resulting implementation is portable, fairly
fast, and reacts well with other libraries that implement their own
memory handling schemes. The major problem with reference counting is
that it sometimes doesn’t realise that objects are no longer accessible,
resulting in a memory leak. This happens when there are cycles of
references.
Consider the simplest possible cycle, a class instance which has a
reference to itself:
instance = SomeClass()
instance.myself = instance
After the above two lines of code have been executed, the reference
count of ‘instance’ is 2; one reference is from the variable named
‘'instance'’, and the other is from the ‘myself’ attribute of the
instance.
If the next line of code is ‘del instance’, what happens? The reference
count of ‘instance’ is decreased by 1, so it has a reference count of 1;
the reference in the ‘myself’ attribute still exists. Yet the instance
is no longer accessible through Python code, and it could be deleted.
Several objects can participate in a cycle if they have references to
each other, causing all of the objects to be leaked.
Python 2.0 fixes this problem by periodically executing a cycle
detection algorithm which looks for inaccessible cycles and deletes the
objects involved. A new *note gc: 86. module provides functions to
perform a garbage collection, obtain debugging statistics, and tuning
the collector’s parameters.
Running the cycle detection algorithm takes some time, and therefore
will result in some additional overhead. It is hoped that after we’ve
gotten experience with the cycle collection from using 2.0, Python 2.1
will be able to minimize the overhead with careful tuning. It’s not yet
obvious how much performance is lost, because benchmarking this is
tricky and depends crucially on how often the program creates and
destroys objects. The detection of cycles can be disabled when Python
is compiled, if you can’t afford even a tiny speed penalty or suspect
that the cycle collection is buggy, by specifying the
‘--without-cycle-gc’ switch when running the ‘configure’ script.
Several people tackled this problem and contributed to a solution. An
early implementation of the cycle detection approach was written by Toby
Kelsey. The current algorithm was suggested by Eric Tiedemann during a
visit to CNRI, and Guido van Rossum and Neil Schemenauer wrote two
different implementations, which were later integrated by Neil. Lots of
other people offered suggestions along the way; the March 2000 archives
of the python-dev mailing list contain most of the relevant discussion,
especially in the threads titled “Reference cycle collection for Python”
and “Finalization again”.
File: python.info, Node: Other Core Changes, Next: Porting to 2 0, Prev: Garbage Collection of Cycles, Up: What’s New in Python 2 0
1.17.9 Other Core Changes
-------------------------
Various minor changes have been made to Python’s syntax and built-in
functions. None of the changes are very far-reaching, but they’re handy
conveniences.
* Menu:
* Minor Language Changes::
* Changes to Built-in Functions::
File: python.info, Node: Minor Language Changes, Next: Changes to Built-in Functions, Up: Other Core Changes
1.17.9.1 Minor Language Changes
...............................
A new syntax makes it more convenient to call a given function with a
tuple of arguments and/or a dictionary of keyword arguments. In Python
1.5 and earlier, you’d use the ‘apply()’ built-in function: ‘apply(f,
args, kw)’ calls the function ‘f()’ with the argument tuple `args' and
the keyword arguments in the dictionary `kw'. ‘apply()’ is the same in
2.0, but thanks to a patch from Greg Ewing, ‘f(*args, **kw)’ is a
shorter and clearer way to achieve the same effect. This syntax is
symmetrical with the syntax for defining functions:
def f(*args, **kw):
# args is a tuple of positional args,
# kw is a dictionary of keyword args
...
The ‘print’ statement can now have its output directed to a file-like
object by following the ‘print’ with ‘>> file’, similar to the
redirection operator in Unix shells. Previously you’d either have to
use the ‘write()’ method of the file-like object, which lacks the
convenience and simplicity of ‘print’, or you could assign a new value
to ‘sys.stdout’ and then restore the old value. For sending output to
standard error, it’s much easier to write this:
print >> sys.stderr, "Warning: action field not supplied"
Modules can now be renamed on importing them, using the syntax ‘import
module as name’ or ‘from module import name as othername’. The patch
was submitted by Thomas Wouters.
A new format style is available when using the ‘%’ operator; ‘%r’ will
insert the *note repr(): 7cc. of its argument. This was also added from
symmetry considerations, this time for symmetry with the existing ‘%s’
format style, which inserts the *note str(): 330. of its argument. For
example, ‘'%r %s' % ('abc', 'abc')’ returns a string containing ‘'abc'
abc’.
Previously there was no way to implement a class that overrode Python’s
built-in *note in: 7a3. operator and implemented a custom version. ‘obj
in seq’ returns true if `obj' is present in the sequence `seq'; Python
computes this by simply trying every index of the sequence until either
`obj' is found or an *note IndexError: e75. is encountered. Moshe Zadka
contributed a patch which adds a *note __contains__(): d28. magic method
for providing a custom implementation for ‘in’. Additionally, new
built-in objects written in C can define what ‘in’ means for them via a
new slot in the sequence protocol.
Earlier versions of Python used a recursive algorithm for deleting
objects. Deeply nested data structures could cause the interpreter to
fill up the C stack and crash; Christian Tismer rewrote the deletion
logic to fix this problem. On a related note, comparing recursive
objects recursed infinitely and crashed; Jeremy Hylton rewrote the code
to no longer crash, producing a useful result instead. For example,
after this code:
a = []
b = []
a.append(a)
b.append(b)
The comparison ‘a==b’ returns true, because the two recursive data
structures are isomorphic. See the thread “trashcan and PR#7” in the
April 2000 archives of the python-dev mailing list for the discussion
leading up to this implementation, and some useful relevant links. Note
that comparisons can now also raise exceptions. In earlier versions of
Python, a comparison operation such as ‘cmp(a,b)’ would always produce
an answer, even if a user-defined ‘__cmp__()’ method encountered an
error, since the resulting exception would simply be silently swallowed.
Work has been done on porting Python to 64-bit Windows on the Itanium
processor, mostly by Trent Mick of ActiveState. (Confusingly,
‘sys.platform’ is still ‘'win32'’ on Win64 because it seems that for
ease of porting, MS Visual C++ treats code as 32 bit on Itanium.)
PythonWin also supports Windows CE; see the Python CE page at
‘http://pythonce.sourceforge.net/’ for more information.
Another new platform is Darwin/MacOS X; initial support for it is in
Python 2.0. Dynamic loading works, if you specify “configure –with-dyld
–with-suffix=.x”. Consult the README in the Python source distribution
for more instructions.
An attempt has been made to alleviate one of Python’s warts, the
often-confusing *note NameError: d7b. exception when code refers to a
local variable before the variable has been assigned a value. For
example, the following code raises an exception on the ‘print’ statement
in both 1.5.2 and 2.0; in 1.5.2 a *note NameError: d7b. exception is
raised, while 2.0 raises a new *note UnboundLocalError: e76. exception.
*note UnboundLocalError: e76. is a subclass of *note NameError: d7b, so
any existing code that expects *note NameError: d7b. to be raised should
still work.
def f():
print "i=",i
i = i + 1
f()
Two new exceptions, *note TabError: e77. and *note IndentationError:
e78, have been introduced. They’re both subclasses of *note
SyntaxError: 458, and are raised when Python code is found to be
improperly indented.
File: python.info, Node: Changes to Built-in Functions, Prev: Minor Language Changes, Up: Other Core Changes
1.17.9.2 Changes to Built-in Functions
......................................
A new built-in, ‘zip(seq1, seq2, ...)’, has been added. *note zip():
c32. returns a list of tuples where each tuple contains the i-th element
from each of the argument sequences. The difference between *note
zip(): c32. and ‘map(None, seq1, seq2)’ is that *note map(): c2d. pads
the sequences with ‘None’ if the sequences aren’t all of the same
length, while *note zip(): c32. truncates the returned list to the
length of the shortest argument sequence.
The *note int(): 184. and ‘long()’ functions now accept an optional
“base” parameter when the first argument is a string. ‘int('123', 10)’
returns 123, while ‘int('123', 16)’ returns 291. ‘int(123, 16)’ raises
a *note TypeError: 192. exception with the message “can’t convert
non-string with explicit base”.
A new variable holding more detailed version information has been added
to the *note sys: fd. module. ‘sys.version_info’ is a tuple ‘(major,
minor, micro, level, serial)’ For example, in a hypothetical 2.0.1beta1,
‘sys.version_info’ would be ‘(2, 0, 1, 'beta', 1)’. `level' is a string
such as ‘"alpha"’, ‘"beta"’, or ‘"final"’ for a final release.
Dictionaries have an odd new method, ‘setdefault(key, default)’, which
behaves similarly to the existing ‘get()’ method. However, if the key
is missing, ‘setdefault()’ both returns the value of `default' as
‘get()’ would do, and also inserts it into the dictionary as the value
for `key'. Thus, the following lines of code:
if dict.has_key( key ): return dict[key]
else:
dict[key] = []
return dict[key]
can be reduced to a single ‘return dict.setdefault(key, [])’ statement.
The interpreter sets a maximum recursion depth in order to catch runaway
recursion before filling the C stack and causing a core dump or GPF..
Previously this limit was fixed when you compiled Python, but in 2.0 the
maximum recursion depth can be read and modified using *note
sys.getrecursionlimit(): e7a. and *note sys.setrecursionlimit(): e7b.
The default value is 1000, and a rough maximum value for a given
platform can be found by running a new script,
‘Misc/find_recursionlimit.py’.
File: python.info, Node: Porting to 2 0, Next: Extending/Embedding Changes, Prev: Other Core Changes, Up: What’s New in Python 2 0
1.17.10 Porting to 2.0
----------------------
New Python releases try hard to be compatible with previous releases,
and the record has been pretty good. However, some changes are
considered useful enough, usually because they fix initial design
decisions that turned out to be actively mistaken, that breaking
backward compatibility can’t always be avoided. This section lists the
changes in Python 2.0 that may cause old Python code to break.
The change which will probably break the most code is tightening up the
arguments accepted by some methods. Some methods would take multiple
arguments and treat them as a tuple, particularly various list methods
such as ‘append()’ and ‘insert()’. In earlier versions of Python, if
‘L’ is a list, ‘L.append( 1,2 )’ appends the tuple ‘(1,2)’ to the list.
In Python 2.0 this causes a *note TypeError: 192. exception to be
raised, with the message: ‘append requires exactly 1 argument; 2 given’.
The fix is to simply add an extra set of parentheses to pass both values
as a tuple: ‘L.append( (1,2) )’.
The earlier versions of these methods were more forgiving because they
used an old function in Python’s C interface to parse their arguments;
2.0 modernizes them to use ‘PyArg_ParseTuple()’, the current argument
parsing function, which provides more helpful error messages and treats
multi-argument calls as errors. If you absolutely must use 2.0 but
can’t fix your code, you can edit ‘Objects/listobject.c’ and define the
preprocessor symbol ‘NO_STRICT_LIST_APPEND’ to preserve the old
behaviour; this isn’t recommended.
Some of the functions in the *note socket: ef. module are still
forgiving in this way. For example, ‘socket.connect( ('hostname', 25)
)()’ is the correct form, passing a tuple representing an IP address,
but ‘socket.connect( 'hostname', 25 )()’ also works.
‘socket.connect_ex()’ and ‘socket.bind()’ are similarly easy-going.
2.0alpha1 tightened these functions up, but because the documentation
actually used the erroneous multiple argument form, many people wrote
code which would break with the stricter checking. GvR backed out the
changes in the face of public reaction, so for the *note socket: ef.
module, the documentation was fixed and the multiple argument form is
simply marked as deprecated; it `will' be tightened up again in a future
Python version.
The ‘\x’ escape in string literals now takes exactly 2 hex digits.
Previously it would consume all the hex digits following the ‘x’ and
take the lowest 8 bits of the result, so ‘\x123456’ was equivalent to
‘\x56’.
The *note AttributeError: 39b. and *note NameError: d7b. exceptions have
a more friendly error message, whose text will be something like ‘'Spam'
instance has no attribute 'eggs'’ or ‘name 'eggs' is not defined’.
Previously the error message was just the missing attribute name ‘eggs’,
and code written to take advantage of this fact will break in 2.0.
Some work has been done to make integers and long integers a bit more
interchangeable. In 1.5.2, large-file support was added for Solaris, to
allow reading files larger than 2 GiB; this made the ‘tell()’ method of
file objects return a long integer instead of a regular integer. Some
code would subtract two file offsets and attempt to use the result to
multiply a sequence or slice a string, but this raised a *note
TypeError: 192. In 2.0, long integers can be used to multiply or slice
a sequence, and it’ll behave as you’d intuitively expect it to; ‘3L *
'abc'’ produces ‘abcabcabc’, and ‘(0,1,2,3)[2L:4L]’ produces (2,3).
Long integers can also be used in various contexts where previously only
integers were accepted, such as in the ‘seek()’ method of file objects,
and in the formats supported by the ‘%’ operator (‘%d’, ‘%i’, ‘%x’,
etc.). For example, ‘"%d" % 2L**64’ will produce the string
‘18446744073709551616’.
The subtlest long integer change of all is that the *note str(): 330. of
a long integer no longer has a trailing ‘L’ character, though *note
repr(): 7cc. still includes it. The ‘L’ annoyed many people who wanted
to print long integers that looked just like regular integers, since
they had to go out of their way to chop off the character. This is no
longer a problem in 2.0, but code which does ‘str(longval)[:-1]’ and
assumes the ‘L’ is there, will now lose the final digit.
Taking the *note repr(): 7cc. of a float now uses a different formatting
precision than *note str(): 330. *note repr(): 7cc. uses ‘%.17g’ format
string for C’s ‘sprintf()’, while *note str(): 330. uses ‘%.12g’ as
before. The effect is that *note repr(): 7cc. may occasionally show
more decimal places than *note str(): 330, for certain numbers. For
example, the number 8.1 can’t be represented exactly in binary, so
‘repr(8.1)’ is ‘'8.0999999999999996'’, while str(8.1) is ‘'8.1'’.
The ‘-X’ command-line option, which turned all standard exceptions into
strings instead of classes, has been removed; the standard exceptions
will now always be classes. The ‘exceptions’ module containing the
standard exceptions was translated from Python to a built-in C module,
written by Barry Warsaw and Fredrik Lundh.
File: python.info, Node: Extending/Embedding Changes, Next: Distutils Making Modules Easy to Install, Prev: Porting to 2 0, Up: What’s New in Python 2 0
1.17.11 Extending/Embedding Changes
-----------------------------------
Some of the changes are under the covers, and will only be apparent to
people writing C extension modules or embedding a Python interpreter in
a larger application. If you aren’t dealing with Python’s C API, you
can safely skip this section.
The version number of the Python C API was incremented, so C extensions
compiled for 1.5.2 must be recompiled in order to work with 2.0. On
Windows, it’s not possible for Python 2.0 to import a third party
extension built for Python 1.5.x due to how Windows DLLs work, so Python
will raise an exception and the import will fail.
Users of Jim Fulton’s ExtensionClass module will be pleased to find out
that hooks have been added so that ExtensionClasses are now supported by
*note isinstance(): 44f. and *note issubclass(): 450. This means you no
longer have to remember to write code such as ‘if type(obj) ==
myExtensionClass’, but can use the more natural ‘if isinstance(obj,
myExtensionClass)’.
The ‘Python/importdl.c’ file, which was a mass of #ifdefs to support
dynamic loading on many different platforms, was cleaned up and
reorganised by Greg Stein. ‘importdl.c’ is now quite small, and
platform-specific code has been moved into a bunch of
‘Python/dynload_*.c’ files. Another cleanup: there were also a number
of ‘my*.h’ files in the Include/ directory that held various portability
hacks; they’ve been merged into a single file, ‘Include/pyport.h’.
Vladimir Marangozov’s long-awaited malloc restructuring was completed,
to make it easy to have the Python interpreter use a custom allocator
instead of C’s standard ‘malloc()’. For documentation, read the
comments in ‘Include/pymem.h’ and ‘Include/objimpl.h’. For the lengthy
discussions during which the interface was hammered out, see the Web
archives of the ‘patches’ and ‘python-dev’ lists at python.org.
Recent versions of the GUSI development environment for MacOS support
POSIX threads. Therefore, Python’s POSIX threading support now works on
the Macintosh. Threading support using the user-space GNU ‘pth’ library
was also contributed.
Threading support on Windows was enhanced, too. Windows supports thread
locks that use kernel objects only in case of contention; in the common
case when there’s no contention, they use simpler functions which are an
order of magnitude faster. A threaded version of Python 1.5.2 on NT is
twice as slow as an unthreaded version; with the 2.0 changes, the
difference is only 10%. These improvements were contributed by Yakov
Markovitch.
Python 2.0’s source now uses only ANSI C prototypes, so compiling Python
now requires an ANSI C compiler, and can no longer be done using a
compiler that only supports K&R C.
Previously the Python virtual machine used 16-bit numbers in its
bytecode, limiting the size of source files. In particular, this
affected the maximum size of literal lists and dictionaries in Python
source; occasionally people who are generating Python code would run
into this limit. A patch by Charles G. Waldman raises the limit from
‘2^16’ to ‘2^{32}’.
Three new convenience functions intended for adding constants to a
module’s dictionary at module initialization time were added:
‘PyModule_AddObject()’, ‘PyModule_AddIntConstant()’, and
‘PyModule_AddStringConstant()’. Each of these functions takes a module
object, a null-terminated C string containing the name to be added, and
a third argument for the value to be assigned to the name. This third
argument is, respectively, a Python object, a C long, or a C string.
A wrapper API was added for Unix-style signal handlers. ‘PyOS_getsig()’
gets a signal handler and ‘PyOS_setsig()’ will set a new handler.
File: python.info, Node: Distutils Making Modules Easy to Install, Next: XML Modules, Prev: Extending/Embedding Changes, Up: What’s New in Python 2 0
1.17.12 Distutils: Making Modules Easy to Install
-------------------------------------------------
Before Python 2.0, installing modules was a tedious affair – there was
no way to figure out automatically where Python is installed, or what
compiler options to use for extension modules. Software authors had to
go through an arduous ritual of editing Makefiles and configuration
files, which only really work on Unix and leave Windows and MacOS
unsupported. Python users faced wildly differing installation
instructions which varied between different extension packages, which
made administering a Python installation something of a chore.
The SIG for distribution utilities, shepherded by Greg Ward, has created
the Distutils, a system to make package installation much easier. They
form the *note distutils: 39. package, a new part of Python’s standard
library. In the best case, installing a Python module from source will
require the same steps: first you simply mean unpack the tarball or zip
archive, and the run “‘python setup.py install’”. The platform will be
automatically detected, the compiler will be recognized, C extension
modules will be compiled, and the distribution installed into the proper
directory. Optional command-line arguments provide more control over
the installation process, the distutils package offers many places to
override defaults – separating the build from the install, building or
installing in non-default directories, and more.
In order to use the Distutils, you need to write a ‘setup.py’ script.
For the simple case, when the software contains only .py files, a
minimal ‘setup.py’ can be just a few lines long:
from distutils.core import setup
setup (name = "foo", version = "1.0",
py_modules = ["module1", "module2"])
The ‘setup.py’ file isn’t much more complicated if the software consists
of a few packages:
from distutils.core import setup
setup (name = "foo", version = "1.0",
packages = ["package", "package.subpackage"])
A C extension can be the most complicated case; here’s an example taken
from the PyXML package:
from distutils.core import setup, Extension
expat_extension = Extension('xml.parsers.pyexpat',
define_macros = [('XML_NS', None)],
include_dirs = [ 'extensions/expat/xmltok',
'extensions/expat/xmlparse' ],
sources = [ 'extensions/pyexpat.c',
'extensions/expat/xmltok/xmltok.c',
'extensions/expat/xmltok/xmlrole.c', ]
)
setup (name = "PyXML", version = "0.5.4",
ext_modules =[ expat_extension ] )
The Distutils can also take care of creating source and binary
distributions. The “sdist” command, run by “‘python setup.py sdist’’,
builds a source distribution such as ‘foo-1.0.tar.gz’. Adding new
commands isn’t difficult, “bdist_rpm” and “bdist_wininst” commands have
already been contributed to create an RPM distribution and a Windows
installer for the software, respectively. Commands to create other
distribution formats such as Debian packages and Solaris ‘.pkg’ files
are in various stages of development.
All this is documented in a new manual, `Distributing Python Modules',
that joins the basic set of Python documentation.
File: python.info, Node: XML Modules, Next: Module changes, Prev: Distutils Making Modules Easy to Install, Up: What’s New in Python 2 0
1.17.13 XML Modules
-------------------
Python 1.5.2 included a simple XML parser in the form of the ‘xmllib’
module, contributed by Sjoerd Mullender. Since 1.5.2’s release, two
different interfaces for processing XML have become common: SAX2
(version 2 of the Simple API for XML) provides an event-driven interface
with some similarities to ‘xmllib’, and the DOM (Document Object Model)
provides a tree-based interface, transforming an XML document into a
tree of nodes that can be traversed and modified. Python 2.0 includes a
SAX2 interface and a stripped-down DOM interface as part of the *note
xml: 133. package. Here we will give a brief overview of these new
interfaces; consult the Python documentation or the source code for
complete details. The Python XML SIG is also working on improved
documentation.
* Menu:
* SAX2 Support::
* DOM Support::
* Relationship to PyXML::
File: python.info, Node: SAX2 Support, Next: DOM Support, Up: XML Modules
1.17.13.1 SAX2 Support
......................
SAX defines an event-driven interface for parsing XML. To use SAX, you
must write a SAX handler class. Handler classes inherit from various
classes provided by SAX, and override various methods that will then be
called by the XML parser. For example, the ‘startElement()’ and
‘endElement()’ methods are called for every starting and end tag
encountered by the parser, the ‘characters()’ method is called for every
chunk of character data, and so forth.
The advantage of the event-driven approach is that the whole document
doesn’t have to be resident in memory at any one time, which matters if
you are processing really huge documents. However, writing the SAX
handler class can get very complicated if you’re trying to modify the
document structure in some elaborate way.
For example, this little example program defines a handler that prints a
message for every starting and ending tag, and then parses the file
‘hamlet.xml’ using it:
from xml import sax
class SimpleHandler(sax.ContentHandler):
def startElement(self, name, attrs):
print 'Start of element:', name, attrs.keys()
def endElement(self, name):
print 'End of element:', name
# Create a parser object
parser = sax.make_parser()
# Tell it what handler to use
handler = SimpleHandler()
parser.setContentHandler( handler )
# Parse a file!
parser.parse( 'hamlet.xml' )
For more information, consult the Python documentation, or the XML HOWTO
at ‘http://pyxml.sourceforge.net/topics/howto/xml-howto.html’.
File: python.info, Node: DOM Support, Next: Relationship to PyXML, Prev: SAX2 Support, Up: XML Modules
1.17.13.2 DOM Support
.....................
The Document Object Model is a tree-based representation for an XML
document. A top-level ‘Document’ instance is the root of the tree, and
has a single child which is the top-level ‘Element’ instance. This
‘Element’ has children nodes representing character data and any
sub-elements, which may have further children of their own, and so
forth. Using the DOM you can traverse the resulting tree any way you
like, access element and attribute values, insert and delete nodes, and
convert the tree back into XML.
The DOM is useful for modifying XML documents, because you can create a
DOM tree, modify it by adding new nodes or rearranging subtrees, and
then produce a new XML document as output. You can also construct a DOM
tree manually and convert it to XML, which can be a more flexible way of
producing XML output than simply writing ‘’…‘ ’ to a file.
The DOM implementation included with Python lives in the *note
xml.dom.minidom: 135. module. It’s a lightweight implementation of the
Level 1 DOM with support for XML namespaces. The ‘parse()’ and
‘parseString()’ convenience functions are provided for generating a DOM
tree:
from xml.dom import minidom
doc = minidom.parse('hamlet.xml')
‘doc’ is a ‘Document’ instance. ‘Document’, like all the other DOM
classes such as ‘Element’ and ‘Text’, is a subclass of the ‘Node’ base
class. All the nodes in a DOM tree therefore support certain common
methods, such as ‘toxml()’ which returns a string containing the XML
representation of the node and its children. Each class also has
special methods of its own; for example, ‘Element’ and ‘Document’
instances have a method to find all child elements with a given tag
name. Continuing from the previous 2-line example:
perslist = doc.getElementsByTagName( 'PERSONA' )
print perslist[0].toxml()
print perslist[1].toxml()
For the `Hamlet' XML file, the above few lines output:
CLAUDIUS, king of Denmark.
HAMLET, son to the late, and nephew to the present king.
The root element of the document is available as ‘doc.documentElement’,
and its children can be easily modified by deleting, adding, or removing
nodes:
root = doc.documentElement
# Remove the first child
root.removeChild( root.childNodes[0] )
# Move the new first child to the end
root.appendChild( root.childNodes[0] )
# Insert the new first child (originally,
# the third child) before the 20th child.
root.insertBefore( root.childNodes[0], root.childNodes[20] )
Again, I will refer you to the Python documentation for a complete
listing of the different ‘Node’ classes and their various methods.
File: python.info, Node: Relationship to PyXML, Prev: DOM Support, Up: XML Modules
1.17.13.3 Relationship to PyXML
...............................
The XML Special Interest Group has been working on XML-related Python
code for a while. Its code distribution, called PyXML, is available
from the SIG’s Web pages at
‘https://www.python.org/community/sigs/current/xml-sig’. The PyXML
distribution also used the package name ‘xml’. If you’ve written
programs that used PyXML, you’re probably wondering about its
compatibility with the 2.0 *note xml: 133. package.
The answer is that Python 2.0’s *note xml: 133. package isn’t compatible
with PyXML, but can be made compatible by installing a recent version
PyXML. Many applications can get by with the XML support that is
included with Python 2.0, but more complicated applications will require
that the full PyXML package will be installed. When installed, PyXML
versions 0.6.0 or greater will replace the *note xml: 133. package
shipped with Python, and will be a strict superset of the standard
package, adding a bunch of additional features. Some of the additional
features in PyXML include:
* 4DOM, a full DOM implementation from FourThought, Inc.
* The xmlproc validating parser, written by Lars Marius Garshol.
* The ‘sgmlop’ parser accelerator module, written by Fredrik Lundh.
File: python.info, Node: Module changes, Next: New modules, Prev: XML Modules, Up: What’s New in Python 2 0
1.17.14 Module changes
----------------------
Lots of improvements and bugfixes were made to Python’s extensive
standard library; some of the affected modules include *note readline:
de, ‘ConfigParser’, *note cgi: 16, *note calendar: 15, *note posix: d1,
*note readline: de, ‘xmllib’, *note aifc: 5, ‘chunk, wave’, *note
random: dc, *note shelve: e7, and *note nntplib: c0. Consult the CVS
logs for the exact patch-by-patch details.
Brian Gallew contributed OpenSSL support for the *note socket: ef.
module. OpenSSL is an implementation of the Secure Socket Layer, which
encrypts the data being sent over a socket. When compiling Python, you
can edit ‘Modules/Setup’ to include SSL support, which adds an
additional function to the *note socket: ef. module: ‘socket.ssl(socket,
keyfile, certfile)’, which takes a socket object and returns an SSL
socket. The ‘httplib’ and *note urllib: 11d. modules were also changed
to support ‘https://’ URLs, though no one has implemented FTP or SMTP
over SSL.
The ‘httplib’ module has been rewritten by Greg Stein to support
HTTP/1.1. Backward compatibility with the 1.5 version of ‘httplib’ is
provided, though using HTTP/1.1 features such as pipelining will require
rewriting code to use a different set of interfaces.
The ‘Tkinter’ module now supports Tcl/Tk version 8.1, 8.2, or 8.3, and
support for the older 7.x versions has been dropped. The Tkinter module
now supports displaying Unicode strings in Tk widgets. Also, Fredrik
Lundh contributed an optimization which makes operations like
‘create_line’ and ‘create_polygon’ much faster, especially when using
lots of coordinates.
The *note curses: 2c. module has been greatly extended, starting from
Oliver Andrich’s enhanced version, to provide many additional functions
from ncurses and SYSV curses, such as colour, alternative character set
support, pads, and mouse support. This means the module is no longer
compatible with operating systems that only have BSD curses, but there
don’t seem to be any currently maintained OSes that fall into this
category.
As mentioned in the earlier discussion of 2.0’s Unicode support, the
underlying implementation of the regular expressions provided by the
*note re: dd. module has been changed. SRE, a new regular expression
engine written by Fredrik Lundh and partially funded by Hewlett Packard,
supports matching against both 8-bit strings and Unicode strings.
File: python.info, Node: New modules, Next: IDLE Improvements, Prev: Module changes, Up: What’s New in Python 2 0
1.17.15 New modules
-------------------
A number of new modules were added. We’ll simply list them with brief
descriptions; consult the 2.0 documentation for the details of a
particular module.
* *note atexit: c.: For registering functions to be called before the
Python interpreter exits. Code that currently sets ‘sys.exitfunc’
directly should be changed to use the *note atexit: c. module
instead, importing *note atexit: c. and calling *note
atexit.register(): e85. with the function to be called on exit.
(Contributed by Skip Montanaro.)
* *note codecs: 1c, ‘encodings’, *note unicodedata: 11a.: Added as
part of the new Unicode support.
* *note filecmp: 7f.: Supersedes the old ‘cmp’, ‘cmpcache’ and
‘dircmp’ modules, which have now become deprecated. (Contributed
by Gordon MacMillan and Moshe Zadka.)
* *note gettext: 89.: This module provides internationalization
(I18N) and localization (L10N) support for Python programs by
providing an interface to the GNU gettext message catalog library.
(Integrated by Barry Warsaw, from separate contributions by Martin
von Löwis, Peter Funk, and James Henstridge.)
* ‘linuxaudiodev’: Support for the ‘/dev/audio’ device on Linux, a
twin to the existing ‘sunaudiodev’ module. (Contributed by Peter
Bosch, with fixes by Jeremy Hylton.)
* *note mmap: b3.: An interface to memory-mapped files on both
Windows and Unix. A file’s contents can be mapped directly into
memory, at which point it behaves like a mutable string, so its
contents can be read and modified. They can even be passed to
functions that expect ordinary strings, such as the *note re: dd.
module. (Contributed by Sam Rushing, with some extensions by A.M.
Kuchling.)
* ‘pyexpat’: An interface to the Expat XML parser. (Contributed by
Paul Prescod.)
* ‘robotparser’: Parse a ‘robots.txt’ file, which is used for writing
Web spiders that politely avoid certain areas of a Web site. The
parser accepts the contents of a ‘robots.txt’ file, builds a set of
rules from it, and can then answer questions about the fetchability
of a given URL. (Contributed by Skip Montanaro.)
* *note tabnanny: 100.: A module/script to check Python source code
for ambiguous indentation. (Contributed by Tim Peters.)
* ‘UserString’: A base class useful for deriving objects that behave
like strings.
* *note webbrowser: 129.: A module that provides a platform
independent way to launch a web browser on a specific URL. For each
platform, various browsers are tried in a specific order. The user
can alter which browser is launched by setting the `BROWSER'
environment variable. (Originally inspired by Eric S. Raymond’s
patch to *note urllib: 11d. which added similar functionality, but
the final module comes from code originally implemented by Fred
Drake as ‘Tools/idle/BrowserControl.py’, and adapted for the
standard library by Fred.)
* ‘_winreg’: An interface to the Windows registry. ‘_winreg’ is an
adaptation of functions that have been part of PythonWin since
1995, but has now been added to the core distribution, and enhanced
to support Unicode. ‘_winreg’ was written by Bill Tutt and Mark
Hammond.
* *note zipfile: 142.: A module for reading and writing ZIP-format
archives. These are archives produced by ‘PKZIP’ on DOS/Windows or
‘zip’ on Unix, not to be confused with ‘gzip’-format files (which
are supported by the *note gzip: 8c. module) (Contributed by James
C. Ahlstrom.)
* ‘imputil’: A module that provides a simpler way for writing
customized import hooks, in comparison to the existing ‘ihooks’
module. (Implemented by Greg Stein, with much discussion on
python-dev along the way.)
File: python.info, Node: IDLE Improvements, Next: Deleted and Deprecated Modules, Prev: New modules, Up: What’s New in Python 2 0
1.17.16 IDLE Improvements
-------------------------
IDLE is the official Python cross-platform IDE, written using Tkinter.
Python 2.0 includes IDLE 0.6, which adds a number of new features and
improvements. A partial list:
* UI improvements and optimizations, especially in the area of syntax
highlighting and auto-indentation.
* The class browser now shows more information, such as the top level
functions in a module.
* Tab width is now a user settable option. When opening an existing
Python file, IDLE automatically detects the indentation
conventions, and adapts.
* There is now support for calling browsers on various platforms,
used to open the Python documentation in a browser.
* IDLE now has a command line, which is largely similar to the
vanilla Python interpreter.
* Call tips were added in many places.
* IDLE can now be installed as a package.
* In the editor window, there is now a line/column bar at the bottom.
* Three new keystroke commands: Check module (‘Alt-F5’), Import
module (‘F5’) and Run script (‘Ctrl-F5’).
File: python.info, Node: Deleted and Deprecated Modules, Next: Acknowledgements<8>, Prev: IDLE Improvements, Up: What’s New in Python 2 0
1.17.17 Deleted and Deprecated Modules
--------------------------------------
A few modules have been dropped because they’re obsolete, or because
there are now better ways to do the same thing. The ‘stdwin’ module is
gone; it was for a platform-independent windowing toolkit that’s no
longer developed.
A number of modules have been moved to the ‘lib-old’ subdirectory:
‘cmp’, ‘cmpcache’, ‘dircmp’, ‘dump’, ‘find’, ‘grep’, ‘packmail’, ‘poly’,
‘util’, ‘whatsound’, ‘zmod’. If you have code which relies on a module
that’s been moved to ‘lib-old’, you can simply add that directory to
‘sys.path’ to get them back, but you’re encouraged to update any code
that uses these modules.
File: python.info, Node: Acknowledgements<8>, Prev: Deleted and Deprecated Modules, Up: What’s New in Python 2 0
1.17.18 Acknowledgements
------------------------
The authors would like to thank the following people for offering
suggestions on various drafts of this article: David Bolen, Mark
Hammond, Gregg Hauser, Jeremy Hylton, Fredrik Lundh, Detlef Lannert,
Aahz Maruch, Skip Montanaro, Vladimir Marangozov, Tobias Polzin, Guido
van Rossum, Neil Schemenauer, and Russ Schmidt.
The “Changelog” is an HTML version of the file built(1) from the
contents of the Misc/NEWS.d(2) directory tree, which contains `all'
nontrivial changes to Python for the current version.
---------- Footnotes ----------
(1) https://pypi.org/project/blurb
(2) https://github.com/python/cpython/tree/3.8/Misc/NEWS.d
File: python.info, Node: Changelog, Prev: What’s New in Python 2 0, Up: What’s New in Python
1.18 Changelog
==============
`The NEWS file is not available.'
File: python.info, Node: The Python Tutorial, Next: Python Setup and Usage, Prev: What’s New in Python, Up: Top
2 The Python Tutorial
*********************
Python is an easy to learn, powerful programming language. It has
efficient high-level data structures and a simple but effective approach
to object-oriented programming. Python’s elegant syntax and dynamic
typing, together with its interpreted nature, make it an ideal language
for scripting and rapid application development in many areas on most
platforms.
The Python interpreter and the extensive standard library are freely
available in source or binary form for all major platforms from the
Python Web site, ‘https://www.python.org/’, and may be freely
distributed. The same site also contains distributions of and pointers
to many free third party Python modules, programs and tools, and
additional documentation.
The Python interpreter is easily extended with new functions and data
types implemented in C or C++ (or other languages callable from C).
Python is also suitable as an extension language for customizable
applications.
This tutorial introduces the reader informally to the basic concepts and
features of the Python language and system. It helps to have a Python
interpreter handy for hands-on experience, but all examples are
self-contained, so the tutorial can be read off-line as well.
For a description of standard objects and modules, see *note The Python
Standard Library: e8e. *note The Python Language Reference: e8f. gives
a more formal definition of the language. To write extensions in C or
C++, read *note Extending and Embedding the Python Interpreter: e90. and
*note Python/C API Reference Manual: e91. There are also several books
covering Python in depth.
This tutorial does not attempt to be comprehensive and cover every
single feature, or even every commonly used feature. Instead, it
introduces many of Python’s most noteworthy features, and will give you
a good idea of the language’s flavor and style. After reading it, you
will be able to read and write Python modules and programs, and you will
be ready to learn more about the various Python library modules
described in *note The Python Standard Library: e8e.
The *note Glossary: e92. is also worth going through.
* Menu:
* Whetting Your Appetite::
* Using the Python Interpreter::
* An Informal Introduction to Python::
* More Control Flow Tools::
* Data Structures::
* Modules::
* Input and Output::
* Errors and Exceptions::
* Classes::
* Brief Tour of the Standard Library::
* Brief Tour of the Standard Library — Part II::
* Virtual Environments and Packages::
* What Now?::
* Interactive Input Editing and History Substitution::
* Floating Point Arithmetic; Issues and Limitations: Floating Point Arithmetic Issues and Limitations.
* Appendix::
File: python.info, Node: Whetting Your Appetite, Next: Using the Python Interpreter, Up: The Python Tutorial
2.1 Whetting Your Appetite
==========================
If you do much work on computers, eventually you find that there’s some
task you’d like to automate. For example, you may wish to perform a
search-and-replace over a large number of text files, or rename and
rearrange a bunch of photo files in a complicated way. Perhaps you’d
like to write a small custom database, or a specialized GUI application,
or a simple game.
If you’re a professional software developer, you may have to work with
several C/C++/Java libraries but find the usual
write/compile/test/re-compile cycle is too slow. Perhaps you’re writing
a test suite for such a library and find writing the testing code a
tedious task. Or maybe you’ve written a program that could use an
extension language, and you don’t want to design and implement a whole
new language for your application.
Python is just the language for you.
You could write a Unix shell script or Windows batch files for some of
these tasks, but shell scripts are best at moving around files and
changing text data, not well-suited for GUI applications or games. You
could write a C/C++/Java program, but it can take a lot of development
time to get even a first-draft program. Python is simpler to use,
available on Windows, Mac OS X, and Unix operating systems, and will
help you get the job done more quickly.
Python is simple to use, but it is a real programming language, offering
much more structure and support for large programs than shell scripts or
batch files can offer. On the other hand, Python also offers much more
error checking than C, and, being a `very-high-level language', it has
high-level data types built in, such as flexible arrays and
dictionaries. Because of its more general data types Python is
applicable to a much larger problem domain than Awk or even Perl, yet
many things are at least as easy in Python as in those languages.
Python allows you to split your program into modules that can be reused
in other Python programs. It comes with a large collection of standard
modules that you can use as the basis of your programs — or as examples
to start learning to program in Python. Some of these modules provide
things like file I/O, system calls, sockets, and even interfaces to
graphical user interface toolkits like Tk.
Python is an interpreted language, which can save you considerable time
during program development because no compilation and linking is
necessary. The interpreter can be used interactively, which makes it
easy to experiment with features of the language, to write throw-away
programs, or to test functions during bottom-up program development. It
is also a handy desk calculator.
Python enables programs to be written compactly and readably. Programs
written in Python are typically much shorter than equivalent C, C++, or
Java programs, for several reasons:
* the high-level data types allow you to express complex operations
in a single statement;
* statement grouping is done by indentation instead of beginning and
ending brackets;
* no variable or argument declarations are necessary.
Python is `extensible': if you know how to program in C it is easy to
add a new built-in function or module to the interpreter, either to
perform critical operations at maximum speed, or to link Python programs
to libraries that may only be available in binary form (such as a
vendor-specific graphics library). Once you are really hooked, you can
link the Python interpreter into an application written in C and use it
as an extension or command language for that application.
By the way, the language is named after the BBC show “Monty Python’s
Flying Circus” and has nothing to do with reptiles. Making references
to Monty Python skits in documentation is not only allowed, it is
encouraged!
Now that you are all excited about Python, you’ll want to examine it in
some more detail. Since the best way to learn a language is to use it,
the tutorial invites you to play with the Python interpreter as you
read.
In the next chapter, the mechanics of using the interpreter are
explained. This is rather mundane information, but essential for trying
out the examples shown later.
The rest of the tutorial introduces various features of the Python
language and system through examples, beginning with simple expressions,
statements and data types, through functions and modules, and finally
touching upon advanced concepts like exceptions and user-defined
classes.
File: python.info, Node: Using the Python Interpreter, Next: An Informal Introduction to Python, Prev: Whetting Your Appetite, Up: The Python Tutorial
2.2 Using the Python Interpreter
================================
* Menu:
* Invoking the Interpreter::
* The Interpreter and Its Environment::
File: python.info, Node: Invoking the Interpreter, Next: The Interpreter and Its Environment, Up: Using the Python Interpreter
2.2.1 Invoking the Interpreter
------------------------------
The Python interpreter is usually installed as
‘/usr/local/bin/python3.8’ on those machines where it is available;
putting ‘/usr/local/bin’ in your Unix shell’s search path makes it
possible to start it by typing the command:
python3.8
to the shell. (1) Since the choice of the directory where the
interpreter lives is an installation option, other places are possible;
check with your local Python guru or system administrator. (E.g.,
‘/usr/local/python’ is a popular alternative location.)
On Windows machines where you have installed Python from the *note
Microsoft Store: e9b, the ‘python3.8’ command will be available. If you
have the *note py.exe launcher: 98f. installed, you can use the ‘py’
command. See *note Excursus; Setting environment variables: e9c. for
other ways to launch Python.
Typing an end-of-file character (‘Control-D’ on Unix, ‘Control-Z’ on
Windows) at the primary prompt causes the interpreter to exit with a
zero exit status. If that doesn’t work, you can exit the interpreter by
typing the following command: ‘quit()’.
The interpreter’s line-editing features include interactive editing,
history substitution and code completion on systems that support the GNU
Readline(2) library. Perhaps the quickest check to see whether command
line editing is supported is typing ‘Control-P’ to the first Python
prompt you get. If it beeps, you have command line editing; see
Appendix *note Interactive Input Editing and History Substitution: e9d.
for an introduction to the keys. If nothing appears to happen, or if
‘^P’ is echoed, command line editing isn’t available; you’ll only be
able to use backspace to remove characters from the current line.
The interpreter operates somewhat like the Unix shell: when called with
standard input connected to a tty device, it reads and executes commands
interactively; when called with a file name argument or with a file as
standard input, it reads and executes a `script' from that file.
A second way of starting the interpreter is ‘python -c command [arg]
...’, which executes the statement(s) in `command', analogous to the
shell’s *note -c: e9e. option. Since Python statements often contain
spaces or other characters that are special to the shell, it is usually
advised to quote `command' in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked
using ‘python -m module [arg] ...’, which executes the source file for
`module' as if you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the
script and enter interactive mode afterwards. This can be done by
passing *note -i: e1f. before the script.
All command line options are described in *note Command line and
environment: e9f.
* Menu:
* Argument Passing::
* Interactive Mode::
---------- Footnotes ----------
(1) (1) On Unix, the Python 3.x interpreter is by default not
installed with the executable named ‘python’, so that it does not
conflict with a simultaneously installed Python 2.x executable.
(2) https://tiswww.case.edu/php/chet/readline/rltop.html
File: python.info, Node: Argument Passing, Next: Interactive Mode, Up: Invoking the Interpreter
2.2.1.1 Argument Passing
........................
When known to the interpreter, the script name and additional arguments
thereafter are turned into a list of strings and assigned to the ‘argv’
variable in the ‘sys’ module. You can access this list by executing
‘import sys’. The length of the list is at least one; when no script
and no arguments are given, ‘sys.argv[0]’ is an empty string. When the
script name is given as ‘'-'’ (meaning standard input), ‘sys.argv[0]’ is
set to ‘'-'’. When *note -c: e9e. `command' is used, ‘sys.argv[0]’ is
set to ‘'-c'’. When *note -m: 337. `module' is used, ‘sys.argv[0]’ is
set to the full name of the located module. Options found after *note
-c: e9e. `command' or *note -m: 337. `module' are not consumed by the
Python interpreter’s option processing but left in ‘sys.argv’ for the
command or module to handle.
File: python.info, Node: Interactive Mode, Prev: Argument Passing, Up: Invoking the Interpreter
2.2.1.2 Interactive Mode
........................
When commands are read from a tty, the interpreter is said to be in
`interactive mode'. In this mode it prompts for the next command with
the `primary prompt', usually three greater-than signs (‘>>>’); for
continuation lines it prompts with the `secondary prompt', by default
three dots (‘...’). The interpreter prints a welcome message stating
its version number and a copyright notice before printing the first
prompt:
$ python3.8
Python 3.8 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Continuation lines are needed when entering a multi-line construct. As
an example, take a look at this *note if: de7. statement:
>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!
For more on interactive mode, see *note Interactive Mode: ea3.
File: python.info, Node: The Interpreter and Its Environment, Prev: Invoking the Interpreter, Up: Using the Python Interpreter
2.2.2 The Interpreter and Its Environment
-----------------------------------------
* Menu:
* Source Code Encoding::
File: python.info, Node: Source Code Encoding, Up: The Interpreter and Its Environment
2.2.2.1 Source Code Encoding
............................
By default, Python source files are treated as encoded in UTF-8. In
that encoding, characters of most languages in the world can be used
simultaneously in string literals, identifiers and comments — although
the standard library only uses ASCII characters for identifiers, a
convention that any portable code should follow. To display all these
characters properly, your editor must recognize that the file is UTF-8,
and it must use a font that supports all the characters in the file.
To declare an encoding other than the default one, a special comment
line should be added as the `first' line of the file. The syntax is as
follows:
# -*- coding: encoding -*-
where `encoding' is one of the valid *note codecs: 1c. supported by
Python.
For example, to declare that Windows-1252 encoding is to be used, the
first line of your source code file should be:
# -*- coding: cp1252 -*-
One exception to the `first line' rule is when the source code starts
with a *note UNIX “shebang” line: ea8. In this case, the encoding
declaration should be added as the second line of the file. For
example:
#!/usr/bin/env python3
# -*- coding: cp1252 -*-
File: python.info, Node: An Informal Introduction to Python, Next: More Control Flow Tools, Prev: Using the Python Interpreter, Up: The Python Tutorial
2.3 An Informal Introduction to Python
======================================
In the following examples, input and output are distinguished by the
presence or absence of prompts (*note >>>: eac. and *note …: ead.): to
repeat the example, you must type everything after the prompt, when the
prompt appears; lines that do not begin with a prompt are output from
the interpreter. Note that a secondary prompt on a line by itself in an
example means you must type a blank line; this is used to end a
multi-line command.
Many of the examples in this manual, even those entered at the
interactive prompt, include comments. Comments in Python start with the
hash character, ‘#’, and extend to the end of the physical line. A
comment may appear at the start of a line or following whitespace or
code, but not within a string literal. A hash character within a string
literal is just a hash character. Since comments are to clarify code
and are not interpreted by Python, they may be omitted when typing in
examples.
Some examples:
# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."
* Menu:
* Using Python as a Calculator::
* First Steps Towards Programming::
File: python.info, Node: Using Python as a Calculator, Next: First Steps Towards Programming, Up: An Informal Introduction to Python
2.3.1 Using Python as a Calculator
----------------------------------
Let’s try some simple Python commands. Start the interpreter and wait
for the primary prompt, ‘>>>’. (It shouldn’t take long.)
* Menu:
* Numbers::
* Strings::
* Lists::
File: python.info, Node: Numbers, Next: Strings, Up: Using Python as a Calculator
2.3.1.1 Numbers
...............
The interpreter acts as a simple calculator: you can type an expression
at it and it will write the value. Expression syntax is
straightforward: the operators ‘+’, ‘-’, ‘*’ and ‘/’ work just like in
most other languages (for example, Pascal or C); parentheses (‘()’) can
be used for grouping. For example:
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
The integer numbers (e.g. ‘2’, ‘4’, ‘20’) have type *note int: 184, the
ones with a fractional part (e.g. ‘5.0’, ‘1.6’) have type *note float:
187. We will see more about numeric types later in the tutorial.
Division (‘/’) always returns a float. To do *note floor division: eb2.
and get an integer result (discarding any fractional result) you can use
the ‘//’ operator; to calculate the remainder you can use ‘%’:
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # result * divisor + remainder
17
With Python, it is possible to use the ‘**’ operator to calculate powers
(1):
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
The equal sign (‘=’) is used to assign a value to a variable.
Afterwards, no result is displayed before the next interactive prompt:
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
If a variable is not “defined” (assigned a value), trying to use it will
give you an error:
>>> n # try to access an undefined variable
Traceback (most recent call last):
File "", line 1, in
NameError: name 'n' is not defined
There is full support for floating point; operators with mixed type
operands convert the integer operand to floating point:
>>> 4 * 3.75 - 1
14.0
In interactive mode, the last printed expression is assigned to the
variable ‘_’. This means that when you are using Python as a desk
calculator, it is somewhat easier to continue calculations, for example:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
This variable should be treated as read-only by the user. Don’t
explicitly assign a value to it — you would create an independent local
variable with the same name masking the built-in variable with its magic
behavior.
In addition to *note int: 184. and *note float: 187, Python supports
other types of numbers, such as *note Decimal: 188. and *note Fraction:
185. Python also has built-in support for *note complex numbers: eb3,
and uses the ‘j’ or ‘J’ suffix to indicate the imaginary part (e.g.
‘3+5j’).
---------- Footnotes ----------
(1) (1) Since ‘**’ has higher precedence than ‘-’, ‘-3**2’ will be
interpreted as ‘-(3**2)’ and thus result in ‘-9’. To avoid this and get
‘9’, you can use ‘(-3)**2’.
File: python.info, Node: Strings, Next: Lists, Prev: Numbers, Up: Using Python as a Calculator
2.3.1.2 Strings
...............
Besides numbers, Python can also manipulate strings, which can be
expressed in several ways. They can be enclosed in single quotes
(‘'...'’) or double quotes (‘"..."’) with the same result (1). ‘\’ can
be used to escape quotes:
>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
In the interactive interpreter, the output string is enclosed in quotes
and special characters are escaped with backslashes. While this might
sometimes look different from the input (the enclosing quotes could
change), the two strings are equivalent. The string is enclosed in
double quotes if the string contains a single quote and no double
quotes, otherwise it is enclosed in single quotes. The *note print():
886. function produces a more readable output, by omitting the enclosing
quotes and by printing escaped and special characters:
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line.
If you don’t want characters prefaced by ‘\’ to be interpreted as
special characters, you can use `raw strings' by adding an ‘r’ before
the first quote:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
String literals can span multiple lines. One way is using
triple-quotes: ‘"""..."""’ or ‘'''...'''’. End of lines are
automatically included in the string, but it’s possible to prevent this
by adding a ‘\’ at the end of the line. The following example:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
produces the following output (note that the initial newline is not
included):
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
Strings can be concatenated (glued together) with the ‘+’ operator, and
repeated with ‘*’:
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
Two or more `string literals' (i.e. the ones enclosed between quotes)
next to each other are automatically concatenated.
>>> 'Py' 'thon'
'Python'
This feature is particularly useful when you want to break long strings:
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
This only works with two literals though, not with variables or
expressions:
>>> prefix = 'Py'
>>> prefix 'thon' # can't concatenate a variable and a string literal
File "", line 1
prefix 'thon'
^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "", line 1
('un' * 3) 'ium'
^
SyntaxError: invalid syntax
If you want to concatenate variables or a variable and a literal, use
‘+’:
>>> prefix + 'thon'
'Python'
Strings can be `indexed' (subscripted), with the first character having
index 0. There is no separate character type; a character is simply a
string of size one:
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'
Note that since -0 is the same as 0, negative indices start from -1.
In addition to indexing, `slicing' is also supported. While indexing is
used to obtain individual characters, `slicing' allows you to obtain
substring:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
Note how the start is always included, and the end always excluded.
This makes sure that ‘s[:i] + s[i:]’ is always equal to ‘s’:
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
Slice indices have useful defaults; an omitted first index defaults to
zero, an omitted second index defaults to the size of the string being
sliced.
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
One way to remember how slices work is to think of the indices as
pointing `between' characters, with the left edge of the first character
numbered 0. Then the right edge of the last character of a string of
`n' characters has index `n', for example:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
The first row of numbers gives the position of the indices 0…6 in the
string; the second row gives the corresponding negative indices. The
slice from `i' to `j' consists of all characters between the edges
labeled `i' and `j', respectively.
For non-negative indices, the length of a slice is the difference of the
indices, if both are within bounds. For example, the length of
‘word[1:3]’ is 2.
Attempting to use an index that is too large will result in an error:
>>> word[42] # the word only has 6 characters
Traceback (most recent call last):
File "", line 1, in
IndexError: string index out of range
However, out of range slice indexes are handled gracefully when used for
slicing:
>>> word[4:42]
'on'
>>> word[42:]
''
Python strings cannot be changed — they are *note immutable: eb6.
Therefore, assigning to an indexed position in the string results in an
error:
>>> word[0] = 'J'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
If you need a different string, you should create a new one:
>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'
The built-in function *note len(): 150. returns the length of a string:
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
See also
........
*note Text Sequence Type — str: eb7.
Strings are examples of `sequence types', and support the common
operations supported by such types.
*note String Methods: eb8.
Strings support a large number of methods for basic transformations
and searching.
*note Formatted string literals: 15c.
String literals that have embedded expressions.
*note Format String Syntax: d1a.
Information about string formatting with *note str.format(): 4da.
*note printf-style String Formatting: eb9.
The old formatting operations invoked when strings are the left
operand of the ‘%’ operator are described in more detail here.
---------- Footnotes ----------
(1) (2) Unlike other languages, special characters such as ‘\n’ have
the same meaning with both single (‘'...'’) and double (‘"..."’) quotes.
The only difference between the two is that within single quotes you
don’t need to escape ‘"’ (but you have to escape ‘\'’) and vice versa.
File: python.info, Node: Lists, Prev: Strings, Up: Using Python as a Calculator
2.3.1.3 Lists
.............
Python knows a number of `compound' data types, used to group together
other values. The most versatile is the `list', which can be written as
a list of comma-separated values (items) between square brackets. Lists
might contain items of different types, but usually the items all have
the same type.
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
Like strings (and all other built-in *note sequence: ebc. types), lists
can be indexed and sliced:
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
All slice operations return a new list containing the requested
elements. This means that the following slice returns a *note shallow
copy: ebd. of the list:
>>> squares[:]
[1, 4, 9, 16, 25]
Lists also support operations like concatenation:
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Unlike strings, which are *note immutable: eb6, lists are a *note
mutable: ebe. type, i.e. it is possible to change their content:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
You can also add new items at the end of the list, by using the
‘append()’ `method' (we will see more about methods later):
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
Assignment to slices is also possible, and this can even change the size
of the list or clear it entirely:
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
The built-in function *note len(): 150. also applies to lists:
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
It is possible to nest lists (create lists containing other lists), for
example:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
File: python.info, Node: First Steps Towards Programming, Prev: Using Python as a Calculator, Up: An Informal Introduction to Python
2.3.2 First Steps Towards Programming
-------------------------------------
Of course, we can use Python for more complicated tasks than adding two
and two together. For instance, we can write an initial sub-sequence of
the Fibonacci series(1) as follows:
>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while a < 10:
... print(a)
... a, b = b, a+b
...
0
1
1
2
3
5
8
This example introduces several new features.
* The first line contains a `multiple assignment': the variables ‘a’
and ‘b’ simultaneously get the new values 0 and 1. On the last
line this is used again, demonstrating that the expressions on the
right-hand side are all evaluated first before any of the
assignments take place. The right-hand side expressions are
evaluated from the left to the right.
* The *note while: ec1. loop executes as long as the condition (here:
‘a < 10’) remains true. In Python, like in C, any non-zero integer
value is true; zero is false. The condition may also be a string
or list value, in fact any sequence; anything with a non-zero
length is true, empty sequences are false. The test used in the
example is a simple comparison. The standard comparison operators
are written the same as in C: ‘<’ (less than), ‘>’ (greater than),
‘==’ (equal to), ‘<=’ (less than or equal to), ‘>=’ (greater than
or equal to) and ‘!=’ (not equal to).
* The `body' of the loop is `indented': indentation is Python’s way
of grouping statements. At the interactive prompt, you have to
type a tab or space(s) for each indented line. In practice you
will prepare more complicated input for Python with a text editor;
all decent text editors have an auto-indent facility. When a
compound statement is entered interactively, it must be followed by
a blank line to indicate completion (since the parser cannot guess
when you have typed the last line). Note that each line within a
basic block must be indented by the same amount.
* The *note print(): 886. function writes the value of the
argument(s) it is given. It differs from just writing the
expression you want to write (as we did earlier in the calculator
examples) in the way it handles multiple arguments, floating point
quantities, and strings. Strings are printed without quotes, and a
space is inserted between items, so you can format things nicely,
like this:
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536
The keyword argument `end' can be used to avoid the newline after
the output, or end the output with a different string:
>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/Fibonacci_number
File: python.info, Node: More Control Flow Tools, Next: Data Structures, Prev: An Informal Introduction to Python, Up: The Python Tutorial
2.4 More Control Flow Tools
===========================
Besides the *note while: ec1. statement just introduced, Python uses the
usual flow control statements known from other languages, with some
twists.
* Menu:
* if Statements::
* for Statements::
* The range() Function: The range Function.
* break and continue Statements, and else Clauses on Loops: break and continue Statements and else Clauses on Loops.
* pass Statements::
* Defining Functions::
* More on Defining Functions::
* Intermezzo; Coding Style: Intermezzo Coding Style.
File: python.info, Node: if Statements, Next: for Statements, Up: More Control Flow Tools
2.4.1 ‘if’ Statements
---------------------
Perhaps the most well-known statement type is the *note if: de7.
statement. For example:
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
There can be zero or more *note elif: ec7. parts, and the *note else:
ec8. part is optional. The keyword ‘‘elif’’ is short for ‘else if’, and
is useful to avoid excessive indentation. An ‘if’ … ‘elif’ … ‘elif’ …
sequence is a substitute for the ‘switch’ or ‘case’ statements found in
other languages.
File: python.info, Node: for Statements, Next: The range Function, Prev: if Statements, Up: More Control Flow Tools
2.4.2 ‘for’ Statements
----------------------
The *note for: c30. statement in Python differs a bit from what you may
be used to in C or Pascal. Rather than always iterating over an
arithmetic progression of numbers (like in Pascal), or giving the user
the ability to define both the iteration step and halting condition (as
C), Python’s ‘for’ statement iterates over the items of any sequence (a
list or a string), in the order that they appear in the sequence. For
example (no pun intended):
>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
Code that modifies a collection while iterating over that same
collection can be tricky to get right. Instead, it is usually more
straight-forward to loop over a copy of the collection or to create a
new collection:
# Strategy: Iterate over a copy
for user, status in users.copy().items():
if status == 'inactive':
del users[user]
# Strategy: Create a new collection
active_users = {}
for user, status in users.items():
if status == 'active':
active_users[user] = status
File: python.info, Node: The range Function, Next: break and continue Statements and else Clauses on Loops, Prev: for Statements, Up: More Control Flow Tools
2.4.3 The ‘range()’ Function
----------------------------
If you do need to iterate over a sequence of numbers, the built-in
function *note range(): 9be. comes in handy. It generates arithmetic
progressions:
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
The given end point is never part of the generated sequence; ‘range(10)’
generates 10 values, the legal indices for items of a sequence of length
10. It is possible to let the range start at another number, or to
specify a different increment (even negative; sometimes this is called
the ‘step’):
range(5, 10)
5, 6, 7, 8, 9
range(0, 10, 3)
0, 3, 6, 9
range(-10, -100, -30)
-10, -40, -70
To iterate over the indices of a sequence, you can combine *note
range(): 9be. and *note len(): 150. as follows:
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb
In most such cases, however, it is convenient to use the *note
enumerate(): de3. function, see *note Looping Techniques: ecd.
A strange thing happens if you just print a range:
>>> print(range(10))
range(0, 10)
In many ways the object returned by *note range(): 9be. behaves as if it
is a list, but in fact it isn’t. It is an object which returns the
successive items of the desired sequence when you iterate over it, but
it doesn’t really make the list, thus saving space.
We say such an object is *note iterable: bac, that is, suitable as a
target for functions and constructs that expect something from which
they can obtain successive items until the supply is exhausted. We have
seen that the *note for: c30. statement is such a construct, while an
example of a function that takes an iterable is *note sum(): 1e5.:
>>> sum(range(4)) # 0 + 1 + 2 + 3
6
Later we will see more functions that return iterables and take
iterables as arguments. Lastly, maybe you are curious about how to get
a list from a range. Here is the solution:
>>> list(range(4))
[0, 1, 2, 3]
In chapter *note Data Structures: ece, we will discuss in more detail
about *note list(): 262.
File: python.info, Node: break and continue Statements and else Clauses on Loops, Next: pass Statements, Prev: The range Function, Up: More Control Flow Tools
2.4.4 ‘break’ and ‘continue’ Statements, and ‘else’ Clauses on Loops
--------------------------------------------------------------------
The *note break: 2db. statement, like in C, breaks out of the innermost
enclosing *note for: c30. or *note while: ec1. loop.
Loop statements may have an ‘else’ clause; it is executed when the loop
terminates through exhaustion of the iterable (with *note for: c30.) or
when the condition becomes false (with *note while: ec1.), but not when
the loop is terminated by a *note break: 2db. statement. This is
exemplified by the following loop, which searches for prime numbers:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
(Yes, this is the correct code. Look closely: the ‘else’ clause belongs
to the *note for: c30. loop, `not' the *note if: de7. statement.)
When used with a loop, the ‘else’ clause has more in common with the
‘else’ clause of a *note try: d72. statement than it does with that of
*note if: de7. statements: a *note try: d72. statement’s ‘else’ clause
runs when no exception occurs, and a loop’s ‘else’ clause runs when no
‘break’ occurs. For more on the ‘try’ statement and exceptions, see
*note Handling Exceptions: ed1.
The *note continue: 181. statement, also borrowed from C, continues with
the next iteration of the loop:
>>> for num in range(2, 10):
... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found an odd number", num)
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9
File: python.info, Node: pass Statements, Next: Defining Functions, Prev: break and continue Statements and else Clauses on Loops, Up: More Control Flow Tools
2.4.5 ‘pass’ Statements
-----------------------
The *note pass: ed4. statement does nothing. It can be used when a
statement is required syntactically but the program requires no action.
For example:
>>> while True:
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
This is commonly used for creating minimal classes:
>>> class MyEmptyClass:
... pass
...
Another place *note pass: ed4. can be used is as a place-holder for a
function or conditional body when you are working on new code, allowing
you to keep thinking at a more abstract level. The ‘pass’ is silently
ignored:
>>> def initlog(*args):
... pass # Remember to implement this!
...
File: python.info, Node: Defining Functions, Next: More on Defining Functions, Prev: pass Statements, Up: More Control Flow Tools
2.4.6 Defining Functions
------------------------
We can create a function that writes the Fibonacci series to an
arbitrary boundary:
>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n."""
... a, b = 0, 1
... while a < n:
... print(a, end=' ')
... a, b = b, a+b
... print()
...
>>> # Now call the function we just defined:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
The keyword *note def: dbe. introduces a function `definition'. It must
be followed by the function name and the parenthesized list of formal
parameters. The statements that form the body of the function start at
the next line, and must be indented.
The first statement of the function body can optionally be a string
literal; this string literal is the function’s documentation string, or
`docstring'. (More about docstrings can be found in the section *note
Documentation Strings: ed7.) There are tools which use docstrings to
automatically produce online or printed documentation, or to let the
user interactively browse through code; it’s good practice to include
docstrings in code that you write, so make a habit of it.
The `execution' of a function introduces a new symbol table used for the
local variables of the function. More precisely, all variable
assignments in a function store the value in the local symbol table;
whereas variable references first look in the local symbol table, then
in the local symbol tables of enclosing functions, then in the global
symbol table, and finally in the table of built-in names. Thus, global
variables and variables of enclosing functions cannot be directly
assigned a value within a function (unless, for global variables, named
in a *note global: ed8. statement, or, for variables of enclosing
functions, named in a *note nonlocal: c3f. statement), although they may
be referenced.
The actual parameters (arguments) to a function call are introduced in
the local symbol table of the called function when it is called; thus,
arguments are passed using `call by value' (where the `value' is always
an object `reference', not the value of the object). (1) When a
function calls another function, or calls itself recursively, a new
local symbol table is created for that call.
A function definition associates the function name with the function
object in the current symbol table. The interpreter recognizes the
object pointed to by that name as a user-defined function. Other names
can also point to that same function object and can also be used to
access the function:
>>> fib
>>> f = fib
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89
Coming from other languages, you might object that ‘fib’ is not a
function but a procedure since it doesn’t return a value. In fact, even
functions without a *note return: 190. statement do return a value,
albeit a rather boring one. This value is called ‘None’ (it’s a
built-in name). Writing the value ‘None’ is normally suppressed by the
interpreter if it would be the only value written. You can see it if
you really want to using *note print(): 886.:
>>> fib(0)
>>> print(fib(0))
None
It is simple to write a function that returns a list of the numbers of
the Fibonacci series, instead of printing it:
>>> def fib2(n): # return Fibonacci series up to n
... """Return a list containing the Fibonacci series up to n."""
... result = []
... a, b = 0, 1
... while a < n:
... result.append(a) # see below
... a, b = b, a+b
... return result
...
>>> f100 = fib2(100) # call it
>>> f100 # write the result
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
This example, as usual, demonstrates some new Python features:
* The *note return: 190. statement returns with a value from a
function. ‘return’ without an expression argument returns ‘None’.
Falling off the end of a function also returns ‘None’.
* The statement ‘result.append(a)’ calls a `method' of the list
object ‘result’. A method is a function that ‘belongs’ to an
object and is named ‘obj.methodname’, where ‘obj’ is some object
(this may be an expression), and ‘methodname’ is the name of a
method that is defined by the object’s type. Different types
define different methods. Methods of different types may have the
same name without causing ambiguity. (It is possible to define
your own object types and methods, using `classes', see *note
Classes: ed9.) The method ‘append()’ shown in the example is
defined for list objects; it adds a new element at the end of the
list. In this example it is equivalent to ‘result = result + [a]’,
but more efficient.
---------- Footnotes ----------
(1) (1) Actually, `call by object reference' would be a better
description, since if a mutable object is passed, the caller will see
any changes the callee makes to it (items inserted into a list).
File: python.info, Node: More on Defining Functions, Next: Intermezzo Coding Style, Prev: Defining Functions, Up: More Control Flow Tools
2.4.7 More on Defining Functions
--------------------------------
It is also possible to define functions with a variable number of
arguments. There are three forms, which can be combined.
* Menu:
* Default Argument Values::
* Keyword Arguments::
* Special parameters::
* Arbitrary Argument Lists::
* Unpacking Argument Lists::
* Lambda Expressions::
* Documentation Strings::
* Function Annotations::
File: python.info, Node: Default Argument Values, Next: Keyword Arguments, Up: More on Defining Functions
2.4.7.1 Default Argument Values
...............................
The most useful form is to specify a default value for one or more
arguments. This creates a function that can be called with fewer
arguments than it is defined to allow. For example:
def ask_ok(prompt, retries=4, reminder='Please try again!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise ValueError('invalid user response')
print(reminder)
This function can be called in several ways:
* giving only the mandatory argument: ‘ask_ok('Do you really want to
quit?')’
* giving one of the optional arguments: ‘ask_ok('OK to overwrite the
file?', 2)’
* or even giving all arguments: ‘ask_ok('OK to overwrite the file?',
2, 'Come on, only yes or no!')’
This example also introduces the *note in: 7a3. keyword. This tests
whether or not a sequence contains a certain value.
The default values are evaluated at the point of function definition in
the `defining' scope, so that
i = 5
def f(arg=i):
print(arg)
i = 6
f()
will print ‘5’.
`Important warning:' The default value is evaluated only once. This
makes a difference when the default is a mutable object such as a list,
dictionary, or instances of most classes. For example, the following
function accumulates the arguments passed to it on subsequent calls:
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
This will print
[1]
[1, 2]
[1, 2, 3]
If you don’t want the default to be shared between subsequent calls, you
can write the function like this instead:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
File: python.info, Node: Keyword Arguments, Next: Special parameters, Prev: Default Argument Values, Up: More on Defining Functions
2.4.7.2 Keyword Arguments
.........................
Functions can also be called using *note keyword arguments: 5c4. of the
form ‘kwarg=value’. For instance, the following function:
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.")
print("-- Lovely plumage, the", type)
print("-- It's", state, "!")
accepts one required argument (‘voltage’) and three optional arguments
(‘state’, ‘action’, and ‘type’). This function can be called in any of
the following ways:
parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
but all the following calls would be invalid:
parrot() # required argument missing
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument
parrot(110, voltage=220) # duplicate value for the same argument
parrot(actor='John Cleese') # unknown keyword argument
In a function call, keyword arguments must follow positional arguments.
All the keyword arguments passed must match one of the arguments
accepted by the function (e.g. ‘actor’ is not a valid argument for the
‘parrot’ function), and their order is not important. This also
includes non-optional arguments (e.g. ‘parrot(voltage=1000)’ is valid
too). No argument may receive a value more than once. Here’s an
example that fails due to this restriction:
>>> def function(a):
... pass
...
>>> function(0, a=0)
Traceback (most recent call last):
File "", line 1, in
TypeError: function() got multiple values for keyword argument 'a'
When a final formal parameter of the form ‘**name’ is present, it
receives a dictionary (see *note Mapping Types — dict: 2fc.) containing
all keyword arguments except for those corresponding to a formal
parameter. This may be combined with a formal parameter of the form
‘*name’ (described in the next subsection) which receives a *note tuple:
ee0. containing the positional arguments beyond the formal parameter
list. (‘*name’ must occur before ‘**name’.) For example, if we define
a function like this:
def cheeseshop(kind, *arguments, **keywords):
print("-- Do you have any", kind, "?")
print("-- I'm sorry, we're all out of", kind)
for arg in arguments:
print(arg)
print("-" * 40)
for kw in keywords:
print(kw, ":", keywords[kw])
It could be called like this:
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")
and of course it would print:
-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch
Note that the order in which the keyword arguments are printed is
guaranteed to match the order in which they were provided in the
function call.
File: python.info, Node: Special parameters, Next: Arbitrary Argument Lists, Prev: Keyword Arguments, Up: More on Defining Functions
2.4.7.3 Special parameters
..........................
By default, arguments may be passed to a Python function either by
position or explicitly by keyword. For readability and performance, it
makes sense to restrict the way arguments can be passed so that a
developer need only look at the function definition to determine if
items are passed by position, by position or keyword, or by keyword.
A function definition may look like:
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
----------- ---------- ----------
| | |
| Positional or keyword |
| - Keyword only
-- Positional only
where ‘/’ and ‘*’ are optional. If used, these symbols indicate the
kind of parameter by how the arguments may be passed to the function:
positional-only, positional-or-keyword, and keyword-only. Keyword
parameters are also referred to as named parameters.
* Menu:
* Positional-or-Keyword Arguments::
* Positional-Only Parameters::
* Keyword-Only Arguments::
* Function Examples::
* Recap::
File: python.info, Node: Positional-or-Keyword Arguments, Next: Positional-Only Parameters, Up: Special parameters
2.4.7.4 Positional-or-Keyword Arguments
.......................................
If ‘/’ and ‘*’ are not present in the function definition, arguments may
be passed to a function by position or by keyword.
File: python.info, Node: Positional-Only Parameters, Next: Keyword-Only Arguments, Prev: Positional-or-Keyword Arguments, Up: Special parameters
2.4.7.5 Positional-Only Parameters
..................................
Looking at this in a bit more detail, it is possible to mark certain
parameters as `positional-only'. If `positional-only', the parameters’
order matters, and the parameters cannot be passed by keyword.
Positional-only parameters are placed before a ‘/’ (forward-slash). The
‘/’ is used to logically separate the positional-only parameters from
the rest of the parameters. If there is no ‘/’ in the function
definition, there are no positional-only parameters.
Parameters following the ‘/’ may be `positional-or-keyword' or
`keyword-only'.
File: python.info, Node: Keyword-Only Arguments, Next: Function Examples, Prev: Positional-Only Parameters, Up: Special parameters
2.4.7.6 Keyword-Only Arguments
..............................
To mark parameters as `keyword-only', indicating the parameters must be
passed by keyword argument, place an ‘*’ in the arguments list just
before the first `keyword-only' parameter.
File: python.info, Node: Function Examples, Next: Recap, Prev: Keyword-Only Arguments, Up: Special parameters
2.4.7.7 Function Examples
.........................
Consider the following example function definitions paying close
attention to the markers ‘/’ and ‘*’:
>>> def standard_arg(arg):
... print(arg)
...
>>> def pos_only_arg(arg, /):
... print(arg)
...
>>> def kwd_only_arg(*, arg):
... print(arg)
...
>>> def combined_example(pos_only, /, standard, *, kwd_only):
... print(pos_only, standard, kwd_only)
The first function definition, ‘standard_arg’, the most familiar form,
places no restrictions on the calling convention and arguments may be
passed by position or keyword:
>>> standard_arg(2)
2
>>> standard_arg(arg=2)
2
The second function ‘pos_only_arg’ is restricted to only use positional
parameters as there is a ‘/’ in the function definition:
>>> pos_only_arg(1)
1
>>> pos_only_arg(arg=1)
Traceback (most recent call last):
File "", line 1, in
TypeError: pos_only_arg() got an unexpected keyword argument 'arg'
The third function ‘kwd_only_args’ only allows keyword arguments as
indicated by a ‘*’ in the function definition:
>>> kwd_only_arg(3)
Traceback (most recent call last):
File "", line 1, in
TypeError: kwd_only_arg() takes 0 positional arguments but 1 was given
>>> kwd_only_arg(arg=3)
3
And the last uses all three calling conventions in the same function
definition:
>>> combined_example(1, 2, 3)
Traceback (most recent call last):
File "", line 1, in
TypeError: combined_example() takes 2 positional arguments but 3 were given
>>> combined_example(1, 2, kwd_only=3)
1 2 3
>>> combined_example(1, standard=2, kwd_only=3)
1 2 3
>>> combined_example(pos_only=1, standard=2, kwd_only=3)
Traceback (most recent call last):
File "", line 1, in
TypeError: combined_example() got an unexpected keyword argument 'pos_only'
Finally, consider this function definition which has a potential
collision between the positional argument ‘name’ and ‘**kwds’ which has
‘name’ as a key:
def foo(name, **kwds):
return 'name' in kwds
There is no possible call that will make it return ‘True’ as the keyword
‘'name'’ will always bind to the first parameter. For example:
>>> foo(1, **{'name': 2})
Traceback (most recent call last):
File "", line 1, in
TypeError: foo() got multiple values for argument 'name'
>>>
But using ‘/’ (positional only arguments), it is possible since it
allows ‘name’ as a positional argument and ‘'name'’ as a key in the
keyword arguments:
def foo(name, /, **kwds):
return 'name' in kwds
>>> foo(1, **{'name': 2})
True
In other words, the names of positional-only parameters can be used in
‘**kwds’ without ambiguity.
File: python.info, Node: Recap, Prev: Function Examples, Up: Special parameters
2.4.7.8 Recap
.............
The use case will determine which parameters to use in the function
definition:
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
As guidance:
* Use positional-only if you want the name of the parameters to not
be available to the user. This is useful when parameter names have
no real meaning, if you want to enforce the order of the arguments
when the function is called or if you need to take some positional
parameters and arbitrary keywords.
* Use keyword-only when names have meaning and the function
definition is more understandable by being explicit with names or
you want to prevent users relying on the position of the argument
being passed.
* For an API, use positional-only to prevent breaking API changes if
the parameter’s name is modified in the future.
File: python.info, Node: Arbitrary Argument Lists, Next: Unpacking Argument Lists, Prev: Special parameters, Up: More on Defining Functions
2.4.7.9 Arbitrary Argument Lists
................................
Finally, the least frequently used option is to specify that a function
can be called with an arbitrary number of arguments. These arguments
will be wrapped up in a tuple (see *note Tuples and Sequences: ee0.).
Before the variable number of arguments, zero or more normal arguments
may occur.
def write_multiple_items(file, separator, *args):
file.write(separator.join(args))
Normally, these ‘variadic’ arguments will be last in the list of formal
parameters, because they scoop up all remaining input arguments that are
passed to the function. Any formal parameters which occur after the
‘*args’ parameter are ‘keyword-only’ arguments, meaning that they can
only be used as keywords rather than positional arguments.
>>> def concat(*args, sep="/"):
... return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
File: python.info, Node: Unpacking Argument Lists, Next: Lambda Expressions, Prev: Arbitrary Argument Lists, Up: More on Defining Functions
2.4.7.10 Unpacking Argument Lists
.................................
The reverse situation occurs when the arguments are already in a list or
tuple but need to be unpacked for a function call requiring separate
positional arguments. For instance, the built-in *note range(): 9be.
function expects separate `start' and `stop' arguments. If they are not
available separately, write the function call with the ‘*’-operator to
unpack the arguments out of a list or tuple:
>>> list(range(3, 6)) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
In the same fashion, dictionaries can deliver keyword arguments with the
‘**’-operator:
>>> def parrot(voltage, state='a stiff', action='voom'):
... print("-- This parrot wouldn't", action, end=' ')
... print("if you put", voltage, "volts through it.", end=' ')
... print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
File: python.info, Node: Lambda Expressions, Next: Documentation Strings, Prev: Unpacking Argument Lists, Up: More on Defining Functions
2.4.7.11 Lambda Expressions
...........................
Small anonymous functions can be created with the *note lambda: c2f.
keyword. This function returns the sum of its two arguments: ‘lambda a,
b: a+b’. Lambda functions can be used wherever function objects are
required. They are syntactically restricted to a single expression.
Semantically, they are just syntactic sugar for a normal function
definition. Like nested function definitions, lambda functions can
reference variables from the containing scope:
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
The above example uses a lambda expression to return a function.
Another use is to pass a small function as an argument:
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
File: python.info, Node: Documentation Strings, Next: Function Annotations, Prev: Lambda Expressions, Up: More on Defining Functions
2.4.7.12 Documentation Strings
..............................
Here are some conventions about the content and formatting of
documentation strings.
The first line should always be a short, concise summary of the object’s
purpose. For brevity, it should not explicitly state the object’s name
or type, since these are available by other means (except if the name
happens to be a verb describing a function’s operation). This line
should begin with a capital letter and end with a period.
If there are more lines in the documentation string, the second line
should be blank, visually separating the summary from the rest of the
description. The following lines should be one or more paragraphs
describing the object’s calling conventions, its side effects, etc.
The Python parser does not strip indentation from multi-line string
literals in Python, so tools that process documentation have to strip
indentation if desired. This is done using the following convention.
The first non-blank line `after' the first line of the string determines
the amount of indentation for the entire documentation string. (We
can’t use the first line since it is generally adjacent to the string’s
opening quotes so its indentation is not apparent in the string
literal.) Whitespace “equivalent” to this indentation is then stripped
from the start of all lines of the string. Lines that are indented less
should not occur, but if they occur all their leading whitespace should
be stripped. Equivalence of whitespace should be tested after expansion
of tabs (to 8 spaces, normally).
Here is an example of a multi-line docstring:
>>> def my_function():
... """Do nothing, but document it.
...
... No, really, it doesn't do anything.
... """
... pass
...
>>> print(my_function.__doc__)
Do nothing, but document it.
No, really, it doesn't do anything.
File: python.info, Node: Function Annotations, Prev: Documentation Strings, Up: More on Defining Functions
2.4.7.13 Function Annotations
.............................
*note Function annotations: ef0. are completely optional metadata
information about the types used by user-defined functions (see PEP
3107(1) and PEP 484(2) for more information).
*note Annotations: ef1. are stored in the ‘__annotations__’ attribute of
the function as a dictionary and have no effect on any other part of the
function. Parameter annotations are defined by a colon after the
parameter name, followed by an expression evaluating to the value of the
annotation. Return annotations are defined by a literal ‘->’, followed
by an expression, between the parameter list and the colon denoting the
end of the *note def: dbe. statement. The following example has a
required argument, an optional argument, and the return value annotated:
>>> def f(ham: str, eggs: str = 'eggs') -> str:
... print("Annotations:", f.__annotations__)
... print("Arguments:", ham, eggs)
... return ham + ' and ' + eggs
...
>>> f('spam')
Annotations: {'ham': , 'return': , 'eggs': }
Arguments: spam eggs
'spam and eggs'
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3107
(2) https://www.python.org/dev/peps/pep-0484
File: python.info, Node: Intermezzo Coding Style, Prev: More on Defining Functions, Up: More Control Flow Tools
2.4.8 Intermezzo: Coding Style
------------------------------
Now that you are about to write longer, more complex pieces of Python,
it is a good time to talk about `coding style'. Most languages can be
written (or more concise, `formatted') in different styles; some are
more readable than others. Making it easy for others to read your code
is always a good idea, and adopting a nice coding style helps
tremendously for that.
For Python, PEP 8(1) has emerged as the style guide that most projects
adhere to; it promotes a very readable and eye-pleasing coding style.
Every Python developer should read it at some point; here are the most
important points extracted for you:
* Use 4-space indentation, and no tabs.
4 spaces are a good compromise between small indentation (allows
greater nesting depth) and large indentation (easier to read).
Tabs introduce confusion, and are best left out.
* Wrap lines so that they don’t exceed 79 characters.
This helps users with small displays and makes it possible to have
several code files side-by-side on larger displays.
* Use blank lines to separate functions and classes, and larger
blocks of code inside functions.
* When possible, put comments on a line of their own.
* Use docstrings.
* Use spaces around operators and after commas, but not directly
inside bracketing constructs: ‘a = f(1, 2) + g(3, 4)’.
* Name your classes and functions consistently; the convention is to
use ‘UpperCamelCase’ for classes and ‘lowercase_with_underscores’
for functions and methods. Always use ‘self’ as the name for the
first method argument (see *note A First Look at Classes: ef4. for
more on classes and methods).
* Don’t use fancy encodings if your code is meant to be used in
international environments. Python’s default, UTF-8, or even plain
ASCII work best in any case.
* Likewise, don’t use non-ASCII characters in identifiers if there is
only the slightest chance people speaking a different language will
read or maintain the code.
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-0008
File: python.info, Node: Data Structures, Next: Modules, Prev: More Control Flow Tools, Up: The Python Tutorial
2.5 Data Structures
===================
This chapter describes some things you’ve learned about already in more
detail, and adds some new things as well.
* Menu:
* More on Lists::
* The del statement::
* Tuples and Sequences::
* Sets::
* Dictionaries::
* Looping Techniques::
* More on Conditions::
* Comparing Sequences and Other Types::
File: python.info, Node: More on Lists, Next: The del statement, Up: Data Structures
2.5.1 More on Lists
-------------------
The list data type has some more methods. Here are all of the methods
of list objects:
-- Method: list.append (x)
Add an item to the end of the list. Equivalent to ‘a[len(a):] =
[x]’.
-- Method: list.extend (iterable)
Extend the list by appending all the items from the iterable.
Equivalent to ‘a[len(a):] = iterable’.
-- Method: list.insert (i, x)
Insert an item at a given position. The first argument is the
index of the element before which to insert, so ‘a.insert(0, x)’
inserts at the front of the list, and ‘a.insert(len(a), x)’ is
equivalent to ‘a.append(x)’.
-- Method: list.remove (x)
Remove the first item from the list whose value is equal to `x'.
It raises a *note ValueError: 1fb. if there is no such item.
-- Method: list.pop ([i])
Remove the item at the given position in the list, and return it.
If no index is specified, ‘a.pop()’ removes and returns the last
item in the list. (The square brackets around the `i' in the
method signature denote that the parameter is optional, not that
you should type square brackets at that position. You will see
this notation frequently in the Python Library Reference.)
-- Method: list.clear ()
Remove all items from the list. Equivalent to ‘del a[:]’.
-- Method: list.index (x[, start[, end]])
Return zero-based index in the list of the first item whose value
is equal to `x'. Raises a *note ValueError: 1fb. if there is no
such item.
The optional arguments `start' and `end' are interpreted as in the
slice notation and are used to limit the search to a particular
subsequence of the list. The returned index is computed relative
to the beginning of the full sequence rather than the `start'
argument.
-- Method: list.count (x)
Return the number of times `x' appears in the list.
-- Method: list.sort (*, key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for
sort customization, see *note sorted(): 444. for their
explanation).
-- Method: list.reverse ()
Reverse the elements of the list in place.
-- Method: list.copy ()
Return a shallow copy of the list. Equivalent to ‘a[:]’.
An example that uses most of the list methods:
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.count('tangerine')
0
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4) # Find next banana starting a position 4
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'
You might have noticed that methods like ‘insert’, ‘remove’ or ‘sort’
that only modify the list have no return value printed – they return the
default ‘None’. (1) This is a design principle for all mutable data
structures in Python.
Another thing you might notice is that not all data can be sorted or
compared. For instance, ‘[None, 'hello', 10]’ doesn’t sort because
integers can’t be compared to strings and `None' can’t be compared to
other types. Also, there are some types that don’t have a defined
ordering relation. For example, ‘3+4j < 5+7j’ isn’t a valid comparison.
* Menu:
* Using Lists as Stacks::
* Using Lists as Queues::
* List Comprehensions: List Comprehensions<2>.
* Nested List Comprehensions::
---------- Footnotes ----------
(1) (1) Other languages may return the mutated object, which allows
method chaining, such as ‘d->insert("a")->remove("b")->sort();’.
File: python.info, Node: Using Lists as Stacks, Next: Using Lists as Queues, Up: More on Lists
2.5.1.1 Using Lists as Stacks
.............................
The list methods make it very easy to use a list as a stack, where the
last element added is the first element retrieved (“last-in,
first-out”). To add an item to the top of the stack, use ‘append()’.
To retrieve an item from the top of the stack, use ‘pop()’ without an
explicit index. For example:
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
File: python.info, Node: Using Lists as Queues, Next: List Comprehensions<2>, Prev: Using Lists as Stacks, Up: More on Lists
2.5.1.2 Using Lists as Queues
.............................
It is also possible to use a list as a queue, where the first element
added is the first element retrieved (“first-in, first-out”); however,
lists are not efficient for this purpose. While appends and pops from
the end of list are fast, doing inserts or pops from the beginning of a
list is slow (because all of the other elements have to be shifted by
one).
To implement a queue, use *note collections.deque: 531. which was
designed to have fast appends and pops from both ends. For example:
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
File: python.info, Node: List Comprehensions<2>, Next: Nested List Comprehensions, Prev: Using Lists as Queues, Up: More on Lists
2.5.1.3 List Comprehensions
...........................
List comprehensions provide a concise way to create lists. Common
applications are to make new lists where each element is the result of
some operations applied to each member of another sequence or iterable,
or to create a subsequence of those elements that satisfy a certain
condition.
For example, assume we want to create a list of squares, like:
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Note that this creates (or overwrites) a variable named ‘x’ that still
exists after the loop completes. We can calculate the list of squares
without any side effects using:
squares = list(map(lambda x: x**2, range(10)))
or, equivalently:
squares = [x**2 for x in range(10)]
which is more concise and readable.
A list comprehension consists of brackets containing an expression
followed by a ‘for’ clause, then zero or more ‘for’ or ‘if’ clauses.
The result will be a new list resulting from evaluating the expression
in the context of the ‘for’ and ‘if’ clauses which follow it. For
example, this listcomp combines the elements of two lists if they are
not equal:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and it’s equivalent to:
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Note how the order of the *note for: c30. and *note if: de7. statements
is the same in both these snippets.
If the expression is a tuple (e.g. the ‘(x, y)’ in the previous
example), it must be parenthesized.
>>> vec = [-4, -2, 0, 2, 4]
>>> # create a new list with the values doubled
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> # filter the list to exclude negative numbers
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> # apply a function to all the elements
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> # call a method on each element
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> # create a list of 2-tuples like (number, square)
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> # the tuple must be parenthesized, otherwise an error is raised
>>> [x, x**2 for x in range(6)]
File "", line 1, in
[x, x**2 for x in range(6)]
^
SyntaxError: invalid syntax
>>> # flatten a list using a listcomp with two 'for'
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
List comprehensions can contain complex expressions and nested
functions:
>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
File: python.info, Node: Nested List Comprehensions, Prev: List Comprehensions<2>, Up: More on Lists
2.5.1.4 Nested List Comprehensions
..................................
The initial expression in a list comprehension can be any arbitrary
expression, including another list comprehension.
Consider the following example of a 3x4 matrix implemented as a list of
3 lists of length 4:
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
The following list comprehension will transpose rows and columns:
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
As we saw in the previous section, the nested listcomp is evaluated in
the context of the *note for: c30. that follows it, so this example is
equivalent to:
>>> transposed = []
>>> for i in range(4):
... transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
which, in turn, is the same as:
>>> transposed = []
>>> for i in range(4):
... # the following 3 lines implement the nested listcomp
... transposed_row = []
... for row in matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
In the real world, you should prefer built-in functions to complex flow
statements. The *note zip(): c32. function would do a great job for
this use case:
>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
See *note Unpacking Argument Lists: ee9. for details on the asterisk in
this line.
File: python.info, Node: The del statement, Next: Tuples and Sequences, Prev: More on Lists, Up: Data Structures
2.5.2 The ‘del’ statement
-------------------------
There is a way to remove an item from a list given its index instead of
its value: the *note del: f02. statement. This differs from the ‘pop()’
method which returns a value. The ‘del’ statement can also be used to
remove slices from a list or clear the entire list (which we did earlier
by assignment of an empty list to the slice). For example:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
*note del: f02. can also be used to delete entire variables:
>>> del a
Referencing the name ‘a’ hereafter is an error (at least until another
value is assigned to it). We’ll find other uses for *note del: f02.
later.
File: python.info, Node: Tuples and Sequences, Next: Sets, Prev: The del statement, Up: Data Structures
2.5.3 Tuples and Sequences
--------------------------
We saw that lists and strings have many common properties, such as
indexing and slicing operations. They are two examples of `sequence'
data types (see *note Sequence Types — list, tuple, range: f04.). Since
Python is an evolving language, other sequence data types may be added.
There is also another standard sequence data type: the `tuple'.
A tuple consists of a number of values separated by commas, for
instance:
>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # Tuples are immutable:
... t[0] = 88888
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
>>> # but they can contain mutable objects:
... v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])
As you see, on output tuples are always enclosed in parentheses, so that
nested tuples are interpreted correctly; they may be input with or
without surrounding parentheses, although often parentheses are
necessary anyway (if the tuple is part of a larger expression). It is
not possible to assign to the individual items of a tuple, however it is
possible to create tuples which contain mutable objects, such as lists.
Though tuples may seem similar to lists, they are often used in
different situations and for different purposes. Tuples are *note
immutable: eb6, and usually contain a heterogeneous sequence of elements
that are accessed via unpacking (see later in this section) or indexing
(or even by attribute in the case of *note namedtuples: 1b7.). Lists
are *note mutable: ebe, and their elements are usually homogeneous and
are accessed by iterating over the list.
A special problem is the construction of tuples containing 0 or 1 items:
the syntax has some extra quirks to accommodate these. Empty tuples are
constructed by an empty pair of parentheses; a tuple with one item is
constructed by following a value with a comma (it is not sufficient to
enclose a single value in parentheses). Ugly, but effective. For
example:
>>> empty = ()
>>> singleton = 'hello', # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
The statement ‘t = 12345, 54321, 'hello!'’ is an example of `tuple
packing': the values ‘12345’, ‘54321’ and ‘'hello!'’ are packed together
in a tuple. The reverse operation is also possible:
>>> x, y, z = t
This is called, appropriately enough, `sequence unpacking' and works for
any sequence on the right-hand side. Sequence unpacking requires that
there are as many variables on the left side of the equals sign as there
are elements in the sequence. Note that multiple assignment is really
just a combination of tuple packing and sequence unpacking.
File: python.info, Node: Sets, Next: Dictionaries, Prev: Tuples and Sequences, Up: Data Structures
2.5.4 Sets
----------
Python also includes a data type for `sets'. A set is an unordered
collection with no duplicate elements. Basic uses include membership
testing and eliminating duplicate entries. Set objects also support
mathematical operations like union, intersection, difference, and
symmetric difference.
Curly braces or the *note set(): b6f. function can be used to create
sets. Note: to create an empty set you have to use ‘set()’, not ‘{}’;
the latter creates an empty dictionary, a data structure that we discuss
in the next section.
Here is a brief demonstration:
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # fast membership testing
True
>>> 'crabgrass' in basket
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
Similarly to *note list comprehensions: efe, set comprehensions are also
supported:
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
File: python.info, Node: Dictionaries, Next: Looping Techniques, Prev: Sets, Up: Data Structures
2.5.5 Dictionaries
------------------
Another useful data type built into Python is the `dictionary' (see
*note Mapping Types — dict: 2fc.). Dictionaries are sometimes found in
other languages as “associative memories” or “associative arrays”.
Unlike sequences, which are indexed by a range of numbers, dictionaries
are indexed by `keys', which can be any immutable type; strings and
numbers can always be keys. Tuples can be used as keys if they contain
only strings, numbers, or tuples; if a tuple contains any mutable object
either directly or indirectly, it cannot be used as a key. You can’t
use lists as keys, since lists can be modified in place using index
assignments, slice assignments, or methods like ‘append()’ and
‘extend()’.
It is best to think of a dictionary as a set of `key: value' pairs, with
the requirement that the keys are unique (within one dictionary). A
pair of braces creates an empty dictionary: ‘{}’. Placing a
comma-separated list of key:value pairs within the braces adds initial
key:value pairs to the dictionary; this is also the way dictionaries are
written on output.
The main operations on a dictionary are storing a value with some key
and extracting the value given the key. It is also possible to delete a
key:value pair with ‘del’. If you store using a key that is already in
use, the old value associated with that key is forgotten. It is an
error to extract a value using a non-existent key.
Performing ‘list(d)’ on a dictionary returns a list of all the keys used
in the dictionary, in insertion order (if you want it sorted, just use
‘sorted(d)’ instead). To check whether a single key is in the
dictionary, use the *note in: 7a3. keyword.
Here is a small example using a dictionary:
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
The *note dict(): 1b8. constructor builds dictionaries directly from
sequences of key-value pairs:
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'guido': 4127, 'jack': 4098}
In addition, dict comprehensions can be used to create dictionaries from
arbitrary key and value expressions:
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
When the keys are simple strings, it is sometimes easier to specify
pairs using keyword arguments:
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'guido': 4127, 'jack': 4098}
File: python.info, Node: Looping Techniques, Next: More on Conditions, Prev: Dictionaries, Up: Data Structures
2.5.6 Looping Techniques
------------------------
When looping through dictionaries, the key and corresponding value can
be retrieved at the same time using the ‘items()’ method.
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
When looping through a sequence, the position index and corresponding
value can be retrieved at the same time using the *note enumerate():
de3. function.
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
To loop over two or more sequences at the same time, the entries can be
paired with the *note zip(): c32. function.
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
To loop over a sequence in reverse, first specify the sequence in a
forward direction and then call the *note reversed(): 18e. function.
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
To loop over a sequence in sorted order, use the *note sorted(): 444.
function which returns a new sorted list while leaving the source
unaltered.
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
It is sometimes tempting to change a list while you are looping over it;
however, it is often simpler and safer to create a new list instead.
>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
... if not math.isnan(value):
... filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]
File: python.info, Node: More on Conditions, Next: Comparing Sequences and Other Types, Prev: Looping Techniques, Up: Data Structures
2.5.7 More on Conditions
------------------------
The conditions used in ‘while’ and ‘if’ statements can contain any
operators, not just comparisons.
The comparison operators ‘in’ and ‘not in’ check whether a value occurs
(does not occur) in a sequence. The operators ‘is’ and ‘is not’ compare
whether two objects are really the same object; this only matters for
mutable objects like lists. All comparison operators have the same
priority, which is lower than that of all numerical operators.
Comparisons can be chained. For example, ‘a < b == c’ tests whether ‘a’
is less than ‘b’ and moreover ‘b’ equals ‘c’.
Comparisons may be combined using the Boolean operators ‘and’ and ‘or’,
and the outcome of a comparison (or of any other Boolean expression) may
be negated with ‘not’. These have lower priorities than comparison
operators; between them, ‘not’ has the highest priority and ‘or’ the
lowest, so that ‘A and not B or C’ is equivalent to ‘(A and (not B)) or
C’. As always, parentheses can be used to express the desired
composition.
The Boolean operators ‘and’ and ‘or’ are so-called `short-circuit'
operators: their arguments are evaluated from left to right, and
evaluation stops as soon as the outcome is determined. For example, if
‘A’ and ‘C’ are true but ‘B’ is false, ‘A and B and C’ does not evaluate
the expression ‘C’. When used as a general value and not as a Boolean,
the return value of a short-circuit operator is the last evaluated
argument.
It is possible to assign the result of a comparison or other Boolean
expression to a variable. For example,
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
Note that in Python, unlike C, assignment inside expressions must be
done explicitly with the *note walrus operator: f0c. ‘:=’. This avoids
a common class of problems encountered in C programs: typing ‘=’ in an
expression when ‘==’ was intended.
File: python.info, Node: Comparing Sequences and Other Types, Prev: More on Conditions, Up: Data Structures
2.5.8 Comparing Sequences and Other Types
-----------------------------------------
Sequence objects typically may be compared to other objects with the
same sequence type. The comparison uses `lexicographical' ordering:
first the first two items are compared, and if they differ this
determines the outcome of the comparison; if they are equal, the next
two items are compared, and so on, until either sequence is exhausted.
If two items to be compared are themselves sequences of the same type,
the lexicographical comparison is carried out recursively. If all items
of two sequences compare equal, the sequences are considered equal. If
one sequence is an initial sub-sequence of the other, the shorter
sequence is the smaller (lesser) one. Lexicographical ordering for
strings uses the Unicode code point number to order individual
characters. Some examples of comparisons between sequences of the same
type:
(1, 2, 3) < (1, 2, 4)
[1, 2, 3] < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4) < (1, 2, 4)
(1, 2) < (1, 2, -1)
(1, 2, 3) == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
Note that comparing objects of different types with ‘<’ or ‘>’ is legal
provided that the objects have appropriate comparison methods. For
example, mixed numeric types are compared according to their numeric
value, so 0 equals 0.0, etc. Otherwise, rather than providing an
arbitrary ordering, the interpreter will raise a *note TypeError: 192.
exception.
File: python.info, Node: Modules, Next: Input and Output, Prev: Data Structures, Up: The Python Tutorial
2.6 Modules
===========
If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost.
Therefore, if you want to write a somewhat longer program, you are
better off using a text editor to prepare the input for the interpreter
and running it with that file as input instead. This is known as
creating a `script'. As your program gets longer, you may want to split
it into several files for easier maintenance. You may also want to use
a handy function that you’ve written in several programs without copying
its definition into each program.
To support this, Python has a way to put definitions in a file and use
them in a script or in an interactive instance of the interpreter. Such
a file is called a `module'; definitions from a module can be `imported'
into other modules or into the `main' module (the collection of
variables that you have access to in a script executed at the top level
and in calculator mode).
A module is a file containing Python definitions and statements. The
file name is the module name with the suffix ‘.py’ appended. Within a
module, the module’s name (as a string) is available as the value of the
global variable ‘__name__’. For instance, use your favorite text editor
to create a file called ‘fibo.py’ in the current directory with the
following contents:
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result
Now enter the Python interpreter and import this module with the
following command:
>>> import fibo
This does not enter the names of the functions defined in ‘fibo’
directly in the current symbol table; it only enters the module name
‘fibo’ there. Using the module name you can access the functions:
>>> fibo.fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'
If you intend to use a function often you can assign it to a local name:
>>> fib = fibo.fib
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
* Menu:
* More on Modules::
* Standard Modules::
* The dir() Function: The dir Function.
* Packages::
File: python.info, Node: More on Modules, Next: Standard Modules, Up: Modules
2.6.1 More on Modules
---------------------
A module can contain executable statements as well as function
definitions. These statements are intended to initialize the module.
They are executed only the `first' time the module name is encountered
in an import statement. (1) (They are also run if the file is executed
as a script.)
Each module has its own private symbol table, which is used as the
global symbol table by all functions defined in the module. Thus, the
author of a module can use global variables in the module without
worrying about accidental clashes with a user’s global variables. On
the other hand, if you know what you are doing you can touch a module’s
global variables with the same notation used to refer to its functions,
‘modname.itemname’.
Modules can import other modules. It is customary but not required to
place all *note import: c1d. statements at the beginning of a module (or
script, for that matter). The imported module names are placed in the
importing module’s global symbol table.
There is a variant of the *note import: c1d. statement that imports
names from a module directly into the importing module’s symbol table.
For example:
>>> from fibo import fib, fib2
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This does not introduce the module name from which the imports are taken
in the local symbol table (so in the example, ‘fibo’ is not defined).
There is even a variant to import all names that a module defines:
>>> from fibo import *
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This imports all names except those beginning with an underscore (‘_’).
In most cases Python programmers do not use this facility since it
introduces an unknown set of names into the interpreter, possibly hiding
some things you have already defined.
Note that in general the practice of importing ‘*’ from a module or
package is frowned upon, since it often causes poorly readable code.
However, it is okay to use it to save typing in interactive sessions.
If the module name is followed by ‘as’, then the name following ‘as’ is
bound directly to the imported module.
>>> import fibo as fib
>>> fib.fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This is effectively importing the module in the same way that ‘import
fibo’ will do, with the only difference of it being available as ‘fib’.
It can also be used when utilising *note from: c45. with similar
effects:
>>> from fibo import fib as fibonacci
>>> fibonacci(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Note: For efficiency reasons, each module is only imported once per
interpreter session. Therefore, if you change your modules, you
must restart the interpreter – or, if it’s just one module you want
to test interactively, use *note importlib.reload(): 399, e.g.
‘import importlib; importlib.reload(modulename)’.
* Menu:
* Executing modules as scripts::
* The Module Search Path::
* “Compiled” Python files::
---------- Footnotes ----------
(1) (1) In fact function definitions are also ‘statements’ that are
‘executed’; the execution of a module-level function definition enters
the function name in the module’s global symbol table.
File: python.info, Node: Executing modules as scripts, Next: The Module Search Path, Up: More on Modules
2.6.1.1 Executing modules as scripts
....................................
When you run a Python module with
python fibo.py
the code in the module will be executed, just as if you imported it, but
with the ‘__name__’ set to ‘"__main__"’. That means that by adding this
code at the end of your module:
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
you can make the file usable as a script as well as an importable
module, because the code that parses the command line only runs if the
module is executed as the “main” file:
$ python fibo.py 50
0 1 1 2 3 5 8 13 21 34
If the module is imported, the code is not run:
>>> import fibo
>>>
This is often used either to provide a convenient user interface to a
module, or for testing purposes (running the module as a script executes
a test suite).
File: python.info, Node: The Module Search Path, Next: “Compiled” Python files, Prev: Executing modules as scripts, Up: More on Modules
2.6.1.2 The Module Search Path
..............................
When a module named ‘spam’ is imported, the interpreter first searches
for a built-in module with that name. If not found, it then searches
for a file named ‘spam.py’ in a list of directories given by the
variable *note sys.path: 488. *note sys.path: 488. is initialized from
these locations:
* The directory containing the input script (or the current directory
when no file is specified).
* *note PYTHONPATH: 953. (a list of directory names, with the same
syntax as the shell variable ‘PATH’).
* The installation-dependent default.
Note: On file systems which support symlinks, the directory
containing the input script is calculated after the symlink is
followed. In other words the directory containing the symlink is
`not' added to the module search path.
After initialization, Python programs can modify *note sys.path: 488.
The directory containing the script being run is placed at the beginning
of the search path, ahead of the standard library path. This means that
scripts in that directory will be loaded instead of modules of the same
name in the library directory. This is an error unless the replacement
is intended. See section *note Standard Modules: f18. for more
information.
File: python.info, Node: “Compiled” Python files, Prev: The Module Search Path, Up: More on Modules
2.6.1.3 “Compiled” Python files
...............................
To speed up loading modules, Python caches the compiled version of each
module in the ‘__pycache__’ directory under the name
‘module.`version'.pyc’, where the version encodes the format of the
compiled file; it generally contains the Python version number. For
example, in CPython release 3.3 the compiled version of spam.py would be
cached as ‘__pycache__/spam.cpython-33.pyc’. This naming convention
allows compiled modules from different releases and different versions
of Python to coexist.
Python checks the modification date of the source against the compiled
version to see if it’s out of date and needs to be recompiled. This is
a completely automatic process. Also, the compiled modules are
platform-independent, so the same library can be shared among systems
with different architectures.
Python does not check the cache in two circumstances. First, it always
recompiles and does not store the result for the module that’s loaded
directly from the command line. Second, it does not check the cache if
there is no source module. To support a non-source (compiled only)
distribution, the compiled module must be in the source directory, and
there must not be a source module.
Some tips for experts:
* You can use the *note -O: 68b. or *note -OO: 68c. switches on the
Python command to reduce the size of a compiled module. The ‘-O’
switch removes assert statements, the ‘-OO’ switch removes both
assert statements and __doc__ strings. Since some programs may
rely on having these available, you should only use this option if
you know what you’re doing. “Optimized” modules have an ‘opt-’ tag
and are usually smaller. Future releases may change the effects of
optimization.
* A program doesn’t run any faster when it is read from a ‘.pyc’ file
than when it is read from a ‘.py’ file; the only thing that’s
faster about ‘.pyc’ files is the speed with which they are loaded.
* The module *note compileall: 21. can create .pyc files for all
modules in a directory.
* There is more detail on this process, including a flow chart of the
decisions, in PEP 3147(1).
---------- Footnotes ----------
(1) https://www.python.org/dev/peps/pep-3147
File: python.info, Node: Standard Modules, Next: The dir Function, Prev: More on Modules, Up: Modules
2.6.2 Standard Modules
----------------------
Python comes with a library of standard modules, described in a separate
document, the Python Library Reference (“Library Reference” hereafter).
Some modules are built into the interpreter; these provide access to
operations that are not part of the core of the language but are
nevertheless built in, either for efficiency or to provide access to
operating system primitives such as system calls. The set of such
modules is a configuration option which also depends on the underlying
platform. For example, the *note winreg: 12a. module is only provided
on Windows systems. One particular module deserves some attention:
*note sys: fd, which is built into every Python interpreter. The
variables ‘sys.ps1’ and ‘sys.ps2’ define the strings used as primary and
secondary prompts:
>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C> print('Yuck!')
Yuck!
C>
These two variables are only defined if the interpreter is in
interactive mode.
The variable ‘sys.path’ is a list of strings that determines the
interpreter’s search path for modules. It is initialized to a default
path taken from the environment variable *note PYTHONPATH: 953, or from
a built-in default if *note PYTHONPATH: 953. is not set. You can modify
it using standard list operations:
>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')
File: python.info, Node: The dir Function, Next: Packages, Prev: Standard Modules, Up: Modules
2.6.3 The ‘dir()’ Function
--------------------------
The built-in function *note dir(): d33. is used to find out which names
a module defines. It returns a sorted list of strings:
>>> import fibo, sys
>>> dir(fibo)
['__name__', 'fib', 'fib2']
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
'__package__', '__stderr__', '__stdin__', '__stdout__',
'_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
'_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
'call_tracing', 'callstats', 'copyright', 'displayhook',
'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
'thread_info', 'version', 'version_info', 'warnoptions']
Without arguments, *note dir(): d33. lists the names you have defined
currently:
>>> a = [1, 2, 3, 4, 5]
>>> import fibo
>>> fib = fibo.fib
>>> dir()
['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']
Note that it lists all types of names: variables, modules, functions,
etc.
*note dir(): d33. does not list the names of built-in functions and
variables. If you want a list of those, they are defined in the
standard module *note builtins: 13.:
>>> import builtins
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',
'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',
'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',
'__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',
'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',
'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',
'zip']
File: python.info, Node: Packages, Prev: The dir Function, Up: Modules
2.6.4 Packages
--------------
Packages are a way of structuring Python’s module namespace by using
“dotted module names”. For example, the module name ‘A.B’ designates a
submodule named ‘B’ in a package named ‘A’. Just like the use of
modules saves the authors of different modules from having to worry
about each other’s global variable names, the use of dotted module names
saves the authors of multi-module packages like NumPy or Pillow from
having to worry about each other’s module names.
Suppose you want to design a collection of modules (a “package”) for the
uniform handling of sound files and sound data. There are many
different sound file formats (usually recognized by their extension, for
example: ‘.wav’, ‘.aiff’, ‘.au’), so you may need to create and maintain
a growing collection of modules for the conversion between the various
file formats. There are also many different operations you might want
to perform on sound data (such as mixing, adding echo, applying an
equalizer function, creating an artificial stereo effect), so in
addition you will be writing a never-ending stream of modules to perform
these operations. Here’s a possible structure for your package
(expressed in terms of a hierarchical filesystem):
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...
When importing the package, Python searches through the directories on
‘sys.path’ looking for the package subdirectory.
The ‘__init__.py’ files are required to make Python treat directories
containing the file as packages. This prevents directories with a
common name, such as ‘string’, unintentionally hiding valid modules that
occur later on the module search path. In the simplest case,
‘__init__.py’ can just be an empty file, but it can also execute
initialization code for the package or set the ‘__all__’ variable,
described later.
Users of the package can import individual modules from the package, for
example:
import sound.effects.echo
This loads the submodule ‘sound.effects.echo’. It must be referenced
with its full name.
sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
An alternative way of importing the submodule is:
from sound.effects import echo
This also loads the submodule ‘echo’, and makes it available without its
package prefix, so it can be used as follows:
echo.echofilter(input, output, delay=0.7, atten=4)
Yet another variation is to import the desired function or variable
directly:
from sound.effects.echo import echofilter
Again, this loads the submodule ‘echo’, but this makes its function
‘echofilter()’ directly available:
echofilter(input, output, delay=0.7, atten=4)
Note that when using ‘from package import item’, the item can be either
a submodule (or subpackage) of the package, or some other name defined
in the package, like a function, class or variable. The ‘import’
statement first tests whether the item is defined in the package; if
not, it assumes it is a module and attempts to load it. If it fails to
find it, an *note ImportError: 334. exception is raised.
Contrarily, when using syntax like ‘import item.subitem.subsubitem’,
each item except for the last must be a package; the last item can be a
module or a package but can’t be a class or function or variable defined
in the previous item.
* Menu:
* Importing * From a Package::
* Intra-package References::
* Packages in Multiple Directories::
File: python.info, Node: Importing * From a Package, Next: Intra-package References, Up: Packages
2.6.4.1 Importing * From a Package
..................................
Now what happens when the user writes ‘from sound.effects import *’?
Ideally, one would hope that this somehow goes out to the filesystem,
finds which submodules are present in the package, and imports them all.
This could take a long time and importing sub-modules might have
unwanted side-effects that should only happen when the sub-module is
explicitly imported.
The only solution is for the package author to provide an explicit index
of the package. The *note import: c1d. statement uses the following
convention: if a package’s ‘__init__.py’ code defines a list named
‘__all__’, it is taken to be the list of module names that should be
imported when ‘from package import *’ is encountered. It is up to the
package author to keep this list up-to-date when a new version of the
package is released. Package authors may also decide not to support it,
if they don’t see a use for importing * from their package. For
example, the file ‘sound/effects/__init__.py’ could contain the
following code:
__all__ = ["echo", "surround", "reverse"]
This would mean that ‘from sound.effects import *’ would import the
three named submodules of the ‘sound’ package.
If ‘__all__’ is not defined, the statement ‘from sound.effects import *’
does `not' import all submodules from the package ‘sound.effects’ into
the current namespace; it only ensures that the package ‘sound.effects’
has been imported (possibly running any initialization code in
‘__init__.py’) and then imports whatever names are defined in the
package. This includes any names defined (and submodules explicitly
loaded) by ‘__init__.py’. It also includes any submodules of the
package that were explicitly loaded by previous *note import: c1d.
statements. Consider this code:
import sound.effects.echo
import sound.effects.surround
from sound.effects import *
In this example, the ‘echo’ and ‘surround’ modules are imported in the
current namespace because they are defined in the ‘sound.effects’
package when the ‘from...import’ statement is executed. (This also
works when ‘__all__’ is defined.)
Although certain modules are designed to export only names that follow
certain patterns when you use ‘import *’, it is still considered bad
practice in production code.
Remember, there is nothing wrong with using ‘from package import
specific_submodule’! In fact, this is the recommended notation unless
the importing module needs to use submodules with the same name from
different packages.
File: python.info, Node: Intra-package References, Next: Packages in Multiple Directories, Prev: Importing * From a Package, Up: Packages
2.6.4.2 Intra-package References
................................
When packages are structured into subpackages (as with the ‘sound’
package in the example), you can use absolute imports to refer to
submodules of siblings packages. For example, if the module
‘sound.filters.vocoder’ needs to use the ‘echo’ module in the
‘sound.effects’ package, it can use ‘from sound.effects import echo’.
You can also write relative imports, with the ‘from module import name’
form of import statement. These imports use leading dots to indicate
the current and parent packages involved in the relative import. From
the ‘surround’ module for example, you might use:
from . import echo
from .. import formats
from ..filters import equalizer
Note that relative imports are based on the name of the current module.
Since the name of the main module is always ‘"__main__"’, modules
intended for use as the main module of a Python application must always
use absolute imports.
File: python.info, Node: Packages in Multiple Directories, Prev: Intra-package References, Up: Packages
2.6.4.3 Packages in Multiple Directories
........................................
Packages support one more special attribute, *note __path__: f23. This
is initialized to be a list containing the name of the directory holding
the package’s ‘__init__.py’ before the code in that file is executed.
This variable can be modified; doing so affects future searches for
modules and subpackages contained in the package.
While this feature is not often needed, it can be used to extend the set
of modules found in a package.
File: python.info, Node: Input and Output, Next: Errors and Exceptions, Prev: Modules, Up: The Python Tutorial
2.7 Input and Output
====================
There are several ways to present the output of a program; data can be
printed in a human-readable form, or written to a file for future use.
This chapter will discuss some of the possibilities.
* Menu:
* Fancier Output Formatting::
* Reading and Writing Files::
File: python.info, Node: Fancier Output Formatting, Next: Reading and Writing Files, Up: Input and Output
2.7.1 Fancier Output Formatting
-------------------------------
So far we’ve encountered two ways of writing values: `expression
statements' and the *note print(): 886. function. (A third way is using
the ‘write()’ method of file objects; the standard output file can be
referenced as ‘sys.stdout’. See the Library Reference for more
information on this.)
Often you’ll want more control over the formatting of your output than
simply printing space-separated values. There are several ways to
format output.
* To use *note formatted string literals: f29, begin a string with
‘f’ or ‘F’ before the opening quotation mark or triple quotation
mark. Inside this string, you can write a Python expression
between ‘{’ and ‘}’ characters that can refer to variables or
literal values.
>>> year = 2016
>>> event = 'Referendum'
>>> f'Results of the {year} {event}'
'Results of the 2016 Referendum'
* The *note str.format(): 4da. method of strings requires more manual
effort. You’ll still use ‘{’ and ‘}’ to mark where a variable will
be substituted and can provide detailed formatting directives, but
you’ll also need to provide the information to be formatted.
>>> yes_votes = 42_572_654
>>> no_votes = 43_132_495
>>> percentage = yes_votes / (yes_votes + no_votes)
>>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
' 42572654 YES votes 49.67%'
* Finally, you can do all the string handling yourself by using
string slicing and concatenation operations to create any layout
you can imagine. The string type has some methods that perform
useful operations for padding strings to a given column width.
When you don’t need fancy output but just want a quick display of some
variables for debugging purposes, you can convert any value to a string
with the *note repr(): 7cc. or *note str(): 330. functions.
The *note str(): 330. function is meant to return representations of
values which are fairly human-readable, while *note repr(): 7cc. is
meant to generate representations which can be read by the interpreter
(or will force a *note SyntaxError: 458. if there is no equivalent
syntax). For objects which don’t have a particular representation for
human consumption, *note str(): 330. will return the same value as *note
repr(): 7cc. Many values, such as numbers or structures like lists and
dictionaries, have the same representation using either function.
Strings, in particular, have two distinct representations.
Some examples:
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
The *note string: f6. module contains a *note Template: 3eb. class that
offers yet another way to substitute values into strings, using
placeholders like ‘$x’ and replacing them with values from a dictionary,
but offers much less control of the formatting.
* Menu:
* Formatted String Literals::
* The String format() Method: The String format Method.
* Manual String Formatting::
* Old string formatting::
File: python.info, Node: Formatted String Literals, Next: The String format Method, Up: Fancier Output Formatting
2.7.1.1 Formatted String Literals
.................................
*note Formatted string literals: 15c. (also called f-strings for short)
let you include the value of Python expressions inside a string by
prefixing the string with ‘f’ or ‘F’ and writing expressions as
‘{expression}’.
An optional format specifier can follow the expression. This allows
greater control over how the value is formatted. The following example
rounds pi to three places after the decimal:
>>> import math
>>> print(f'The value of pi is approximately {math.pi:.3f}.')
The value of pi is approximately 3.142.
Passing an integer after the ‘':'’ will cause that field to be a minimum
number of characters wide. This is useful for making columns line up.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print(f'{name:10} ==> {phone:10d}')
...
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
Other modifiers can be used to convert the value before it is formatted.
‘'!a'’ applies *note ascii(): d4c, ‘'!s'’ applies *note str(): 330, and
‘'!r'’ applies *note repr(): 7cc.:
>>> animals = 'eels'
>>> print(f'My hovercraft is full of {animals}.')
My hovercraft is full of eels.
>>> print(f'My hovercraft is full of {animals!r}.')
My hovercraft is full of 'eels'.
For a reference on these format specifications, see the reference guide
for the *note Format Specification Mini-Language: 4de.
File: python.info, Node: The String format Method, Next: Manual String Formatting, Prev: Formatted String Literals, Up: Fancier Output Formatting
2.7.1.2 The String format() Method
..................................
Basic usage of the *note str.format(): 4da. method looks like this:
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
The brackets and characters within them (called format fields) are
replaced with the objects passed into the *note str.format(): 4da.
method. A number in the brackets can be used to refer to the position
of the object passed into the *note str.format(): 4da. method.
>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam
If keyword arguments are used in the *note str.format(): 4da. method,
their values are referred to by using the name of the argument.
>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.
Positional and keyword arguments can be arbitrarily combined:
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
other='Georg'))
The story of Bill, Manfred, and Georg.
If you have a really long format string that you don’t want to split up,
it would be nice if you could reference the variables to be formatted by
name instead of by position. This can be done by simply passing the
dict and using square brackets ‘'[]'’ to access the keys.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
... 'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
This could also be done by passing the table as keyword arguments with
the ‘**’ notation.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
This is particularly useful in combination with the built-in function
*note vars(): f2d, which returns a dictionary containing all local
variables.
As an example, the following lines produce a tidily-aligned set of
columns giving integers and their squares and cubes:
>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
For a complete overview of string formatting with *note str.format():
4da, see *note Format String Syntax: d1a.
File: python.info, Node: Manual String Formatting, Next: Old string formatting, Prev: The String format Method, Up: Fancier Output Formatting
2.7.1.3 Manual String Formatting
................................
Here’s the same table of squares and cubes, formatted manually:
>>> for x in range(1, 11):
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
... # Note use of 'end' on previous line
... print(repr(x*x*x).rjust(4))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
(Note that the one space between each column was added by the way *note
print(): 886. works: it always adds spaces between its arguments.)
The *note str.rjust(): f2f. method of string objects right-justifies a
string in a field of a given width by padding it with spaces on the
left. There are similar methods *note str.ljust(): f30. and *note
str.center(): f31. These methods do not write anything, they just
return a new string. If the input string is too long, they don’t
truncate it, but return it unchanged; this will mess up your column
lay-out but that’s usually better than the alternative, which would be
lying about a value. (If you really want truncation you can always add
a slice operation, as in ‘x.ljust(n)[:n]’.)
There is another method, *note str.zfill(): f32, which pads a numeric
string on the left with zeros. It understands about plus and minus
signs:
>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'
File: python.info, Node: Old string formatting, Prev: Manual String Formatting, Up: Fancier Output Formatting
2.7.1.4 Old string formatting
.............................
The % operator (modulo) can also be used for string formatting. Given
‘'string' % values’, instances of ‘%’ in ‘string’ are replaced with zero
or more elements of ‘values’. This operation is commonly known as
string interpolation. For example:
>>> import math
>>> print('The value of pi is approximately %5.3f.' % math.pi)
The value of pi is approximately 3.142.
More information can be found in the *note printf-style String
Formatting: eb9. section.
File: python.info, Node: Reading and Writing Files, Prev: Fancier Output Formatting, Up: Input and Output
2.7.2 Reading and Writing Files
-------------------------------
*note open(): 4f0. returns a *note file object: b42, and is most
commonly used with two arguments: ‘open(filename, mode)’.
>>> f = open('workfile', 'w')
The first argument is a string containing the filename. The second
argument is another string containing a few characters describing the
way in which the file will be used. `mode' can be ‘'r'’ when the file
will only be read, ‘'w'’ for only writing (an existing file with the
same name will be erased), and ‘'a'’ opens the file for appending; any
data written to the file is automatically added to the end. ‘'r+'’
opens the file for both reading and writing. The `mode' argument is
optional; ‘'r'’ will be assumed if it’s omitted.
Normally, files are opened in `text mode', that means, you read and
write strings from and to the file, which are encoded in a specific
encoding. If encoding is not specified, the default is platform
dependent (see *note open(): 4f0.). ‘'b'’ appended to the mode opens
the file in `binary mode': now the data is read and written in the form
of bytes objects. This mode should be used for all files that don’t
contain text.
In text mode, the default when reading is to convert platform-specific
line endings (‘\n’ on Unix, ‘\r\n’ on Windows) to just ‘\n’. When
writing in text mode, the default is to convert occurrences of ‘\n’ back
to platform-specific line endings. This behind-the-scenes modification
to file data is fine for text files, but will corrupt binary data like
that in ‘JPEG’ or ‘EXE’ files. Be very careful to use binary mode when
reading and writing such files.
It is good practice to use the *note with: 6e9. keyword when dealing
with file objects. The advantage is that the file is properly closed
after its suite finishes, even if an exception is raised at some point.
Using ‘with’ is also much shorter than writing equivalent *note try:
d72.-*note finally: 182. blocks:
>>> with open('workfile') as f:
... read_data = f.read()
>>> # We can check that the file has been automatically closed.
>>> f.closed
True
If you’re not using the *note with: 6e9. keyword, then you should call
‘f.close()’ to close the file and immediately free up any system
resources used by it.
Warning: Calling ‘f.write()’ without using the ‘with’ keyword or
calling ‘f.close()’ `might' result in the arguments of ‘f.write()’
not being completely written to the disk, even if the program exits
successfully.
After a file object is closed, either by a *note with: 6e9. statement or
by calling ‘f.close()’, attempts to use the file object will
automatically fail.
>>> f.close()
>>> f.read()
Traceback (most recent call last):
File "", line 1, in
ValueError: I/O operation on closed file.
* Menu:
* Methods of File Objects::
* Saving structured data with json::
File: python.info, Node: Methods of File Objects, Next: Saving structured data with json, Up: Reading and Writing Files
2.7.2.1 Methods of File Objects
...............................
The rest of the examples in this section will assume that a file object
called ‘f’ has already been created.
To read a file’s contents, call ‘f.read(size)’, which reads some
quantity of data and returns it as a string (in text mode) or bytes
object (in binary mode). `size' is an optional numeric argument. When
`size' is omitted or negative, the entire contents of the file will be
read and returned; it’s your problem if the file is twice as large as
your machine’s memory. Otherwise, at most `size' characters (in text
mode) or `size' bytes (in binary mode) are read and returned. If the
end of the file has been reached, ‘f.read()’ will return an empty string
(‘''’).
>>> f.read()
'This is the entire file.\n'
>>> f.read()
''
‘f.readline()’ reads a single line from the file; a newline character
(‘\n’) is left at the end of the string, and is only omitted on the last
line of the file if the file doesn’t end in a newline. This makes the
return value unambiguous; if ‘f.readline()’ returns an empty string, the
end of the file has been reached, while a blank line is represented by
‘'\n'’, a string containing only a single newline.
>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''
For reading lines from a file, you can loop over the file object. This
is memory efficient, fast, and leads to simple code:
>>> for line in f:
... print(line, end='')
...
This is the first line of the file.
Second line of the file
If you want to read all the lines of a file in a list you can also use
‘list(f)’ or ‘f.readlines()’.
‘f.write(string)’ writes the contents of `string' to the file, returning
the number of characters written.
>>> f.write('This is a test\n')
15
Other types of objects need to be converted – either to a string (in
text mode) or a bytes object (in binary mode) – before writing them:
>>> value = ('the answer', 42)
>>> s = str(value) # convert the tuple to string
>>> f.write(s)
18
‘f.tell()’ returns an integer giving the file object’s current position
in the file represented as number of bytes from the beginning of the
file when in binary mode and an opaque number when in text mode.
To change the file object’s position, use ‘f.seek(offset, whence)’. The
position is computed from adding `offset' to a reference point; the
reference point is selected by the `whence' argument. A `whence' value
of 0 measures from the beginning of the file, 1 uses the current file
position, and 2 uses the end of the file as the reference point.
`whence' can be omitted and defaults to 0, using the beginning of the
file as the reference point.
>>> f = open('workfile', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5) # Go to the 6th byte in the file
5
>>> f.read(1)
b'5'
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
13
>>> f.read(1)
b'd'
In text files (those opened without a ‘b’ in the mode string), only
seeks relative to the beginning of the file are allowed (the exception
being seeking to the very file end with ‘seek(0, 2)’) and the only valid
`offset' values are those returned from the ‘f.tell()’, or zero. Any
other `offset' value produces undefined behaviour.
File objects have some additional methods, such as ‘isatty()’ and
‘truncate()’ which are less frequently used; consult the Library
Reference for a complete guide to file objects.
File: python.info, Node: Saving structured data with json, Prev: Methods of File Objects, Up: Reading and Writing Files
2.7.2.2 Saving structured data with ‘json’
..........................................
Strings can easily be written to and read from a file. Numbers take a
bit more effort, since the ‘read()’ method only returns strings, which
will have to be passed to a function like *note int(): 184, which takes
a string like ‘'123'’ and returns its numeric value 123. When you want
to save more complex data types like nested lists and dictionaries,
parsing and serializing by hand becomes complicated.
Rather than having users constantly writing and debugging code to save
complicated data types to files, Python allows you to use the popular
data interchange format called JSON (JavaScript Object Notation)(1).
The standard module called *note json: a4. can take Python data
hierarchies, and convert them to string representations; this process is
called `serializing'. Reconstructing the data from the string
representation is called `deserializing'. Between serializing and
deserializing, the string representing the object may have been stored
in a file or data, or sent over a network connection to some distant
machine.
Note: The JSON format is commonly used by modern applications to
allow for data exchange. Many programmers are already familiar
with it, which makes it a good choice for interoperability.
If you have an object ‘x’, you can view its JSON string representation
with a simple line of code:
>>> import json
>>> json.dumps([1, 'simple', 'list'])
'[1, "simple", "list"]'
Another variant of the *note dumps(): 605. function, called *note
dump(): 604, simply serializes the object to a *note text file: f3a. So
if ‘f’ is a *note text file: f3a. object opened for writing, we can do
this:
json.dump(x, f)
To decode the object again, if ‘f’ is a *note text file: f3a. object
which has been opened for reading:
x = json.load(f)
This simple serialization technique can handle lists and dictionaries,
but serializing arbitrary class instances in JSON requires a bit of
extra effort. The reference for the *note json: a4. module contains an
explanation of this.
See also
........
*note pickle: ca. - the pickle module
Contrary to *note JSON: f39, `pickle' is a protocol which allows the
serialization of arbitrarily complex Python objects. As such, it is
specific to Python and cannot be used to communicate with applications
written in other languages. It is also insecure by default:
deserializing pickle data coming from an untrusted source can execute
arbitrary code, if the data was crafted by a skilled attacker.
---------- Footnotes ----------
(1) http://json.org
File: python.info, Node: Errors and Exceptions, Next: Classes, Prev: Input and Output, Up: The Python Tutorial
2.8 Errors and Exceptions
=========================
Until now error messages haven’t been more than mentioned, but if you
have tried out the examples you have probably seen some. There are (at
least) two distinguishable kinds of errors: `syntax errors' and
`exceptions'.
* Menu:
* Syntax Errors::
* Exceptions::
* Handling Exceptions::
* Raising Exceptions::
* User-defined Exceptions::
* Defining Clean-up Actions::
* Predefined Clean-up Actions::
File: python.info, Node: Syntax Errors, Next: Exceptions, Up: Errors and Exceptions
2.8.1 Syntax Errors
-------------------
Syntax errors, also known as parsing errors, are perhaps the most common
kind of complaint you get while you are still learning Python:
>>> while True print('Hello world')
File "", line 1
while True print('Hello world')
^
SyntaxError: invalid syntax
The parser repeats the offending line and displays a little ‘arrow’
pointing at the earliest point in the line where the error was detected.
The error is caused by (or at least detected at) the token `preceding'
the arrow: in the example, the error is detected at the function *note
print(): 886, since a colon (‘':'’) is missing before it. File name and
line number are printed so you know where to look in case the input came
from a script.
File: python.info, Node: Exceptions, Next: Handling Exceptions, Prev: Syntax Errors, Up: Errors and Exceptions
2.8.2 Exceptions
----------------
Even if a statement or expression is syntactically correct, it may cause
an error when an attempt is made to execute it. Errors detected during
execution are called `exceptions' and are not unconditionally fatal: you
will soon learn how to handle them in Python programs. Most exceptions
are not handled by programs, however, and result in error messages as
shown here:
>>> 10 * (1/0)
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
File "", line 1, in
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
File "", line 1, in
TypeError: Can't convert 'int' object to str implicitly
The last line of the error message indicates what happened. Exceptions
come in different types, and the type is printed as part of the message:
the types in the example are *note ZeroDivisionError: f42, *note
NameError: d7b. and *note TypeError: 192. The string printed as the
exception type is the name of the built-in exception that occurred.
This is true for all built-in exceptions, but need not be true for
user-defined exceptions (although it is a useful convention). Standard
exception names are built-in identifiers (not reserved keywords).
The rest of the line provides detail based on the type of exception and
what caused it.
The preceding part of the error message shows the context where the
exception happened, in the form of a stack traceback. In general it
contains a stack traceback listing source lines; however, it will not
display lines read from standard input.
*note Built-in Exceptions: f43. lists the built-in exceptions and their
meanings.
File: python.info, Node: Handling Exceptions, Next: Raising Exceptions, Prev: Exceptions, Up: Errors and Exceptions
2.8.3 Handling Exceptions
-------------------------
It is possible to write programs that handle selected exceptions. Look
at the following example, which asks the user for input until a valid
integer has been entered, but allows the user to interrupt the program
(using ‘Control-C’ or whatever the operating system supports); note that
a user-generated interruption is signalled by raising the *note
KeyboardInterrupt: 197. exception.
>>> while True:
... try:
... x = int(input("Please enter a number: "))
... break
... except ValueError:
... print("Oops! That was no valid number. Try again...")
...
The *note try: d72. statement works as follows.
* First, the `try clause' (the statement(s) between the *note try:
d72. and *note except: b3e. keywords) is executed.
* If no exception occurs, the `except clause' is skipped and
execution of the *note try: d72. statement is finished.
* If an exception occurs during execution of the try clause, the rest
of the clause is skipped. Then if its type matches the exception
named after the *note except: b3e. keyword, the except clause is
executed, and then execution continues after the *note try: d72.
statement.
* If an exception occurs which does not match the exception named in
the except clause, it is passed on to outer *note try: d72.
statements; if no handler is found, it is an `unhandled exception'
and execution stops with a message as shown above.
A *note try: d72. statement may have more than one except clause, to
specify handlers for different exceptions. At most one handler will be
executed. Handlers only handle exceptions that occur in the
corresponding try clause, not in other handlers of the same ‘try’
statement. An except clause may name multiple exceptions as a
parenthesized tuple, for example:
... except (RuntimeError, TypeError, NameError):
... pass
A class in an *note except: b3e. clause is compatible with an exception
if it is the same class or a base class thereof (but not the other way
around — an except clause listing a derived class is not compatible with
a base class). For example, the following code will print B, C, D in
that order:
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D:
print("D")
except C:
print("C")
except B:
print("B")
Note that if the except clauses were reversed (with ‘except B’ first),
it would have printed B, B, B — the first matching except clause is
triggered.
The last except clause may omit the exception name(s), to serve as a
wildcard. Use this with extreme caution, since it is easy to mask a
real programming error in this way! It can also be used to print an
error message and then re-raise the exception (allowing a caller to
handle the exception as well):
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
The *note try: d72. … *note except: b3e. statement has an optional `else
clause', which, when present, must follow all except clauses. It is
useful for code that must be executed if the try clause does not raise
an exception. For example:
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except OSError:
print('cannot open', arg)
else:
print(arg, 'has', len(f.readlines()), 'lines')
f.close()
The use of the ‘else’ clause is better than adding additional code to
the *note try: d72. clause because it avoids accidentally catching an
exception that wasn’t raised by the code being protected by the ‘try’ …
‘except’ statement.
When an exception occurs, it may have an associated value, also known as
the exception’s `argument'. The presence and type of the argument
depend on the exception type.
The except clause may specify a variable after the exception name. The
variable is bound to an exception instance with the arguments stored in
‘instance.args’. For convenience, the exception instance defines *note
__str__(): e37. so the arguments can be printed directly without having
to reference ‘.args’. One may also instantiate an exception first
before raising it and add any attributes to it as desired.
>>> try:
... raise Exception('spam', 'eggs')
... except Exception as inst:
... print(type(inst)) # the exception instance
... print(inst.args) # arguments stored in .args
... print(inst) # __str__ allows args to be printed directly,
... # but may be overridden in exception subclasses
... x, y = inst.args # unpack args
... print('x =', x)
... print('y =', y)
...
('spam', 'eggs')
('spam', 'eggs')
x = spam
y = eggs
If an exception has arguments, they are printed as the last part
(‘detail’) of the message for unhandled exceptions.
Exception handlers don’t just handle exceptions if they occur
immediately in the try clause, but also if they occur inside functions
that are called (even indirectly) in the try clause. For example:
>>> def this_fails():
... x = 1/0
...
>>> try:
... this_fails()
... except ZeroDivisionError as err:
... print('Handling run-time error:', err)
...
Handling run-time error: division by zero
File: python.info, Node: Raising Exceptions, Next: User-defined Exceptions, Prev: Handling Exceptions, Up: Errors and Exceptions
2.8.4 Raising Exceptions
------------------------
The *note raise: c42. statement allows the programmer to force a
specified exception to occur. For example:
>>> raise NameError('HiThere')
Traceback (most recent call last):
File "", line 1, in
NameError: HiThere
The sole argument to *note raise: c42. indicates the exception to be
raised. This must be either an exception instance or an exception class
(a class that derives from *note Exception: 1a9.). If an exception
class is passed, it will be implicitly instantiated by calling its
constructor with no arguments:
raise ValueError # shorthand for 'raise ValueError()'
If you need to determine whether an exception was raised but don’t
intend to handle it, a simpler form of the *note raise: c42. statement
allows you to re-raise the exception:
>>> try:
... raise NameError('HiThere')
... except NameError:
... print('An exception flew by!')
... raise
...
An exception flew by!
Traceback (most recent call last):
File "", line 2, in
NameError: HiThere
File: python.info, Node: User-defined Exceptions, Next: Defining Clean-up Actions, Prev: Raising Exceptions, Up: Errors and Exceptions
2.8.5 User-defined Exceptions
-----------------------------
Programs may name their own exceptions by creating a new exception class
(see *note Classes: ed9. for more about Python classes). Exceptions
should typically be derived from the *note Exception: 1a9. class, either
directly or indirectly.
Exception classes can be defined which do anything any other class can
do, but are usually kept simple, often only offering a number of
attributes that allow information about the error to be extracted by
handlers for the exception. When creating a module that can raise
several distinct errors, a common practice is to create a base class for
exceptions defined by that module, and subclass that to create specific
exception classes for different error conditions:
class Error(Exception):
"""Base class for exceptions in this module."""
pass
class InputError(Error):
"""Exception raised for errors in the input.
Attributes:
expression -- input expression in which the error occurred
message -- explanation of the error
"""
def __init__(self, expression, message):
self.expression = expression
self.message = message
class TransitionError(Error):
"""Raised when an operation attempts a state transition that's not
allowed.
Attributes:
previous -- state at beginning of transition
next -- attempted new state
message -- explanation of why the specific transition is not allowed
"""
def __init__(self, previous, next, message):
self.previous = previous
self.next = next
self.message = message
Most exceptions are defined with names that end in “Error”, similar to
the naming of the standard exceptions.
Many standard modules define their own exceptions to report errors that
may occur in functions they define. More information on classes is
presented in chapter *note Classes: ed9.
File: python.info, Node: Defining Clean-up Actions, Next: Predefined Clean-up Actions, Prev: User-defined Exceptions, Up: Errors and Exceptions
2.8.6 Defining Clean-up Actions
-------------------------------
The *note try: d72. statement has another optional clause which is
intended to define clean-up actions that must be executed under all
circumstances. For example:
>>> try:
... raise KeyboardInterrupt
... finally:
... print('Goodbye, world!')
...
Goodbye, world!
Traceback (most recent call last):
File "", line 2, in
KeyboardInterrupt
If a *note finally: 182. clause is present, the ‘finally’ clause will
execute as the last task before the *note try: d72. statement completes.
The ‘finally’ clause runs whether or not the ‘try’ statement produces an
exception. The following points discuss more complex cases when an
exception occurs:
* If an exception occurs during execution of the ‘try’ clause, the
exception may be handled by an *note except: b3e. clause. If the
exception is not handled by an ‘except’ clause, the exception is
re-raised after the ‘finally’ clause has been executed.
* An exception could occur during execution of an ‘except’ or ‘else’
clause. Again, the exception is re-raised after the ‘finally’
clause has been executed.
* If the ‘try’ statement reaches a *note break: 2db, *note continue:
181. or *note return: 190. statement, the ‘finally’ clause will
execute just prior to the ‘break’, ‘continue’ or ‘return’
statement’s execution.
* If a ‘finally’ clause includes a ‘return’ statement, the returned
value will be the one from the ‘finally’ clause’s ‘return’
statement, not the value from the ‘try’ clause’s ‘return’
statement.
For example:
>>> def bool_return():
... try:
... return True
... finally:
... return False
...
>>> bool_return()
False
A more complicated example:
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print("division by zero!")
... else:
... print("result is", result)
... finally:
... print("executing finally clause")
...
>>> divide(2, 1)
result is 2.0
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
File "", line 1, in
File "", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'
As you can see, the *note finally: 182. clause is executed in any event.
The *note TypeError: 192. raised by dividing two strings is not handled
by the *note except: b3e. clause and therefore re-raised after the
‘finally’ clause has been executed.
In real world applications, the *note finally: 182. clause is useful for
releasing external resources (such as files or network connections),
regardless of whether the use of the resource was successful.
File: python.info, Node: Predefined Clean-up Actions, Prev: Defining Clean-up Actions, Up: Errors and Exceptions
2.8.7 Predefined Clean-up Actions
---------------------------------
Some objects define standard clean-up actions to be undertaken when the
object is no longer needed, regardless of whether or not the operation
using the object succeeded or failed. Look at the following example,
which tries to open a file and print its contents to the screen.
for line in open("myfile.txt"):
print(line, end="")
The problem with this code is that it leaves the file open for an
indeterminate amount of time after this part of the code has finished
executing. This is not an issue in simple scripts, but can be a problem
for larger applications. The *note with: 6e9. statement allows objects
like files to be used in a way that ensures they are always cleaned up
promptly and correctly.
with open("myfile.txt") as f:
for line in f:
print(line, end="")
After the statement is executed, the file `f' is always closed, even if
a problem was encountered while processing the lines. Objects which,
like files, provide predefined clean-up actions will indicate this in
their documentation.
File: python.info, Node: Classes, Next: Brief Tour of the Standard Library, Prev: Errors and Exceptions, Up: The Python Tutorial
2.9 Classes
===========
Classes provide a means of bundling data and functionality together.
Creating a new class creates a new `type' of object, allowing new
`instances' of that type to be made. Each class instance can have
attributes attached to it for maintaining its state. Class instances
can also have methods (defined by its class) for modifying its state.
Compared with other programming languages, Python’s class mechanism adds
classes with a minimum of new syntax and semantics. It is a mixture of
the class mechanisms found in C++ and Modula-3. Python classes provide
all the standard features of Object Oriented Programming: the class
inheritance mechanism allows multiple base classes, a derived class can
override any methods of its base class or classes, and a method can call
the method of a base class with the same name. Objects can contain
arbitrary amounts and kinds of data. As is true for modules, classes
partake of the dynamic nature of Python: they are created at runtime,
and can be modified further after creation.
In C++ terminology, normally class members (including the data members)
are `public' (except see below *note Private Variables: f4f.), and all
member functions are `virtual'. As in Modula-3, there are no shorthands
for referencing the object’s members from its methods: the method
function is declared with an explicit first argument representing the
object, which is provided implicitly by the call. As in Smalltalk,
classes themselves are objects. This provides semantics for importing
and renaming. Unlike C++ and Modula-3, built-in types can be used as
base classes for extension by the user. Also, like in C++, most
built-in operators with special syntax (arithmetic operators,
subscripting etc.) can be redefined for class instances.
(Lacking universally accepted terminology to talk about classes, I will
make occasional use of Smalltalk and C++ terms. I would use Modula-3
terms, since its object-oriented semantics are closer to those of Python
than C++, but I expect that few readers have heard of it.)
* Menu:
* A Word About Names and Objects::
* Python Scopes and Namespaces::
* A First Look at Classes::
* Random Remarks::
* Inheritance::
* Private Variables::
* Odds and Ends::
* Iterators::
* Generators::
* Generator Expressions::
File: python.info, Node: A Word About Names and Objects, Next: Python Scopes and Namespaces, Up: Classes
2.9.1 A Word About Names and Objects
------------------------------------
Objects have individuality, and multiple names (in multiple scopes) can
be bound to the same object. This is known as aliasing in other
languages. This is usually not appreciated on a first glance at Python,
and can be safely ignored when dealing with immutable basic types
(numbers, strings, tuples). However, aliasing has a possibly surprising
effect on the semantics of Python code involving mutable objects such as
lists, dictionaries, and most other types. This is usually used to the
benefit of the program, since aliases behave like pointers in some
respects. For example, passing an object is cheap since only a pointer
is passed by the implementation; and if a function modifies an object
passed as an argument, the caller will see the change — this eliminates
the need for two different argument passing mechanisms as in Pascal.
File: python.info, Node: Python Scopes and Namespaces, Next: A First Look at Classes, Prev: A Word About Names and Objects, Up: Classes
2.9.2 Python Scopes and Namespaces
----------------------------------
Before introducing classes, I first have to tell you something about
Python’s scope rules. Class definitions play some neat tricks with
namespaces, and you need to know how scopes and namespaces work to fully
understand what’s going on. Incidentally, knowledge about this subject
is useful for any advanced Python programmer.
Let’s begin with some definitions.
A `namespace' is a mapping from names to objects. Most namespaces are
currently implemented as Python dictionaries, but that’s normally not
noticeable in any way (except for performance), and it may change in the
future. Examples of namespaces are: the set of built-in names
(containing functions such as *note abs(): f54, and built-in exception
names); the global names in a module; and the local names in a function
invocation. In a sense the set of attributes of an object also form a
namespace. The important thing to know about namespaces is that there
is absolutely no relation between names in different namespaces; for
instance, two different modules may both define a function ‘maximize’
without confusion — users of the modules must prefix it with the module
name.
By the way, I use the word `attribute' for any name following a dot —
for example, in the expression ‘z.real’, ‘real’ is an attribute of the
object ‘z’. Strictly speaking, references to names in modules are
attribute references: in the expression ‘modname.funcname’, ‘modname’ is
a module object and ‘funcname’ is an attribute of it. In this case
there happens to be a straightforward mapping between the module’s
attributes and the global names defined in the module: they share the
same namespace! (1)
Attributes may be read-only or writable. In the latter case, assignment
to attributes is possible. Module attributes are writable: you can
write ‘modname.the_answer = 42’. Writable attributes may also be
deleted with the *note del: f02. statement. For example, ‘del
modname.the_answer’ will remove the attribute ‘the_answer’ from the
object named by ‘modname’.
Namespaces are created at different moments and have different
lifetimes. The namespace containing the built-in names is created when
the Python interpreter starts up, and is never deleted. The global
namespace for a module is created when the module definition is read in;
normally, module namespaces also last until the interpreter quits. The
statements executed by the top-level invocation of the interpreter,
either read from a script file or interactively, are considered part of
a module called *note __main__: 1, so they have their own global
namespace. (The built-in names actually also live in a module; this is
called *note builtins: 13.)
The local namespace for a function is created when the function is
called, and deleted when the function returns or raises an exception
that is not handled within the function. (Actually, forgetting would be
a better way to describe what actually happens.) Of course, recursive
invocations each have their own local namespace.
A `scope' is a textual region of a Python program where a namespace is
directly accessible. “Directly accessible” here means that an
unqualified reference to a name attempts to find the name in the
namespace.
Although scopes are determined statically, they are used dynamically.
At any time during execution, there are 3 or 4 nested scopes whose
namespaces are directly accessible:
* the innermost scope, which is searched first, contains the local
names
* the scopes of any enclosing functions, which are searched starting
with the nearest enclosing scope, contains non-local, but also
non-global names
* the next-to-last scope contains the current module’s global names
* the outermost scope (searched last) is the namespace containing
built-in names
If a name is declared global, then all references and assignments go
directly to the middle scope containing the module’s global names. To
rebind variables found outside of the innermost scope, the *note
nonlocal: c3f. statement can be used; if not declared nonlocal, those
variables are read-only (an attempt to write to such a variable will
simply create a `new' local variable in the innermost scope, leaving the
identically named outer variable unchanged).
Usually, the local scope references the local names of the (textually)
current function. Outside functions, the local scope references the
same namespace as the global scope: the module’s namespace. Class
definitions place yet another namespace in the local scope.
It is important to realize that scopes are determined textually: the
global scope of a function defined in a module is that module’s
namespace, no matter from where or by what alias the function is called.
On the other hand, the actual search for names is done dynamically, at
run time — however, the language definition is evolving towards static
name resolution, at “compile” time, so don’t rely on dynamic name
resolution! (In fact, local variables are already determined
statically.)
A special quirk of Python is that – if no *note global: ed8. or *note
nonlocal: c3f. statement is in effect – assignments to names always go
into the innermost scope. Assignments do not copy data — they just bind
names to objects. The same is true for deletions: the statement ‘del x’
removes the binding of ‘x’ from the namespace referenced by the local
scope. In fact, all operations that introduce new names use the local
scope: in particular, *note import: c1d. statements and function
definitions bind the module or function name in the local scope.
The *note global: ed8. statement can be used to indicate that particular
variables live in the global scope and should be rebound there; the
*note nonlocal: c3f. statement indicates that particular variables live
in an enclosing scope and should be rebound there.
* Menu:
* Scopes and Namespaces Example::
---------- Footnotes ----------
(1) (1) Except for one thing. Module objects have a secret read-only
attribute called *note __dict__: 4fc. which returns the dictionary used
to implement the module’s namespace; the name *note __dict__: 4fc. is an
attribute but not a global name. Obviously, using this violates the
abstraction of namespace implementation, and should be restricted to
things like post-mortem debuggers.
File: python.info, Node: Scopes and Namespaces Example, Up: Python Scopes and Namespaces
2.9.2.1 Scopes and Namespaces Example
.....................................
This is an example demonstrating how to reference the different scopes
and namespaces, and how *note global: ed8. and *note nonlocal: c3f.
affect variable binding:
def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment:", spam)
do_nonlocal()
print("After nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam)
scope_test()
print("In global scope:", spam)
The output of the example code is:
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
Note how the `local' assignment (which is default) didn’t change
`scope_test'’s binding of `spam'. The *note nonlocal: c3f. assignment
changed `scope_test'’s binding of `spam', and the *note global: ed8.
assignment changed the module-level binding.
You can also see that there was no previous binding for `spam' before
the *note global: ed8. assignment.
File: python.info, Node: A First Look at Classes, Next: Random Remarks, Prev: Python Scopes and Namespaces, Up: Classes
2.9.3 A First Look at Classes
-----------------------------
Classes introduce a little bit of new syntax, three new object types,
and some new semantics.
* Menu:
* Class Definition Syntax::
* Class Objects::
* Instance Objects::
* Method Objects::
* Class and Instance Variables::
File: python.info, Node: Class Definition Syntax, Next: Class Objects, Up: A First Look at Classes
2.9.3.1 Class Definition Syntax
...............................
The simplest form of class definition looks like this:
class ClassName:
.
.
.
Class definitions, like function definitions (*note def: dbe.
statements) must be executed before they have any effect. (You could
conceivably place a class definition in a branch of an *note if: de7.
statement, or inside a function.)
In practice, the statements inside a class definition will usually be
function definitions, but other statements are allowed, and sometimes
useful — we’ll come back to this later. The function definitions inside
a class normally have a peculiar form of argument list, dictated by the
calling conventions for methods — again, this is explained later.
When a class definition is entered, a new namespace is created, and used
as the local scope — thus, all assignments to local variables go into
this new namespace. In particular, function definitions bind the name
of the new function here.
When a class definition is left normally (via the end), a `class object'
is created. This is basically a wrapper around the contents of the
namespace created by the class definition; we’ll learn more about class
objects in the next section. The original local scope (the one in
effect just before the class definition was entered) is reinstated, and
the class object is bound here to the class name given in the class
definition header (‘ClassName’ in the example).
File: python.info, Node: Class Objects, Next: Instance Objects, Prev: Class Definition Syntax, Up: A First Look at Classes
2.9.3.2 Class Objects
.....................
Class objects support two kinds of operations: attribute references and
instantiation.
`Attribute references' use the standard syntax used for all attribute
references in Python: ‘obj.name’. Valid attribute names are all the
names that were in the class’s namespace when the class object was
created. So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
then ‘MyClass.i’ and ‘MyClass.f’ are valid attribute references,
returning an integer and a function object, respectively. Class
attributes can also be assigned to, so you can change the value of
‘MyClass.i’ by assignment. ‘__doc__’ is also a valid attribute,
returning the docstring belonging to the class: ‘"A simple example
class"’.
Class `instantiation' uses function notation. Just pretend that the
class object is a parameterless function that returns a new instance of
the class. For example (assuming the above class):
x = MyClass()
creates a new `instance' of the class and assigns this object to the
local variable ‘x’.
The instantiation operation (“calling” a class object) creates an empty
object. Many classes like to create objects with instances customized
to a specific initial state. Therefore a class may define a special
method named *note __init__(): d5e, like this:
def __init__(self):
self.data = []
When a class defines an *note __init__(): d5e. method, class
instantiation automatically invokes *note __init__(): d5e. for the
newly-created class instance. So in this example, a new, initialized
instance can be obtained by:
x = MyClass()
Of course, the *note __init__(): d5e. method may have arguments for
greater flexibility. In that case, arguments given to the class
instantiation operator are passed on to *note __init__(): d5e. For
example,
>>> class Complex:
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)
File: python.info, Node: Instance Objects, Next: Method Objects, Prev: Class Objects, Up: A First Look at Classes
2.9.3.3 Instance Objects
........................
Now what can we do with instance objects? The only operations
understood by instance objects are attribute references. There are two
kinds of valid attribute names: data attributes and methods.
`data attributes' correspond to “instance variables” in Smalltalk, and
to “data members” in C++. Data attributes need not be declared; like
local variables, they spring into existence when they are first assigned
to. For example, if ‘x’ is the instance of ‘MyClass’ created above, the
following piece of code will print the value ‘16’, without leaving a
trace:
x.counter = 1
while x.counter < 10:
x.counter = x.counter * 2
print(x.counter)
del x.counter
The other kind of instance attribute reference is a `method'. A method
is a function that “belongs to” an object. (In Python, the term method
is not unique to class instances: other object types can have methods as
well. For example, list objects have methods called append, insert,
remove, sort, and so on. However, in the following discussion, we’ll
use the term method exclusively to mean methods of class instance
objects, unless explicitly stated otherwise.)
Valid method names of an instance object depend on its class. By
definition, all attributes of a class that are function objects define
corresponding methods of its instances. So in our example, ‘x.f’ is a
valid method reference, since ‘MyClass.f’ is a function, but ‘x.i’ is
not, since ‘MyClass.i’ is not. But ‘x.f’ is not the same thing as
‘MyClass.f’ — it is a `method object', not a function object.
File: python.info, Node: Method Objects, Next: Class and Instance Variables, Prev: Instance Objects, Up: A First Look at Classes
2.9.3.4 Method Objects
......................
Usually, a method is called right after it is bound:
x.f()
In the ‘MyClass’ example, this will return the string ‘'hello world'’.
However, it is not necessary to call a method right away: ‘x.f’ is a
method object, and can be stored away and called at a later time. For
example:
xf = x.f
while True:
print(xf())
will continue to print ‘hello world’ until the end of time.
What exactly happens when a method is called? You may have noticed that
‘x.f()’ was called without an argument above, even though the function
definition for ‘f()’ specified an argument. What happened to the
argument? Surely Python raises an exception when a function that
requires an argument is called without any — even if the argument isn’t
actually used…
Actually, you may have guessed the answer: the special thing about
methods is that the instance object is passed as the first argument of
the function. In our example, the call ‘x.f()’ is exactly equivalent to
‘MyClass.f(x)’. In general, calling a method with a list of `n'
arguments is equivalent to calling the corresponding function with an
argument list that is created by inserting the method’s instance object
before the first argument.
If you still don’t understand how methods work, a look at the
implementation can perhaps clarify matters. When a non-data attribute
of an instance is referenced, the instance’s class is searched. If the
name denotes a valid class attribute that is a function object, a method
object is created by packing (pointers to) the instance object and the
function object just found together in an abstract object: this is the
method object. When the method object is called with an argument list,
a new argument list is constructed from the instance object and the
argument list, and the function object is called with this new argument
list.
File: python.info, Node: Class and Instance Variables, Prev: Method Objects, Up: A First Look at Classes
2.9.3.5 Class and Instance Variables
....................................
Generally speaking, instance variables are for data unique to each
instance and class variables are for attributes and methods shared by
all instances of the class:
class Dog:
kind = 'canine' # class variable shared by all instances
def __init__(self, name):
self.name = name # instance variable unique to each instance
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind # shared by all dogs
'canine'
>>> e.kind # shared by all dogs
'canine'
>>> d.name # unique to d
'Fido'
>>> e.name # unique to e
'Buddy'
As discussed in *note A Word About Names and Objects: f51, shared data
can have possibly surprising effects with involving *note mutable: ebe.
objects such as lists and dictionaries. For example, the `tricks' list
in the following code should not be used as a class variable because
just a single list would be shared by all `Dog' instances:
class Dog:
tricks = [] # mistaken use of a class variable
def __init__(self, name):
self.name = name
def add_trick(self, trick):
self.tricks.append(trick)
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks # unexpectedly shared by all dogs
['roll over', 'play dead']
Correct design of the class should use an instance variable instead:
class Dog:
def __init__(self, name):
self.name = name
self.tricks = [] # creates a new empty list for each dog
def add_trick(self, trick):
self.tricks.append(trick)
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks
['roll over']
>>> e.tricks
['play dead']
File: python.info, Node: Random Remarks, Next: Inheritance, Prev: A First Look at Classes, Up: Classes
2.9.4 Random Remarks
--------------------
If the same attribute name occurs in both an instance and in a class,
then attribute lookup prioritizes the instance:
>>> class Warehouse:
purpose = 'storage'
region = 'west'
>>> w1 = Warehouse()
>>> print(w1.purpose, w1.region)
storage west
>>> w2 = Warehouse()
>>> w2.region = 'east'
>>> print(w2.purpose, w2.region)
storage east
Data attributes may be referenced by methods as well as by ordinary
users (“clients”) of an object. In other words, classes are not usable
to implement pure abstract data types. In fact, nothing in Python makes
it possible to enforce data hiding — it is all based upon convention.
(On the other hand, the Python implementation, written in C, can
completely hide implementation details and control access to an object
if necessary; this can be used by extensions to Python written in C.)
Clients should use data attributes with care — clients may mess up
invariants maintained by the methods by stamping on their data
attributes. Note that clients may add data attributes of their own to
an instance object without affecting the validity of the methods, as
long as name conflicts are avoided — again, a naming convention can save
a lot of headaches here.
There is no shorthand for referencing data attributes (or other
methods!) from within methods. I find that this actually increases the
readability of methods: there is no chance of confusing local variables
and instance variables when glancing through a method.
Often, the first argument of a method is called ‘self’. This is nothing
more than a convention: the name ‘self’ has absolutely no special
meaning to Python. Note, however, that by not following the convention
your code may be less readable to other Python programmers, and it is
also conceivable that a `class browser' program might be written that
relies upon such a convention.
Any function object that is a class attribute defines a method for
instances of that class. It is not necessary that the function
definition is textually enclosed in the class definition: assigning a
function object to a local variable in the class is also ok. For
example:
# Function defined outside the class
def f1(self, x, y):
return min(x, x+y)
class C:
f = f1
def g(self):
return 'hello world'
h = g
Now ‘f’, ‘g’ and ‘h’ are all attributes of class ‘C’ that refer to
function objects, and consequently they are all methods of instances of
‘C’ — ‘h’ being exactly equivalent to ‘g’. Note that this practice
usually only serves to confuse the reader of a program.
Methods may call other methods by using method attributes of the ‘self’
argument:
class Bag:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
Methods may reference global names in the same way as ordinary
functions. The global scope associated with a method is the module
containing its definition. (A class is never used as a global scope.)
While one rarely encounters a good reason for using global data in a
method, there are many legitimate uses of the global scope: for one
thing, functions and modules imported into the global scope can be used
by methods, as well as functions and classes defined in it. Usually,
the class containing the method is itself defined in this global scope,
and in the next section we’ll find some good reasons why a method would
want to reference its own class.
Each value is an object, and therefore has a `class' (also called its
`type'). It is stored as ‘object.__class__’.
File: python.info, Node: Inheritance, Next: Private Variables, Prev: Random Remarks, Up: Classes
2.9.5 Inheritance
-----------------
Of course, a language feature would not be worthy of the name “class”
without supporting inheritance. The syntax for a derived class
definition looks like this:
class DerivedClassName(BaseClassName):
.
.
.
The name ‘BaseClassName’ must be defined in a scope containing the
derived class definition. In place of a base class name, other
arbitrary expressions are also allowed. This can be useful, for
example, when the base class is defined in another module:
class DerivedClassName(modname.BaseClassName):
Execution of a derived class definition proceeds the same as for a base
class. When the class object is constructed, the base class is
remembered. This is used for resolving attribute references: if a
requested attribute is not found in the class, the search proceeds to
look in the base class. This rule is applied recursively if the base
class itself is derived from some other class.
There’s nothing special about instantiation of derived classes:
‘DerivedClassName()’ creates a new instance of the class. Method
references are resolved as follows: the corresponding class attribute is
searched, descending down the chain of base classes if necessary, and
the method reference is valid if this yields a function object.
Derived classes may override methods of their base classes. Because
methods have no special privileges when calling other methods of the
same object, a method of a base class that calls another method defined
in the same base class may end up calling a method of a derived class
that overrides it. (For C++ programmers: all methods in Python are
effectively ‘virtual’.)
An overriding method in a derived class may in fact want to extend
rather than simply replace the base class method of the same name.
There is a simple way to call the base class method directly: just call
‘BaseClassName.methodname(self, arguments)’. This is occasionally
useful to clients as well. (Note that this only works if the base class
is accessible as ‘BaseClassName’ in the global scope.)
Python has two built-in functions that work with inheritance:
* Use *note isinstance(): 44f. to check an instance’s type:
‘isinstance(obj, int)’ will be ‘True’ only if ‘obj.__class__’ is
*note int: 184. or some class derived from *note int: 184.
* Use *note issubclass(): 450. to check class inheritance:
‘issubclass(bool, int)’ is ‘True’ since *note bool: 183. is a
subclass of *note int: 184. However, ‘issubclass(float, int)’ is
‘False’ since *note float: 187. is not a subclass of *note int:
184.
* Menu:
* Multiple Inheritance::
File: python.info, Node: Multiple Inheritance, Up: Inheritance
2.9.5.1 Multiple Inheritance
............................
Python supports a form of multiple inheritance as well. A class
definition with multiple base classes looks like this:
class DerivedClassName(Base1, Base2, Base3):
.
.
.
For most purposes, in the simplest cases, you can think of the search
for attributes inherited from a parent class as depth-first,
left-to-right, not searching twice in the same class where there is an
overlap in the hierarchy. Thus, if an attribute is not found in
‘DerivedClassName’, it is searched for in ‘Base1’, then (recursively) in
the base classes of ‘Base1’, and if it was not found there, it was
searched for in ‘Base2’, and so on.
In fact, it is slightly more complex than that; the method resolution
order changes dynamically to support cooperative calls to *note super():
4e2. This approach is known in some other multiple-inheritance
languages as call-next-method and is more powerful than the super call
found in single-inheritance languages.
Dynamic ordering is necessary because all cases of multiple inheritance
exhibit one or more diamond relationships (where at least one of the
parent classes can be accessed through multiple paths from the
bottommost class). For example, all classes inherit from *note object:
2b0, so any case of multiple inheritance provides more than one path to
reach *note object: 2b0. To keep the base classes from being accessed
more than once, the dynamic algorithm linearizes the search order in a
way that preserves the left-to-right ordering specified in each class,
that calls each parent only once, and that is monotonic (meaning that a
class can be subclassed without affecting the precedence order of its
parents). Taken together, these properties make it possible to design
reliable and extensible classes with multiple inheritance. For more
detail, see ‘https://www.python.org/download/releases/2.3/mro/’.
File: python.info, Node: Private Variables, Next: Odds and Ends, Prev: Inheritance, Up: Classes
2.9.6 Private Variables
-----------------------
“Private” instance variables that cannot be accessed except from inside
an object don’t exist in Python. However, there is a convention that is
followed by most Python code: a name prefixed with an underscore (e.g.
‘_spam’) should be treated as a non-public part of the API (whether it
is a function, a method or a data member). It should be considered an
implementation detail and subject to change without notice.
Since there is a valid use-case for class-private members (namely to
avoid name clashes of names with names defined by subclasses), there is
limited support for such a mechanism, called `name mangling'. Any
identifier of the form ‘__spam’ (at least two leading underscores, at
most one trailing underscore) is textually replaced with
‘_classname__spam’, where ‘classname’ is the current class name with
leading underscore(s) stripped. This mangling is done without regard to
the syntactic position of the identifier, as long as it occurs within
the definition of a class.
Name mangling is helpful for letting subclasses override methods without
breaking intraclass method calls. For example:
class Mapping:
def __init__(self, iterable):
self.items_list = []
self.__update(iterable)
def update(self, iterable):
for item in iterable:
self.items_list.append(item)
__update = update # private copy of original update() method
class MappingSubclass(Mapping):
def update(self, keys, values):
# provides new signature for update()
# but does not break __init__()
for item in zip(keys, values):
self.items_list.append(item)
The above example would work even if ‘MappingSubclass’ were to introduce
a ‘__update’ identifier since it is replaced with ‘_Mapping__update’ in
the ‘Mapping’ class and ‘_MappingSubclass__update’ in the
‘MappingSubclass’ class respectively.
Note that the mangling rules are designed mostly to avoid accidents; it
still is possible to access or modify a variable that is considered
private. This can even be useful in special circumstances, such as in
the debugger.
Notice that code passed to ‘exec()’ or ‘eval()’ does not consider the
classname of the invoking class to be the current class; this is similar
to the effect of the ‘global’ statement, the effect of which is likewise
restricted to code that is byte-compiled together. The same restriction
applies to ‘getattr()’, ‘setattr()’ and ‘delattr()’, as well as when
referencing ‘__dict__’ directly.
File: python.info, Node: Odds and Ends, Next: Iterators, Prev: Private Variables, Up: Classes
2.9.7 Odds and Ends
-------------------
Sometimes it is useful to have a data type similar to the Pascal
“record” or C “struct”, bundling together a few named data items. An
empty class definition will do nicely:
class Employee:
pass
john = Employee() # Create an empty employee record
# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000
A piece of Python code that expects a particular abstract data type can
often be passed a class that emulates the methods of that data type
instead. For instance, if you have a function that formats some data
from a file object, you can define a class with methods ‘read()’ and
‘readline()’ that get the data from a string buffer instead, and pass it
as an argument.
Instance method objects have attributes, too: ‘m.__self__’ is the
instance object with the method ‘m()’, and ‘m.__func__’ is the function
object corresponding to the method.
File: python.info, Node: Iterators, Next: Generators, Prev: Odds and Ends, Up: Classes
2.9.8 Iterators
---------------
By now you have probably noticed that most container objects can be
looped over using a *note for: c30. statement:
for element in [1, 2, 3]:
print(element)
for element in (1, 2, 3):
print(element)
for key in {'one':1, 'two':2}:
print(key)
for char in "123":
print(char)
for line in open("myfile.txt"):
print(line, end='')
This style of access is clear, concise, and convenient. The use of
iterators pervades and unifies Python. Behind the scenes, the *note
for: c30. statement calls *note iter(): d27. on the container object.
The function returns an iterator object that defines the method *note
__next__(): c64. which accesses elements in the container one at a time.
When there are no more elements, *note __next__(): c64. raises a *note
StopIteration: 486. exception which tells the ‘for’ loop to terminate.
You can call the *note __next__(): c64. method using the *note next():
682. built-in function; this example shows how it all works:
>>> s = 'abc'
>>> it = iter(s)
>>> it
>>> next(it)
'a'
>>> next(it)
'b'
>>> next(it)
'c'
>>> next(it)
Traceback (most recent call last):
File "", line 1, in
next(it)
StopIteration
Having seen the mechanics behind the iterator protocol, it is easy to
add iterator behavior to your classes. Define an *note __iter__(): 50f.
method which returns an object with a *note __next__(): c64. method. If
the class defines ‘__next__()’, then *note __iter__(): 50f. can just
return ‘self’:
class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
>>> rev = Reverse('spam')
>>> iter(rev)
<__main__.Reverse object at 0x00A1DB50>
>>> for char in rev:
... print(char)
...
m
a
p
s
File: python.info, Node: Generators, Next: Generator Expressions, Prev: Iterators, Up: Classes
2.9.9 Generators
----------------
*note Generators: 9a2. are a simple and powerful tool for creating
iterators. They are written like regular functions but use the *note
yield: 18f. statement whenever they want to return data. Each time
*note next(): 682. is called on it, the generator resumes where it left
off (it remembers all the data values and which statement was last
executed). An example shows that generators can be trivially easy to
create:
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
>>> for char in reverse('golf'):
... print(char)
...
f
l
o
g
Anything that can be done with generators can also be done with
class-based iterators as described in the previous section. What makes
generators so compact is that the *note __iter__(): 50f. and *note
__next__(): f6f. methods are created automatically.
Another key feature is that the local variables and execution state are
automatically saved between calls. This made the function easier to
write and much more clear than an approach using instance variables like
‘self.index’ and ‘self.data’.
In addition to automatic method creation and saving program state, when
generators terminate, they automatically raise *note StopIteration: 486.
In combination, these features make it easy to create iterators with no
more effort than writing a regular function.
File: python.info, Node: Generator Expressions, Prev: Generators, Up: Classes
2.9.10 Generator Expressions
----------------------------
Some simple generators can be coded succinctly as expressions using a
syntax similar to list comprehensions but with parentheses instead of
square brackets. These expressions are designed for situations where
the generator is used right away by an enclosing function. Generator
expressions are more compact but less versatile than full generator
definitions and tend to be more memory friendly than equivalent list
comprehensions.
Examples:
>>> sum(i*i for i in range(10)) # sum of squares
285
>>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec)) # dot product
260
>>> unique_words = set(word for line in page for word in line.split())
>>> valedictorian = max((student.gpa, student.name) for student in graduates)
>>> data = 'golf'
>>> list(data[i] for i in range(len(data)-1, -1, -1))
['f', 'l', 'o', 'g']
File: python.info, Node: Brief Tour of the Standard Library, Next: Brief Tour of the Standard Library — Part II, Prev: Classes, Up: The Python Tutorial
2.10 Brief Tour of the Standard Library
=======================================
* Menu:
* Operating System Interface::
* File Wildcards::
* Command Line Arguments::
* Error Output Redirection and Program Termination::
* String Pattern Matching::
* Mathematics::
* Internet Access::
* Dates and Times::
* Data Compression::
* Performance Measurement::
* Quality Control::
* Batteries Included::
File: python.info, Node: Operating System Interface, Next: File Wildcards, Up: Brief Tour of the Standard Library
2.10.1 Operating System Interface
---------------------------------
The *note os: c4. module provides dozens of functions for interacting
with the operating system:
>>> import os
>>> os.getcwd() # Return the current working directory
'C:\\Python38'
>>> os.chdir('/server/accesslogs') # Change current working directory
>>> os.system('mkdir today') # Run the command mkdir in the system shell
0
Be sure to use the ‘import os’ style instead of ‘from os import *’.
This will keep *note os.open(): 665. from shadowing the built-in *note
open(): 4f0. function which operates much differently.
The built-in *note dir(): d33. and *note help(): 572. functions are
useful as interactive aids for working with large modules like *note os:
c4.:
>>> import os
>>> dir(os)
>>> help(os)
For daily file and directory management tasks, the *note shutil: e9.
module provides a higher level interface that is easier to use:
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'
File: python.info, Node: File Wildcards, Next: Command Line Arguments, Prev: Operating System Interface, Up: Brief Tour of the Standard Library
2.10.2 File Wildcards
---------------------
The *note glob: 8a. module provides a function for making file lists
from directory wildcard searches:
>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']
File: python.info, Node: Command Line Arguments, Next: Error Output Redirection and Program Termination, Prev: File Wildcards, Up: Brief Tour of the Standard Library
2.10.3 Command Line Arguments
-----------------------------
Common utility scripts often need to process command line arguments.
These arguments are stored in the *note sys: fd. module’s `argv'
attribute as a list. For instance the following output results from
running ‘python demo.py one two three’ at the command line:
>>> import sys
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']
The *note argparse: 6. module provides a more sophisticated mechanism to
process command line arguments. The following script extracts one or
more filenames and an optional number of lines to be displayed:
import argparse
parser = argparse.ArgumentParser(prog = 'top',
description = 'Show top lines from each file')
parser.add_argument('filenames', nargs='+')
parser.add_argument('-l', '--lines', type=int, default=10)
args = parser.parse_args()
print(args)
When run at the command line with ‘python top.py --lines=5 alpha.txt
beta.txt’, the script sets ‘args.lines’ to ‘5’ and ‘args.filenames’ to
‘['alpha.txt', 'beta.txt']’.
File: python.info, Node: Error Output Redirection and Program Termination, Next: String Pattern Matching, Prev: Command Line Arguments, Up: Brief Tour of the Standard Library
2.10.4 Error Output Redirection and Program Termination
-------------------------------------------------------
The *note sys: fd. module also has attributes for `stdin', `stdout', and
`stderr'. The latter is useful for emitting warnings and error messages
to make them visible even when `stdout' has been redirected:
>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one
The most direct way to terminate a script is to use ‘sys.exit()’.
File: python.info, Node: String Pattern Matching, Next: Mathematics, Prev: Error Output Redirection and Program Termination, Up: Brief Tour of the Standard Library
2.10.5 String Pattern Matching
------------------------------
The *note re: dd. module provides regular expression tools for advanced
string processing. For complex matching and manipulation, regular
expressions offer succinct, optimized solutions:
>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'
When only simple capabilities are needed, string methods are preferred
because they are easier to read and debug:
>>> 'tea for too'.replace('too', 'two')
'tea for two'
File: python.info, Node: Mathematics, Next: Internet Access, Prev: String Pattern Matching, Up: Brief Tour of the Standard Library
2.10.6 Mathematics
------------------
The *note math: b1. module gives access to the underlying C library
functions for floating point math:
>>> import math
>>> math.cos(math.pi / 4)
0.70710678118654757
>>> math.log(1024, 2)
10.0
The *note random: dc. module provides tools for making random
selections:
>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(range(100), 10) # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random() # random float
0.17970987693706186
>>> random.randrange(6) # random integer chosen from range(6)
4
The *note statistics: f5. module calculates basic statistical properties
(the mean, median, variance, etc.) of numeric data:
>>> import statistics
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
>>> statistics.mean(data)
1.6071428571428572
>>> statistics.median(data)
1.25
>>> statistics.variance(data)
1.3720238095238095
The SciPy project <‘https://scipy.org’> has many other modules for
numerical computations.
File: python.info, Node: Internet Access, Next: Dates and Times, Prev: Mathematics, Up: Brief Tour of the Standard Library
2.10.7 Internet Access
----------------------
There are a number of modules for accessing the internet and processing
internet protocols. Two of the simplest are *note urllib.request: 120.
for retrieving data from URLs and *note smtplib: ed. for sending mail:
>>> from urllib.request import urlopen
>>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
... for line in response:
... line = line.decode('utf-8') # Decoding the binary data to text.
... if 'EST' in line or 'EDT' in line: # look for Eastern Time
... print(line)
Nov. 25, 09:43:32 PM EST
>>> import smtplib
>>> server = smtplib.SMTP('localhost')
>>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',
... """To: jcaesar@example.org
... From: soothsayer@example.org
...
... Beware the Ides of March.
... """)
>>> server.quit()
(Note that the second example needs a mailserver running on localhost.)
File: python.info, Node: Dates and Times, Next: Data Compression, Prev: Internet Access, Up: Brief Tour of the Standard Library
2.10.8 Dates and Times
----------------------
The *note datetime: 31. module supplies classes for manipulating dates
and times in both simple and complex ways. While date and time
arithmetic is supported, the focus of the implementation is on efficient
member extraction for output formatting and manipulation. The module
also supports objects that are timezone aware.
>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
>>> # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368
File: python.info, Node: Data Compression, Next: Performance Measurement, Prev: Dates and Times, Up: Brief Tour of the Standard Library
2.10.9 Data Compression
-----------------------
Common data archiving and compression formats are directly supported by
modules including: *note zlib: 144, *note gzip: 8c, *note bz2: 14, *note
lzma: ad, *note zipfile: 142. and *note tarfile: 101.
>>> import zlib
>>> s = b'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
b'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979
File: python.info, Node: Performance Measurement, Next: Quality Control, Prev: Data Compression, Up: Brief Tour of the Standard Library
2.10.10 Performance Measurement
-------------------------------
Some Python users develop a deep interest in knowing the relative
performance of different approaches to the same problem. Python
provides a measurement tool that answers those questions immediately.
For example, it may be tempting to use the tuple packing and unpacking
feature instead of the traditional approach to swapping arguments. The
*note timeit: 10b. module quickly demonstrates a modest performance
advantage:
>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.57535828626024577
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.54962537085770791
In contrast to *note timeit: 10b.’s fine level of granularity, the *note
profile: d3. and *note pstats: d4. modules provide tools for identifying
time critical sections in larger blocks of code.
File: python.info, Node: Quality Control, Next: Batteries Included, Prev: Performance Measurement, Up: Brief Tour of the Standard Library
2.10.11 Quality Control
-----------------------
One approach for developing high quality software is to write tests for
each function as it is developed and to run those tests frequently
during the development process.
The *note doctest: 67. module provides a tool for scanning a module and
validating tests embedded in a program’s docstrings. Test construction
is as simple as cutting-and-pasting a typical call along with its
results into the docstring. This improves the documentation by
providing the user with an example and it allows the doctest module to
make sure the code remains true to the documentation:
def average(values):
"""Computes the arithmetic mean of a list of numbers.
>>> print(average([20, 30, 70]))
40.0
"""
return sum(values) / len(values)
import doctest
doctest.testmod() # automatically validate the embedded tests
The *note unittest: 11b. module is not as effortless as the *note
doctest: 67. module, but it allows a more comprehensive set of tests to
be maintained in a separate file:
import unittest
class TestStatisticalFunctions(unittest.TestCase):
def test_average(self):
self.assertEqual(average([20, 30, 70]), 40.0)
self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
with self.assertRaises(ZeroDivisionError):
average([])
with self.assertRaises(TypeError):
average(20, 30, 70)
unittest.main() # Calling from the command line invokes all tests
File: python.info, Node: Batteries Included, Prev: Quality Control, Up: Brief Tour of the Standard Library
2.10.12 Batteries Included
--------------------------
Python has a “batteries included” philosophy. This is best seen through
the sophisticated and robust capabilities of its larger packages. For
example:
* The *note xmlrpc.client: 13f. and *note xmlrpc.server: 140. modules
make implementing remote procedure calls into an almost trivial
task. Despite the modules names, no direct knowledge or handling
of XML is needed.
* The *note email: 69. package is a library for managing email
messages, including MIME and other RFC 2822(1)-based message
documents. Unlike *note smtplib: ed. and *note poplib: d0. which
actually send and receive messages, the email package has a
complete toolset for building or decoding complex message
structures (including attachments) and for implementing internet
encoding and header protocols.
* The *note json: a4. package provides robust support for parsing
this popular data interchange format. The *note csv: 2a. module
supports direct reading and writing of files in Comma-Separated
Value format, commonly supported by databases and spreadsheets.
XML processing is supported by the *note xml.etree.ElementTree:
137, *note xml.dom: 134. and *note xml.sax: 13b. packages.
Together, these modules and packages greatly simplify data
interchange between Python applications and other tools.
* The *note sqlite3: f2. module is a wrapper for the SQLite database
library, providing a persistent database that can be updated and
accessed using slightly nonstandard SQL syntax.
* Internationalization is supported by a number of modules including
*note gettext: 89, *note locale: a9, and the *note codecs: 1c.
package.
---------- Footnotes ----------
(1) https://tools.ietf.org/html/rfc2822.html
File: python.info, Node: Brief Tour of the Standard Library — Part II, Next: Virtual Environments and Packages, Prev: Brief Tour of the Standard Library, Up: The Python Tutorial
2.11 Brief Tour of the Standard Library — Part II
=================================================
This second tour covers more advanced modules that support professional
programming needs. These modules rarely occur in small scripts.
* Menu:
* Output Formatting::
* Templating::
* Working with Binary Data Record Layouts::
* Multi-threading: Multi-threading<2>.
* Logging::
* Weak References::
* Tools for Working with Lists::
* Decimal Floating Point Arithmetic::
File: python.info, Node: Output Formatting, Next: Templating, Up: Brief Tour of the Standard Library — Part II
2.11.1 Output Formatting
------------------------
The *note reprlib: df. module provides a version of *note repr(): 7cc.
customized for abbreviated displays of large or deeply nested
containers:
>>> import reprlib
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))
"{'a', 'c', 'd', 'e', 'f', 'g', ...}"
The *note pprint: d2. module offers more sophisticated control over
printing both built-in and user defined objects in a way that is
readable by the interpreter. When the result is longer than one line,
the “pretty printer” adds line breaks and indentation to more clearly
reveal data structure:
>>> import pprint
>>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
... 'yellow'], 'blue']]]
...
>>> pprint.pprint(t, width=30)
[[[['black', 'cyan'],
'white',
['green', 'red']],
[['magenta', 'yellow'],
'blue']]]
The *note textwrap: 108. module formats paragraphs of text to fit a
given screen width:
>>> import textwrap
>>> doc = """The wrap() method is just like fill() except that it returns
... a list of strings instead of one big string with newlines to separate
... the wrapped lines."""
...
>>> print(textwrap.fill(doc, width=40))
The wrap() method is just like fill()
except that it returns a list of strings
instead of one big string with newlines
to separate the wrapped lines.
The *note locale: a9. module accesses a database of culture specific
data formats. The grouping attribute of locale’s format function
provides a direct way of formatting numbers with group separators:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv() # get a mapping of conventions
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'
>>> locale.format_string("%s%.*f", (conv['currency_symbol'],
... conv['frac_digits'], x), grouping=True)
'$1,234,567.80'
File: python.info, Node: Templating, Next: Working with Binary Data Record Layouts, Prev: Output Formatting, Up: Brief Tour of the Standard Library — Part II
2.11.2 Templating
-----------------
The *note string: f6. module includes a versatile *note Template: 3eb.
class with a simplified syntax suitable for editing by end-users. This
allows users to customize their applications without having to alter the
application.
The format uses placeholder names formed by ‘$’ with valid Python
identifiers (alphanumeric characters and underscores). Surrounding the
placeholder with braces allows it to be followed by more alphanumeric
letters with no intervening spaces. Writing ‘$$’ creates a single
escaped ‘$’:
>>> from string import Template
>>> t = Template('${village}folk send $$10 to $cause.')
>>> t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'
The *note substitute(): f94. method raises a *note KeyError: 2c7. when a
placeholder is not supplied in a dictionary or a keyword argument. For
mail-merge style applications, user supplied data may be incomplete and
the *note safe_substitute(): f95. method may be more appropriate — it
will leave placeholders unchanged if data is missing:
>>> t = Template('Return the $item to $owner.')
>>> d = dict(item='unladen swallow')
>>> t.substitute(d)
Traceback (most recent call last):
...
KeyError: 'owner'
>>> t.safe_substitute(d)
'Return the unladen swallow to $owner.'
Template subclasses can specify a custom delimiter. For example, a
batch renaming utility for a photo browser may elect to use percent
signs for placeholders such as the current date, image sequence number,
or file format:
>>> import time, os.path
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
>>> class BatchRename(Template):
... delimiter = '%'
>>> fmt = input('Enter rename style (%d-date %n-seqnum %f-format): ')
Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f
>>> t = BatchRename(fmt)
>>> date = time.strftime('%d%b%y')
>>> for i, filename in enumerate(photofiles):
... base, ext = os.path.splitext(filename)
... newname = t.substitute(d=date, n=i, f=ext)
... print('{0} --> {1}'.format(filename, newname))
img_1074.jpg --> Ashley_0.jpg
img_1076.jpg --> Ashley_1.jpg
img_1077.jpg --> Ashley_2.jpg
Another application for templating is separating program logic from the
details of multiple output formats. This makes it possible to
substitute custom templates for XML files, plain text reports, and HTML
web reports.
File: python.info, Node: Working with Binary Data Record Layouts, Next: Multi-threading<2>, Prev: Templating, Up: Brief Tour of the Standard Library — Part II
2.11.3 Working with Binary Data Record Layouts
----------------------------------------------
The *note struct: f8. module provides *note pack(): c0b. and *note
unpack(): f98. functions for working with variable length binary record
formats. The following example shows how to loop through header
information in a ZIP file without using the *note zipfile: 142. module.
Pack codes ‘"H"’ and ‘"I"’ represent two and four byte unsigned numbers
respectively. The ‘"<"’ indicates that they are standard size and in
little-endian byte order:
import struct
with open('myfile.zip', 'rb') as f:
data = f.read()
start = 0
for i in range(3): # show the first 3 file headers
start += 14
fields = struct.unpack(', Next: Logging, Prev: Working with Binary Data Record Layouts, Up: Brief Tour of the Standard Library — Part II
2.11.4 Multi-threading
----------------------
Threading is a technique for decoupling tasks which are not sequentially
dependent. Threads can be used to improve the responsiveness of
applications that accept user input while other tasks run in the
background. A related use case is running I/O in parallel with
computations in another thread.
The following code shows how the high level *note threading: 109. module
can run tasks in background while the main program continues to run:
import threading, zipfile
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print('Finished background zip of:', self.infile)
background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print('The main program continues to run in foreground.')
background.join() # Wait for the background task to finish
print('Main program waited until background was done.')
The principal challenge of multi-threaded applications is coordinating
threads that share data or other resources. To that end, the threading
module provides a number of synchronization primitives including locks,
events, condition variables, and semaphores.
While those tools are powerful, minor design errors can result in
problems that are difficult to reproduce. So, the preferred approach to
task coordination is to concentrate all access to a resource in a single
thread and then use the *note queue: da. module to feed that thread with
requests from other threads. Applications using *note Queue: d18.
objects for inter-thread communication and coordination are easier to
design, more readable, and more reliable.
File: python.info, Node: Logging, Next: Weak References, Prev: Multi-threading<2>, Up: Brief Tour of the Standard Library — Part II
2.11.5 Logging
--------------
The *note logging: aa. module offers a full featured and flexible
logging system. At its simplest, log messages are sent to a file or to
‘sys.stderr’:
import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')
This produces the following output:
WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down
By default, informational and debugging messages are suppressed and the
output is sent to standard error. Other output options include routing
messages through email, datagrams, sockets, or to an HTTP Server. New
filters can select different routing based on message priority: ‘DEBUG’,
‘INFO’, ‘WARNING’, ‘ERROR’, and ‘CRITICAL’.
The logging system can be configured directly from Python or can be
loaded from a user editable configuration file for customized logging
without altering the application.
File: python.info, Node: Weak References, Next: Tools for Working with Lists, Prev: Logging, Up: Brief Tour of the Standard Library — Part II
2.11.6 Weak References
----------------------
Python does automatic memory management (reference counting for most
objects and *note garbage collection: f9f. to eliminate cycles). The
memory is freed shortly after the last reference to it has been
eliminated.
This approach works fine for most applications but occasionally there is
a need to track objects only as long as they are being used by something
else. Unfortunately, just tracking them creates a reference that makes
them permanent. The *note weakref: 128. module provides tools for
tracking objects without creating a reference. When the object is no
longer needed, it is automatically removed from a weakref table and a
callback is triggered for weakref objects. Typical applications include
caching objects that are expensive to create:
>>> import weakref, gc
>>> class A:
... def __init__(self, value):
... self.value = value
... def __repr__(self):
... return str(self.value)
...
>>> a = A(10) # create a reference
>>> d = weakref.WeakValueDictionary()
>>> d['primary'] = a # does not create a reference
>>> d['primary'] # fetch the object if it is still alive
10
>>> del a # remove the one reference
>>> gc.collect() # run garbage collection right away
0
>>> d['primary'] # entry was automatically removed
Traceback (most recent call last):
File "", line 1, in
d['primary'] # entry was automatically removed
File "C:/python38/lib/weakref.py", line 46, in __getitem__
o = self.data[key]()
KeyError: 'primary'
File: python.info, Node: Tools for Working with Lists, Next: Decimal Floating Point Arithmetic, Prev: Weak References, Up: Brief Tour of the Standard Library — Part II
2.11.7 Tools for Working with Lists
-----------------------------------
Many data structure needs can be met with the built-in list type.
However, sometimes there is a need for alternative implementations with
different performance trade-offs.
The *note array: 7. module provides an *note array(): 451. object that
is like a list that stores only homogeneous data and stores it more
compactly. The following example shows an array of numbers stored as
two byte unsigned binary numbers (typecode ‘"H"’) rather than the usual
16 bytes per entry for regular lists of Python int objects:
>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a)
26932
>>> a[1:3]
array('H', [10, 700])
The *note collections: 1e. module provides a *note deque(): 531. object
that is like a list with faster appends and pops from the left side but
slower lookups in the middle. These objects are well suited for
implementing queues and breadth first tree searches:
>>> from collections import deque
>>> d = deque(["task1", "task2", "task3"])
>>> d.append("task4")
>>> print("Handling", d.popleft())
Handling task1
unsearched = deque([starting_node])
def breadth_first_search(unsearched):
node = unsearched.popleft()
for m in gen_moves(node):
if is_goal(m):
return m
unsearched.append(m)
In addition to alternative list implementations, the library also offers
other tools such as the *note bisect: 12. module with functions for
manipulating sorted lists:
>>> import bisect
>>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')]
>>> bisect.insort(scores, (300, 'ruby'))
>>> scores
[(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]
The *note heapq: 8e. module provides functions for implementing heaps
based on regular lists. The lowest valued entry is always kept at
position zero. This is useful for applications which repeatedly access
the smallest element but do not want to run a full list sort:
>>> from heapq import heapify, heappop, heappush
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
>>> heapify(data) # rearrange the list into heap order
>>> heappush(data, -5) # add a new entry
>>> [heappop(data) for i in range(3)] # fetch the three smallest entries
[-5, 0, 1]
File: python.info, Node: Decimal Floating Point Arithmetic, Prev: Tools for Working with Lists, Up: Brief Tour of the Standard Library — Part II
2.11.8 Decimal Floating Point Arithmetic
----------------------------------------
The *note decimal: 36. module offers a *note Decimal: 188. datatype for
decimal floating point arithmetic. Compared to the built-in *note
float: 187. implementation of binary floating point, the class is
especially helpful for
* financial applications and other uses which require exact decimal
representation,
* control over precision,
* control over rounding to meet legal or regulatory requirements,
* tracking of significant decimal places, or
* applications where the user expects the results to match
calculations done by hand.
For example, calculating a 5% tax on a 70 cent phone charge gives
different results in decimal floating point and binary floating point.
The difference becomes significant if the results are rounded to the
nearest cent:
>>> from decimal import *
>>> round(Decimal('0.70') * Decimal('1.05'), 2)
Decimal('0.74')
>>> round(.70 * 1.05, 2)
0.73
The *note Decimal: 188. result keeps a trailing zero, automatically
inferring four place significance from multiplicands with two place
significance. Decimal reproduces mathematics as done by hand and avoids
issues that can arise when binary floating point cannot exactly
represent decimal quantities.
Exact representation enables the *note Decimal: 188. class to perform
modulo calculations and equality tests that are unsuitable for binary
floating point:
>>> Decimal('1.00') % Decimal('.10')
Decimal('0.00')
>>> 1.00 % 0.10
0.09999999999999995
>>> sum([Decimal('0.1')]*10) == Decimal('1.0')
True
>>> sum([0.1]*10) == 1.0
False
The *note decimal: 36. module provides arithmetic with as much precision
as needed:
>>> getcontext().prec = 36
>>> Decimal(1) / Decimal(7)
Decimal('0.142857142857142857142857142857142857')
File: python.info, Node: Virtual Environments and Packages, Next: What Now?, Prev: Brief Tour of the Standard Library — Part II, Up: The Python Tutorial
2.12 Virtual Environments and Packages
======================================
* Menu:
* Introduction: Introduction<4>.
* Creating Virtual Environments::
* Managing Packages with pip::
File: python.info, Node: Introduction<4>, Next: Creating Virtual Environments, Up: Virtual Environments and Packages
2.12.1 Introduction
-------------------
Python applications will often use packages and modules that don’t come
as part of the standard library. Applications will sometimes need a
specific version of a library, because the application may require that
a particular bug has been fixed or the application may be written using
an obsolete version of the library’s interface.
This means it may not be possible for one Python installation to meet
the requirements of every application. If application A needs version
1.0 of a particular module but application B needs version 2.0, then the
requirements are in conflict and installing either version 1.0 or 2.0
will leave one application unable to run.
The solution for this problem is to create a *note virtual environment:
fa8, a self-contained directory tree that contains a Python installation
for a particular version of Python, plus a number of additional
packages.
Different applications can then use different virtual environments. To
resolve the earlier example of conflicting requirements, application A
can have its own virtual environment with version 1.0 installed while
application B has another virtual environment with version 2.0. If
application B requires a library be upgraded to version 3.0, this will
not affect application A’s environment.
File: python.info, Node: Creating Virtual Environments, Next: Managing Packages with pip, Prev: Introduction<4>, Up: Virtual Environments and Packages
2.12.2 Creating Virtual Environments
------------------------------------
The module used to create and manage virtual environments is called
*note venv: 125. *note venv: 125. will usually install the most recent
version of Python that you have available. If you have multiple
versions of Python on your system, you can select a specific Python
version by running ‘python3’ or whichever version you want.
To create a virtual environment, decide upon a directory where you want
to place it, and run the *note venv: 125. module as a script with the
directory path:
python3 -m venv tutorial-env
This will create the ‘tutorial-env’ directory if it doesn’t exist, and
also create directories inside it containing a copy of the Python
interpreter, the standard library, and various supporting files.
A common directory location for a virtual environment is ‘.venv’. This
name keeps the directory typically hidden in your shell and thus out of
the way while giving it a name that explains why the directory exists.
It also prevents clashing with ‘.env’ environment variable definition
files that some tooling supports.
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
(This script is written for the bash shell. If you use the ‘csh’ or
‘fish’ shells, there are alternate ‘activate.csh’ and ‘activate.fish’
scripts you should use instead.)
Activating the virtual environment will change your shell’s prompt to
show what virtual environment you’re using, and modify the environment
so that running ‘python’ will get you that particular version and
installation of Python. For example:
$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May 6 2016, 10:59:36)
...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>
File: python.info, Node: Managing Packages with pip, Prev: Creating Virtual Environments, Up: Virtual Environments and Packages
2.12.3 Managing Packages with pip
---------------------------------
You can install, upgrade, and remove packages using a program called
‘pip’. By default ‘pip’ will install packages from the Python Package
Index, <‘https://pypi.org’>. You can browse the Python Package Index by
going to it in your web browser, or you can use ‘pip’’s limited search
feature:
(tutorial-env) $ pip search astronomy
skyfield - Elegant astronomy for Python
gary - Galactic astronomy and gravitational dynamics.
novas - The United States Naval Observatory NOVAS astronomy library
astroobs - Provides astronomy ephemeris to plan telescope observations
PyAstronomy - A collection of astronomy related tools for Python.
...
‘pip’ has a number of subcommands: “search”, “install”, “uninstall”,
“freeze”, etc. (Consult the *note Installing Python Modules: 7f5. guide
for complete documentation for ‘pip’.)
You can install the latest version of a package by specifying a
package’s name:
(tutorial-env) $ pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
Running setup.py install for novas
Successfully installed novas-3.1.1.3
You can also install a specific version of a package by giving the
package name followed by ‘==’ and the version number:
(tutorial-env) $ pip install requests==2.6.0
Collecting requests==2.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0
If you re-run this command, ‘pip’ will notice that the requested version
is already installed and do nothing. You can supply a different version
number to get that version, or you can run ‘pip install --upgrade’ to
upgrade the package to the latest version:
(tutorial-env) $ pip install --upgrade requests
Collecting requests
Installing collected packages: requests
Found existing installation: requests 2.6.0
Uninstalling requests-2.6.0:
Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0
‘pip uninstall’ followed by one or more package names will remove the
packages from the virtual environment.
‘pip show’ will display information about a particular package:
(tutorial-env) $ pip show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache 2.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:
‘pip list’ will display all of the packages installed in the virtual
environment:
(tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)
‘pip freeze’ will produce a similar list of the installed packages, but
the output uses the format that ‘pip install’ expects. A common
convention is to put this list in a ‘requirements.txt’ file:
(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0
The ‘requirements.txt’ can then be committed to version control and
shipped as part of an application. Users can then install all the
necessary packages with ‘install -r’:
(tutorial-env) $ pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
...
Installing collected packages: novas, numpy, requests
Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
‘pip’ has many more options. Consult the *note Installing Python
Modules: 7f5. guide for complete documentation for ‘pip’. When you’ve
written a package and want to make it available on the Python Package
Index, consult the *note Distributing Python Modules: 7f6. guide.
File: python.info, Node: What Now?, Next: Interactive Input Editing and History Substitution, Prev: Virtual Environments and Packages, Up: The Python Tutorial
2.13 What Now?
==============
Reading this tutorial has probably reinforced your interest in using
Python — you should be eager to apply Python to solving your real-world
problems. Where should you go to learn more?
This tutorial is part of Python’s documentation set. Some other
documents in the set are:
* *note The Python Standard Library: e8e.:
You should browse through this manual, which gives complete (though
terse) reference material about types, functions, and the modules
in the standard library. The standard Python distribution includes
a `lot' of additional code. There are modules to read Unix
mailboxes, retrieve documents via HTTP, generate random numbers,
parse command-line options, write CGI programs, compress data, and
many other tasks. Skimming through the Library Reference will give
you an idea of what’s available.
* *note Installing Python Modules: 7f5. explains how to install
additional modules written by other Python users.
* *note The Python Language Reference: e8f.: A detailed explanation
of Python’s syntax and semantics. It’s heavy reading, but is
useful as a complete guide to the language itself.
More Python resources:
* ‘https://www.python.org’: The major Python Web site. It contains
code, documentation, and pointers to Python-related pages around
the Web. This Web site is mirrored in various places around the
world, such as Europe, Japan, and Australia; a mirror may be faster
than the main site, depending on your geographical location.
* ‘https://docs.python.org’: Fast access to Python’s documentation.
* ‘https://pypi.org’: The Python Package Index, previously also
nicknamed the Cheese Shop (1), is an index of user-created Python
modules that are available for download. Once you begin releasing
code, you can register it here so that others can find it.
* ‘https://code.activestate.com/recipes/langs/python/’: The Python
Cookbook is a sizable collection of code examples, larger modules,
and useful scripts. Particularly notable contributions are
collected in a book also titled Python Cookbook (O’Reilly &
Associates, ISBN 0-596-00797-3.)
* ‘http://www.pyvideo.org’ collects links to Python-related videos
from conferences and user-group meetings.
* ‘https://scipy.org’: The Scientific Python project includes modules
for fast array computations and manipulations plus a host of
packages for such things as linear algebra, Fourier transforms,
non-linear solvers, random number distributions, statistical
analysis and the like.
For Python-related questions and problem reports, you can post to the
newsgroup ‘comp.lang.python’, or send them to the mailing list at
. The newsgroup and mailing list are gatewayed,
so messages posted to one will automatically be forwarded to the other.
There are hundreds of postings a day, asking (and answering) questions,
suggesting new features, and announcing new modules. Mailing list
archives are available at ‘https://mail.python.org/pipermail/’.
Before posting, be sure to check the list of *note Frequently Asked
Questions: fae. (also called the FAQ). The FAQ answers many of the
questions that come up again and again, and may already contain the
solution for your problem.
---------- Footnotes ----------
(1) (1) “Cheese Shop” is a Monty Python’s sketch: a customer enters a
cheese shop, but whatever cheese he asks for, the clerk says it’s
missing.
File: python.info, Node: Interactive Input Editing and History Substitution, Next: Floating Point Arithmetic Issues and Limitations, Prev: What Now?, Up: The Python Tutorial
2.14 Interactive Input Editing and History Substitution
=======================================================
Some versions of the Python interpreter support editing of the current
input line and history substitution, similar to facilities found in the
Korn shell and the GNU Bash shell. This is implemented using the GNU
Readline(1) library, which supports various styles of editing. This
library has its own documentation which we won’t duplicate here.
* Menu:
* Tab Completion and History Editing::
* Alternatives to the Interactive Interpreter::
---------- Footnotes ----------
(1) https://tiswww.case.edu/php/chet/readline/rltop.html
File: python.info, Node: Tab Completion and History Editing, Next: Alternatives to the Interactive Interpreter, Up: Interactive Input Editing and History Substitution
2.14.1 Tab Completion and History Editing
-----------------------------------------
Completion of variable and module names is *note automatically enabled:
8e6. at interpreter startup so that the ‘Tab’ key invokes the completion
function; it looks at Python statement names, the current local
variables, and the available module names. For dotted expressions such
as ‘string.a’, it will evaluate the expression up to the final ‘'.'’ and
then suggest completions from the attributes of the resulting object.
Note that this may execute application-defined code if an object with a
*note __getattr__(): 31a. method is part of the expression. The default
configuration also saves your history into a file named
‘.python_history’ in your user directory. The history will be available
again during the next interactive interpreter session.
File: python.info, Node: Alternatives to the Interactive Interpreter, Prev: Tab Completion and History Editing, Up: Interactive Input Editing and History Substitution
2.14.2 Alternatives to the Interactive Interpreter
--------------------------------------------------
This facility is an enormous step forward compared to earlier versions
of the interpreter; however, some wishes are left: It would be nice if
the proper indentation were suggested on continuation lines (the parser
knows if an indent token is required next). The completion mechanism
might use the interpreter’s symbol table. A command to check (or even
suggest) matching parentheses, quotes, etc., would also be useful.
One alternative enhanced interactive interpreter that has been around
for quite some time is IPython(1), which features tab completion, object
exploration and advanced history management. It can also be thoroughly
customized and embedded into other applications. Another similar
enhanced interactive environment is bpython(2).
---------- Footnotes ----------
(1) https://ipython.org/
(2) https://www.bpython-interpreter.org/
File: python.info, Node: Floating Point Arithmetic Issues and Limitations, Next: Appendix, Prev: Interactive Input Editing and History Substitution, Up: The Python Tutorial
2.15 Floating Point Arithmetic: Issues and Limitations
======================================================
Floating-point numbers are represented in computer hardware as base 2
(binary) fractions. For example, the decimal fraction
0.125
has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction
0.001
has value 0/2 + 0/4 + 1/8. These two fractions have identical values,
the only real difference being that the first is written in base 10
fractional notation, and the second in base 2.
Unfortunately, most decimal fractions cannot be represented exactly as
binary fractions. A consequence is that, in general, the decimal
floating-point numbers you enter are only approximated by the binary
floating-point numbers actually stored in the machine.
The problem is easier to understand at first in base 10. Consider the
fraction 1/3. You can approximate that as a base 10 fraction:
0.3
or, better,
0.33
or, better,
0.333
and so on. No matter how many digits you’re willing to write down, the
result will never be exactly 1/3, but will be an increasingly better
approximation of 1/3.
In the same way, no matter how many base 2 digits you’re willing to use,
the decimal value 0.1 cannot be represented exactly as a base 2
fraction. In base 2, 1/10 is the infinitely repeating fraction
0.0001100110011001100110011001100110011001100110011...
Stop at any finite number of bits, and you get an approximation. On
most machines today, floats are approximated using a binary fraction
with the numerator using the first 53 bits starting with the most
significant bit and with the denominator as a power of two. In the case
of 1/10, the binary fraction is ‘3602879701896397 / 2 ** 55’ which is
close to but not exactly equal to the true value of 1/10.
Many users are not aware of the approximation because of the way values
are displayed. Python only prints a decimal approximation to the true
decimal value of the binary approximation stored by the machine. On
most machines, if Python were to print the true decimal value of the
binary approximation stored for 0.1, it would have to display
>>> 0.1
0.1000000000000000055511151231257827021181583404541015625
That is more digits than most people find useful, so Python keeps the
number of digits manageable by displaying a rounded value instead
>>> 1 / 10
0.1
Just remember, even though the printed result looks like the exact value
of 1/10, the actual stored value is the nearest representable binary
fraction.
Interestingly, there are many different decimal numbers that share the
same nearest approximate binary fraction. For example, the numbers
‘0.1’ and ‘0.10000000000000001’ and
‘0.1000000000000000055511151231257827021181583404541015625’ are all
approximated by ‘3602879701896397 / 2 ** 55’. Since all of these
decimal values share the same approximation, any one of them could be
displayed while still preserving the invariant ‘eval(repr(x)) == x’.
Historically, the Python prompt and built-in *note repr(): 7cc. function
would choose the one with 17 significant digits, ‘0.10000000000000001’.
Starting with Python 3.1, Python (on most systems) is now able to choose
the shortest of these and simply display ‘0.1’.
Note that this is in the very nature of binary floating-point: this is
not a bug in Python, and it is not a bug in your code either. You’ll
see the same kind of thing in all languages that support your hardware’s
floating-point arithmetic (although some languages may not `display' the
difference by default, or in all output modes).
For more pleasant output, you may wish to use string formatting to
produce a limited number of significant digits:
>>> format(math.pi, '.12g') # give 12 significant digits
'3.14159265359'
>>> format(math.pi, '.2f') # give 2 digits after the point
'3.14'
>>> repr(math.pi)
'3.141592653589793'
It’s important to realize that this is, in a real sense, an illusion:
you’re simply rounding the `display' of the true machine value.
One illusion may beget another. For example, since 0.1 is not exactly
1/10, summing three values of 0.1 may not yield exactly 0.3, either:
>>> .1 + .1 + .1 == .3
False
Also, since the 0.1 cannot get any closer to the exact value of 1/10 and
0.3 cannot get any closer to the exact value of 3/10, then pre-rounding
with *note round(): c6d. function cannot help:
>>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)
False
Though the numbers cannot be made closer to their intended exact values,
the *note round(): c6d. function can be useful for post-rounding so that
results with inexact values become comparable to one another:
>>> round(.1 + .1 + .1, 10) == round(.3, 10)
True
Binary floating-point arithmetic holds many surprises like this. The
problem with “0.1” is explained in precise detail below, in the
“Representation Error” section. See The Perils of Floating Point(1) for
a more complete account of other common surprises.
As that says near the end, “there are no easy answers.” Still, don’t be
unduly wary of floating-point! The errors in Python float operations
are inherited from the floating-point hardware, and on most machines are
on the order of no more than 1 part in 2**53 per operation. That’s more
than adequate for most tasks, but you do need to keep in mind that it’s
not decimal arithmetic and that every float operation can suffer a new
rounding error.
While pathological cases do exist, for most casual use of floating-point
arithmetic you’ll see the result you expect in the end if you simply
round the display of your final results to the number of decimal digits
you expect. *note str(): 330. usually suffices, and for finer control
see the *note str.format(): 4da. method’s format specifiers in *note
Format String Syntax: d1a.
For use cases which require exact decimal representation, try using the
*note decimal: 36. module which implements decimal arithmetic suitable
for accounting applications and high-precision applications.
Another form of exact arithmetic is supported by the *note fractions:
83. module which implements arithmetic based on rational numbers (so the
numbers like 1/3 can be represented exactly).
If you are a heavy user of floating point operations you should take a
look at the Numerical Python package and many other packages for
mathematical and statistical operations supplied by the SciPy project.
See <‘https://scipy.org’>.
Python provides tools that may help on those rare occasions when you
really `do' want to know the exact value of a float. The *note
float.as_integer_ratio(): fb9. method expresses the value of a float as
a fraction:
>>> x = 3.14159
>>> x.as_integer_ratio()
(3537115888337719, 1125899906842624)
Since the ratio is exact, it can be used to losslessly recreate the
original value:
>>> x == 3537115888337719 / 1125899906842624
True
The *note float.hex(): fba. method expresses a float in hexadecimal
(base 16), again giving the exact value stored by your computer:
>>> x.hex()
'0x1.921f9f01b866ep+1'
This precise hexadecimal representation can be used to reconstruct the
float value exactly:
>>> x == float.fromhex('0x1.921f9f01b866ep+1')
True
Since the representation is exact, it is useful for reliably porting
values across different versions of Python (platform independence) and
exchanging data with other languages that support the same format (such
as Java and C99).
Another helpful tool is the *note math.fsum(): d3e. function which helps
mitigate loss-of-precision during summation. It tracks “lost digits” as
values are added onto a running total. That can make a difference in
overall accuracy so that the errors do not accumulate to the point where
they affect the final total:
>>> sum([0.1] * 10) == 1.0
False
>>> math.fsum([0.1] * 10) == 1.0
True
* Menu:
* Representation Error::
---------- Footnotes ----------
(1) http://www.lahey.com/float.htm
File: python.info, Node: Representation Error, Up: Floating Point Arithmetic Issues and Limitations
2.15.1 Representation Error
---------------------------
This section explains the “0.1” example in detail, and shows how you can
perform an exact analysis of cases like this yourself. Basic
familiarity with binary floating-point representation is assumed.
`Representation error' refers to the fact that some (most, actually)
decimal fractions cannot be represented exactly as binary (base 2)
fractions. This is the chief reason why Python (or Perl, C, C++, Java,
Fortran, and many others) often won’t display the exact decimal number
you expect.
Why is that? 1/10 is not exactly representable as a binary fraction.
Almost all machines today (November 2000) use IEEE-754 floating point
arithmetic, and almost all platforms map Python floats to IEEE-754
“double precision”. 754 doubles contain 53 bits of precision, so on
input the computer strives to convert 0.1 to the closest fraction it can
of the form `J'/2**`N' where `J' is an integer containing exactly 53
bits. Rewriting
1 / 10 ~= J / (2**N)
as
J ~= 2**N / 10
and recalling that `J' has exactly 53 bits (is ‘>= 2**52’ but ‘<
2**53’), the best value for `N' is 56:
>>> 2**52 <= 2**56 // 10 < 2**53
True
That is, 56 is the only value for `N' that leaves `J' with exactly 53
bits. The best possible value for `J' is then that quotient rounded:
>>> q, r = divmod(2**56, 10)
>>> r
6
Since the remainder is more than half of 10, the best approximation is
obtained by rounding up:
>>> q+1
7205759403792794
Therefore the best possible approximation to 1/10 in 754 double
precision is:
7205759403792794 / 2 ** 56
Dividing both the numerator and denominator by two reduces the fraction
to:
3602879701896397 / 2 ** 55
Note that since we rounded up, this is actually a little bit larger than
1/10; if we had not rounded up, the quotient would have been a little
bit smaller than 1/10. But in no case can it be `exactly' 1/10!
So the computer never “sees” 1/10: what it sees is the exact fraction
given above, the best 754 double approximation it can get:
>>> 0.1 * 2 ** 55
3602879701896397.0
If we multiply that fraction by 10**55, we can see the value out to 55
decimal digits:
>>> 3602879701896397 * 10 ** 55 // 2 ** 55
1000000000000000055511151231257827021181583404541015625
meaning that the exact number stored in the computer is equal to the
decimal value 0.1000000000000000055511151231257827021181583404541015625.
Instead of displaying the full decimal value, many languages (including
older versions of Python), round the result to 17 significant digits:
>>> format(0.1, '.17f')
'0.10000000000000001'
The *note fractions: 83. and *note decimal: 36. modules make these
calculations easy:
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> Fraction.from_float(0.1)
Fraction(3602879701896397, 36028797018963968)
>>> (0.1).as_integer_ratio()
(3602879701896397, 36028797018963968)
>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> format(Decimal.from_float(0.1), '.17')
'0.10000000000000001'
File: python.info, Node: Appendix, Prev: Floating Point Arithmetic Issues and Limitations, Up: The Python Tutorial
2.16 Appendix
=============
* Menu:
* Interactive Mode: Interactive Mode<2>.
File: python.info, Node: Interactive Mode<2>, Up: Appendix
2.16.1 Interactive Mode
-----------------------
* Menu:
* Error Handling::
* Executable Python Scripts::
* The Interactive Startup File::
* The Customization Modules::
File: python.info, Node: Error Handling, Next: Executable Python Scripts, Up: Interactive Mode<2>
2.16.1.1 Error Handling
.......................
When an error occurs, the interpreter prints an error message and a
stack trace. In interactive mode, it then returns to the primary
prompt; when input came from a file, it exits with a nonzero exit status
after printing the stack trace. (Exceptions handled by an *note except:
b3e. clause in a *note try: d72. statement are not errors in this
context.) Some errors are unconditionally fatal and cause an exit with
a nonzero exit; this applies to internal inconsistencies and some cases
of running out of memory. All error messages are written to the
standard error stream; normal output from executed commands is written
to standard output.
Typing the interrupt character (usually ‘Control-C’ or ‘Delete’) to the
primary or secondary prompt cancels the input and returns to the primary
prompt. (1) Typing an interrupt while a command is executing raises the
*note KeyboardInterrupt: 197. exception, which may be handled by a *note
try: d72. statement.
---------- Footnotes ----------
(1) (1) A problem with the GNU Readline package may prevent this.
File: python.info, Node: Executable Python Scripts, Next: The Interactive Startup File, Prev: Error Handling, Up: Interactive Mode<2>
2.16.1.2 Executable Python Scripts
..................................
On BSD’ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line
#!/usr/bin/env python3.5
(assuming that the interpreter is on the user’s ‘PATH’) at the beginning
of the script and giving the file an executable mode. The ‘#!’ must be
the first two characters of the file. On some platforms, this first
line must end with a Unix-style line ending (‘'\n'’), not a Windows
(‘'\r\n'’) line ending. Note that the hash, or pound, character, ‘'#'’,
is used to start a comment in Python.
The script can be given an executable mode, or permission, using the
‘chmod’ command.
$ chmod +x myscript.py
On Windows systems, there is no notion of an “executable mode”. The
Python installer automatically associates ‘.py’ files with ‘python.exe’
so that a double-click on a Python file will run it as a script. The
extension can also be ‘.pyw’, in that case, the console window that
normally appears is suppressed.
File: python.info, Node: The Interactive Startup File, Next: The Customization Modules, Prev: Executable Python Scripts, Up: Interactive Mode<2>
2.16.1.3 The Interactive Startup File
.....................................
When you use Python interactively, it is frequently handy to have some
standard commands executed every time the interpreter is started. You
can do this by setting an environment variable named *note
PYTHONSTARTUP: 8e5. to the name of a file containing your start-up
commands. This is similar to the ‘.profile’ feature of the Unix shells.
This file is only read in interactive sessions, not when Python reads
commands from a script, and not when ‘/dev/tty’ is given as the explicit
source of commands (which otherwise behaves like an interactive
session). It is executed in the same namespace where interactive
commands are executed, so that objects that it defines or imports can be
used without qualification in the interactive session. You can also
change the prompts ‘sys.ps1’ and ‘sys.ps2’ in this file.
If you want to read an additional start-up file from the current
directory, you can program this in the global start-up file using code
like ‘if os.path.isfile('.pythonrc.py'):
exec(open('.pythonrc.py').read())’. If you want to use the startup file
in a script, you must do this explicitly in the script:
import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
with open(filename) as fobj:
startup_file = fobj.read()
exec(startup_file)
File: python.info, Node: The Customization Modules, Prev: The Interactive Startup File, Up: Interactive Mode<2>
2.16.1.4 The Customization Modules
..................................
Python provides two hooks to let you customize it: ‘sitecustomize’ and
‘usercustomize’. To see how it works, you need first to find the
location of your user site-packages directory. Start Python and run
this code:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.5/site-packages'
Now you can create a file named ‘usercustomize.py’ in that directory and
put anything you want in it. It will affect every invocation of Python,
unless it is started with the *note -s: d15. option to disable the
automatic import.
‘sitecustomize’ works in the same way, but is typically created by an
administrator of the computer in the global site-packages directory, and
is imported before ‘usercustomize’. See the documentation of the *note
site: eb. module for more details.
File: python.info, Node: Python Setup and Usage, Next: The Python Language Reference, Prev: The Python Tutorial, Up: Top
3 Python Setup and Usage
************************
This part of the documentation is devoted to general information on the
setup of the Python environment on different platforms, the invocation
of the interpreter and things that make working with Python easier.
* Menu:
* Command line and environment::
* Using Python on Unix platforms::
* Using Python on Windows::
* Using Python on a Macintosh::
* Editors and IDEs::
File: python.info, Node: Command line and environment, Next: Using Python on Unix platforms, Up: Python Setup and Usage
3.1 Command line and environment
================================
The CPython interpreter scans the command line and the environment for
various settings.
`CPython implementation detail:' Other implementations’ command line
schemes may differ. See *note Alternate Implementations: fcd. for
further resources.
* Menu:
* Command line::
* Environment variables::
File: python.info, Node: Command line, Next: Environment variables, Up: Command line and environment
3.1.1 Command line
------------------
When invoking Python, you may specify any of these options:
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
The most common use case is, of course, a simple invocation of a script:
python myscript.py
* Menu:
* Interface options::
* Generic options::
* Miscellaneous options::
* Options you shouldn’t use::
File: python.info, Node: Interface options, Next: Generic options, Up: Command line
3.1.1.1 Interface options
.........................
The interpreter interface resembles that of the UNIX shell, but provides
some additional methods of invocation:
* When called with standard input connected to a tty device, it
prompts for commands and executes them until an EOF (an end-of-file
character, you can produce that with ‘Ctrl-D’ on UNIX or ‘Ctrl-Z,
Enter’ on Windows) is read.
* When called with a file name argument or with a file as standard
input, it reads and executes a script from that file.
* When called with a directory name argument, it reads and executes
an appropriately named script from that directory.
* When called with ‘-c command’, it executes the Python statement(s)
given as `command'. Here `command' may contain multiple statements
separated by newlines. Leading whitespace is significant in Python
statements!
* When called with ‘-m module-name’, the given module is located on
the Python module path and executed as a script.
In non-interactive mode, the entire input is parsed before it is
executed.
An interface option terminates the list of options consumed by the
interpreter, all consecutive arguments will end up in *note sys.argv:
bfb. – note that the first element, subscript zero (‘sys.argv[0]’), is a
string reflecting the program’s source.
-- Program Option: -c
Execute the Python code in `command'. `command' can be one or more
statements separated by newlines, with significant leading
whitespace as in normal module code.
If this option is given, the first element of *note sys.argv: bfb.
will be ‘"-c"’ and the current directory will be added to the start
of *note sys.path: 488. (allowing modules in that directory to be
imported as top level modules).
Raises an *note auditing event: fd1. ‘cpython.run_command’ with
argument ‘command’.
-- Program Option: -m
Search *note sys.path: 488. for the named module and execute its
contents as the *note __main__: 1. module.
Since the argument is a `module' name, you must not give a file
extension (‘.py’). The module name should be a valid absolute
Python module name, but the implementation may not always enforce
this (e.g. it may allow you to use a name that includes a hyphen).
Package names (including namespace packages) are also permitted.
When a package name is supplied instead of a normal module, the
interpreter will execute ‘.__main__’ as the main module. This
behaviour is deliberately similar to the handling of directories
and zipfiles that are passed to the interpreter as the script
argument.
Note: This option cannot be used with built-in modules and
extension modules written in C, since they do not have Python
module files. However, it can still be used for precompiled
modules, even if the original source file is not available.
If this option is given, the first element of *note sys.argv: bfb.
will be the full path to the module file (while the module file is
being located, the first element will be set to ‘"-m"’). As with
the *note -c: e9e. option, the current directory will be added to
the start of *note sys.path: 488.
*note -I: fd2. option can be used to run the script in isolated
mode where *note sys.path: 488. contains neither the current
directory nor the user’s site-packages directory. All ‘PYTHON*’
environment variables are ignored, too.
Many standard library modules contain code that is invoked on their
execution as a script. An example is the *note timeit: 10b.
module:
python -m timeit -s 'setup here' 'benchmarked code here'
python -m timeit -h # for details
Raises an *note auditing event: fd1. ‘cpython.run_module’ with
argument ‘module-name’.
See also
........
*note runpy.run_module(): fd3.
Equivalent functionality directly available to Python code
PEP 338(1) – Executing modules as scripts
Changed in version 3.1: Supply the package name to run a ‘__main__’
submodule.
Changed in version 3.4: namespace packages are also supported
-- Describe: -
Read commands from standard input (*note sys.stdin: 30d.). If
standard input is a terminal, *note -i: e1f. is implied.
If this option is given, the first element of *note sys.argv: bfb.
will be ‘"-"’ and the current directory will be added to the start
of *note sys.path: 488.
Raises an *note auditing event: fd1. ‘cpython.run_stdin’ with no
arguments.
-- Describe: ’ and
‘’).
-- Method: HTMLParser.handle_entityref (name)
This method is called to process a named character reference of the
form ‘&name;’ (e.g. ‘>’), where `name' is a general entity
reference (e.g. ‘'gt'’). This method is never called if
`convert_charrefs' is ‘True’.
-- Method: HTMLParser.handle_charref (name)
This method is called to process decimal and hexadecimal numeric
character references of the form ‘NNN;’ and ‘NNN;’. For
example, the decimal equivalent for ‘>’ is ‘>’, whereas the
hexadecimal is ‘>’; in this case the method will receive
‘'62'’ or ‘'x3E'’. This method is never called if
`convert_charrefs' is ‘True’.
-- Method: HTMLParser.handle_comment (data)
This method is called when a comment is encountered (e.g.
‘’).
For example, the comment ‘’ will cause this method
to be called with the argument ‘' comment '’.
The content of Internet Explorer conditional comments (condcoms)
will also be sent to this method, so, for ‘’, this method will receive
‘'[if IE 9]>IE9-specific content’).
The `decl' parameter will be the entire contents of the declaration
inside the ‘’ markup (e.g. ‘'DOCTYPE html'’).
-- Method: HTMLParser.handle_pi (data)
Method called when a processing instruction is encountered. The
`data' parameter will contain the entire processing instruction.
For example, for the processing instruction ‘’,
this method would be called as ‘handle_pi("proc color='red'")’. It
is intended to be overridden by a derived class; the base class
implementation does nothing.
Note: The *note HTMLParser: 7bf. class uses the SGML syntactic
rules for processing instructions. An XHTML processing
instruction using the trailing ‘'?'’ will cause the ‘'?'’ to
be included in `data'.
-- Method: HTMLParser.unknown_decl (data)
This method is called when an unrecognized declaration is read by
the parser.
The `data' parameter will be the entire contents of the declaration
inside the ‘’ markup. It is sometimes useful to be
overridden by a derived class. The base class implementation does
nothing.
File: python.info, Node: Examples<19>, Prev: HTMLParser Methods, Up: html parser — Simple HTML and XHTML parser
5.20.2.3 Examples
.................
The following class implements a parser that will be used to illustrate
more examples:
from html.parser import HTMLParser
from html.entities import name2codepoint
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print("Start tag:", tag)
for attr in attrs:
print(" attr:", attr)
def handle_endtag(self, tag):
print("End tag :", tag)
def handle_data(self, data):
print("Data :", data)
def handle_comment(self, data):
print("Comment :", data)
def handle_entityref(self, name):
c = chr(name2codepoint[name])
print("Named ent:", c)
def handle_charref(self, name):
if name.startswith('x'):
c = chr(int(name[1:], 16))
else:
c = chr(int(name))
print("Num ent :", c)
def handle_decl(self, data):
print("Decl :", data)
parser = MyHTMLParser()
Parsing a doctype:
>>> parser.feed('')
Decl : DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"
Parsing an element with a few attributes and a title:
>>> parser.feed(' ')
Start tag: img
attr: ('src', 'python-logo.png')
attr: ('alt', 'The Python logo')
>>>
>>> parser.feed('Python ')
Start tag: h1
Data : Python
End tag : h1
The content of ‘script’ and ‘style’ elements is returned as is, without
further parsing:
>>> parser.feed('')
Start tag: style
attr: ('type', 'text/css')
Data : #python { color: green }
End tag : style
>>> parser.feed('')
Start tag: script
attr: ('type', 'text/javascript')
Data : alert("hello! ");
End tag : script
Parsing comments:
>>> parser.feed(''
... '')
Comment : a comment
Comment : [if IE 9]>IE-specific content'’):
>>> parser.feed('>>>')
Named ent: >
Num ent : >
Num ent : >
Feeding incomplete chunks to *note feed(): 26dc. works, but *note
handle_data(): 26e4. might be called more than once (unless
`convert_charrefs' is set to ‘True’):
>>> for chunk in ['buff', 'ered ', 'text']:
... parser.feed(chunk)
...
Start tag: span
Data : buff
Data : ered
Data : text
End tag : span
Parsing invalid HTML (e.g. unquoted attributes) also works:
>>> parser.feed('tag soup
')
Start tag: p
Start tag: a
attr: ('class', 'link')
attr: ('href', '#main')
Data : tag soup
End tag : p
End tag : a
File: python.info, Node: html entities — Definitions of HTML general entities, Next: XML Processing Modules, Prev: html parser — Simple HTML and XHTML parser, Up: Structured Markup Processing Tools
5.20.3 ‘html.entities’ — Definitions of HTML general entities
-------------------------------------------------------------
`Source code:' Lib/html/entities.py(1)
__________________________________________________________________
This module defines four dictionaries, *note html5: a15, *note
name2codepoint: 26ef, *note codepoint2name: 26f0, and *note entitydefs:
26f1.
-- Data: html.entities.html5
A dictionary that maps HTML5 named character references (2) to the
equivalent Unicode character(s), e.g. ‘html5['gt;'] == '>'’. Note
that the trailing semicolon is included in the name (e.g.
‘'gt;'’), however some of the names are accepted by the standard
even without the semicolon: in this case the name is present with
and without the ‘';'’. See also *note html.unescape(): 85a.
New in version 3.3.
-- Data: html.entities.entitydefs
A dictionary mapping XHTML 1.0 entity definitions to their
replacement text in ISO Latin-1.
-- Data: html.entities.name2codepoint
A dictionary that maps HTML entity names to the Unicode code
points.
-- Data: html.entities.codepoint2name
A dictionary that maps Unicode code points to HTML entity names.
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/html/entities.py
(2) (1) See
‘https://www.w3.org/TR/html5/syntax.html#named-character-references’
File: python.info, Node: XML Processing Modules, Next: xml etree ElementTree — The ElementTree XML API, Prev: html entities — Definitions of HTML general entities, Up: Structured Markup Processing Tools
5.20.4 XML Processing Modules
-----------------------------
`Source code:' Lib/xml/(1)
__________________________________________________________________
Python’s interfaces for processing XML are grouped in the ‘xml’ package.
Warning: The XML modules are not secure against erroneous or
maliciously constructed data. If you need to parse untrusted or
unauthenticated data see the *note XML vulnerabilities: 26f5. and
*note The defusedxml Package: 26f6. sections.
It is important to note that modules in the *note xml: 133. package
require that there be at least one SAX-compliant XML parser available.
The Expat parser is included with Python, so the *note
xml.parsers.expat: 138. module will always be available.
The documentation for the *note xml.dom: 134. and *note xml.sax: 13b.
packages are the definition of the Python bindings for the DOM and SAX
interfaces.
The XML handling submodules are:
* *note xml.etree.ElementTree: 137.: the ElementTree API, a simple
and lightweight XML processor
* *note xml.dom: 134.: the DOM API definition
* *note xml.dom.minidom: 135.: a minimal DOM implementation
* *note xml.dom.pulldom: 136.: support for building partial DOM trees
* *note xml.sax: 13b.: SAX2 base classes and convenience functions
* *note xml.parsers.expat: 138.: the Expat parser binding
* Menu:
* XML vulnerabilities::
* The defusedxml Package::
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/xml/
File: python.info, Node: XML vulnerabilities, Next: The defusedxml Package, Up: XML Processing Modules
5.20.4.1 XML vulnerabilities
............................
The XML processing modules are not secure against maliciously
constructed data. An attacker can abuse XML features to carry out
denial of service attacks, access local files, generate network
connections to other machines, or circumvent firewalls.
The following table gives an overview of the known attacks and whether
the various modules are vulnerable to them.
kind sax etree minidom pulldom xmlrpc
------------------------------------------------------------------------------------------------------------------------------
billion laughs `Vulnerable' `Vulnerable' `Vulnerable' `Vulnerable' `Vulnerable'
quadratic blowup `Vulnerable' `Vulnerable' `Vulnerable' `Vulnerable' `Vulnerable'
external entity expansion Safe (4) Safe (1) Safe (2) Safe (4) Safe (3)
DTD(1) retrieval Safe (4) Safe Safe Safe (4) Safe
decompression bomb Safe Safe Safe Safe `Vulnerable'
1. *note xml.etree.ElementTree: 137. doesn’t expand external entities
and raises a ‘ParserError’ when an entity occurs.
2. *note xml.dom.minidom: 135. doesn’t expand external entities and
simply returns the unexpanded entity verbatim.
3. ‘xmlrpclib’ doesn’t expand external entities and omits them.
4. Since Python 3.7.1, external general entities are no longer
processed by default.
billion laughs / exponential entity expansion
The Billion Laughs(2) attack – also known as exponential entity
expansion – uses multiple levels of nested entities. Each entity
refers to another entity several times, and the final entity
definition contains a small string. The exponential expansion
results in several gigabytes of text and consumes lots of memory
and CPU time.
quadratic blowup entity expansion
A quadratic blowup attack is similar to a Billion Laughs(3) attack;
it abuses entity expansion, too. Instead of nested entities it
repeats one large entity with a couple of thousand chars over and
over again. The attack isn’t as efficient as the exponential case
but it avoids triggering parser countermeasures that forbid
deeply-nested entities.
external entity expansion
Entity declarations can contain more than just text for
replacement. They can also point to external resources or local
files. The XML parser accesses the resource and embeds the content
into the XML document.
DTD(4) retrieval
Some XML libraries like Python’s *note xml.dom.pulldom: 136.
retrieve document type definitions from remote or local locations.
The feature has similar implications as the external entity
expansion issue.
decompression bomb
Decompression bombs (aka ZIP bomb(5)) apply to all XML libraries
that can parse compressed XML streams such as gzipped HTTP streams
or LZMA-compressed files. For an attacker it can reduce the amount
of transmitted data by three magnitudes or more.
The documentation for defusedxml(6) on PyPI has further information
about all known attack vectors with examples and references.
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/Document_type_definition
(2) https://en.wikipedia.org/wiki/Billion_laughs
(3) https://en.wikipedia.org/wiki/Billion_laughs
(4) https://en.wikipedia.org/wiki/Document_type_definition
(5) https://en.wikipedia.org/wiki/Zip_bomb
(6) https://pypi.org/project/defusedxml/
File: python.info, Node: The defusedxml Package, Prev: XML vulnerabilities, Up: XML Processing Modules
5.20.4.2 The ‘defusedxml’ Package
.................................
defusedxml(1) is a pure Python package with modified subclasses of all
stdlib XML parsers that prevent any potentially malicious operation.
Use of this package is recommended for any server code that parses
untrusted XML data. The package also ships with example exploits and
extended documentation on more XML exploits such as XPath injection.
---------- Footnotes ----------
(1) https://pypi.org/project/defusedxml/
File: python.info, Node: xml etree ElementTree — The ElementTree XML API, Next: xml dom — The Document Object Model API, Prev: XML Processing Modules, Up: Structured Markup Processing Tools
5.20.5 ‘xml.etree.ElementTree’ — The ElementTree XML API
--------------------------------------------------------
`Source code:' Lib/xml/etree/ElementTree.py(1)
__________________________________________________________________
The *note xml.etree.ElementTree: 137. module implements a simple and
efficient API for parsing and creating XML data.
Changed in version 3.3: This module will use a fast implementation
whenever available. The ‘xml.etree.cElementTree’ module is deprecated.
Warning: The *note xml.etree.ElementTree: 137. module is not secure
against maliciously constructed data. If you need to parse
untrusted or unauthenticated data see *note XML vulnerabilities:
26f5.
* Menu:
* Tutorial::
* XPath support::
* Reference: Reference<2>.
* XInclude support::
* Reference: Reference<3>.
---------- Footnotes ----------
(1)
https://github.com/python/cpython/tree/3.8/Lib/xml/etree/ElementTree.py
File: python.info, Node: Tutorial, Next: XPath support, Up: xml etree ElementTree — The ElementTree XML API
5.20.5.1 Tutorial
.................
This is a short tutorial for using *note xml.etree.ElementTree: 137.
(‘ET’ in short). The goal is to demonstrate some of the building blocks
and basic concepts of the module.
* Menu:
* XML tree and elements::
* Parsing XML::
* Pull API for non-blocking parsing::
* Finding interesting elements::
* Modifying an XML File::
* Building XML documents::
* Parsing XML with Namespaces::
* Additional resources::
File: python.info, Node: XML tree and elements, Next: Parsing XML, Up: Tutorial
5.20.5.2 XML tree and elements
..............................
XML is an inherently hierarchical data format, and the most natural way
to represent it is with a tree. ‘ET’ has two classes for this purpose -
*note ElementTree: 917. represents the whole XML document as a tree, and
*note Element: ac4. represents a single node in this tree. Interactions
with the whole document (reading and writing to/from files) are usually
done on the *note ElementTree: 917. level. Interactions with a single
XML element and its sub-elements are done on the *note Element: ac4.
level.
File: python.info, Node: Parsing XML, Next: Pull API for non-blocking parsing, Prev: XML tree and elements, Up: Tutorial
5.20.5.3 Parsing XML
....................
We’ll be using the following XML document as the sample data for this
section:
1
2008
141100
4
2011
59900
68
2011
13600
We can import this data by reading from a file:
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
Or directly from a string:
root = ET.fromstring(country_data_as_string)
*note fromstring(): 2700. parses XML from a string directly into an
*note Element: ac4, which is the root element of the parsed tree. Other
parsing functions may create an *note ElementTree: 917. Check the
documentation to be sure.
As an *note Element: ac4, ‘root’ has a tag and a dictionary of
attributes:
>>> root.tag
'data'
>>> root.attrib
{}
It also has children nodes over which we can iterate:
>>> for child in root:
... print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
Children are nested, and we can access specific child nodes by index:
>>> root[0][1].text
'2008'
Note: Not all elements of the XML input will end up as elements of
the parsed tree. Currently, this module skips over any XML
comments, processing instructions, and document type declarations
in the input. Nevertheless, trees built using this module’s API
rather than parsing from XML text can have comments and processing
instructions in them; they will be included when generating XML
output. A document type declaration may be accessed by passing a
custom *note TreeBuilder: 253. instance to the *note XMLParser:
252. constructor.
File: python.info, Node: Pull API for non-blocking parsing, Next: Finding interesting elements, Prev: Parsing XML, Up: Tutorial
5.20.5.4 Pull API for non-blocking parsing
..........................................
Most parsing functions provided by this module require the whole
document to be read at once before returning any result. It is possible
to use an *note XMLParser: 252. and feed data into it incrementally, but
it is a push API that calls methods on a callback target, which is too
low-level and inconvenient for most needs. Sometimes what the user
really wants is to be able to parse XML incrementally, without blocking
operations, while enjoying the convenience of fully constructed *note
Element: ac4. objects.
The most powerful tool for doing this is *note XMLPullParser: 913. It
does not require a blocking read to obtain the XML data, and is instead
fed with data incrementally with *note XMLPullParser.feed(): 2702.
calls. To get the parsed XML elements, call *note
XMLPullParser.read_events(): 2703. Here is an example:
>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('sometext')
>>> list(parser.read_events())
[('start', )]
>>> parser.feed(' more text ')
>>> for event, elem in parser.read_events():
... print(event)
... print(elem.tag, 'text=', elem.text)
...
end
The obvious use case is applications that operate in a non-blocking
fashion where the XML data is being received from a socket or read
incrementally from some storage device. In such cases, blocking reads
are unacceptable.
Because it’s so flexible, *note XMLPullParser: 913. can be inconvenient
to use for simpler use-cases. If you don’t mind your application
blocking on reading XML data but would still like to have incremental
parsing capabilities, take a look at *note iterparse(): 948. It can be
useful when you’re reading a large XML document and don’t want to hold
it wholly in memory.
File: python.info, Node: Finding interesting elements, Next: Modifying an XML File, Prev: Pull API for non-blocking parsing, Up: Tutorial
5.20.5.5 Finding interesting elements
.....................................
*note Element: ac4. has some useful methods that help iterate
recursively over all the sub-tree below it (its children, their
children, and so on). For example, *note Element.iter(): ce7.:
>>> for neighbor in root.iter('neighbor'):
... print(neighbor.attrib)
...
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}
*note Element.findall(): 2705. finds only elements with a tag which are
direct children of the current element. *note Element.find(): 2706.
finds the `first' child with a particular tag, and *note Element.text:
2707. accesses the element’s text content. *note Element.get(): 2708.
accesses the element’s attributes:
>>> for country in root.findall('country'):
... rank = country.find('rank').text
... name = country.get('name')
... print(name, rank)
...
Liechtenstein 1
Singapore 4
Panama 68
More sophisticated specification of which elements to look for is
possible by using *note XPath: 41a.
File: python.info, Node: Modifying an XML File, Next: Building XML documents, Prev: Finding interesting elements, Up: Tutorial
5.20.5.6 Modifying an XML File
..............................
*note ElementTree: 917. provides a simple way to build XML documents and
write them to files. The *note ElementTree.write(): 918. method serves
this purpose.
Once created, an *note Element: ac4. object may be manipulated by
directly changing its fields (such as *note Element.text: 2707.), adding
and modifying attributes (*note Element.set(): 270a. method), as well as
adding new children (for example with *note Element.append(): 270b.).
Let’s say we want to add one to each country’s rank, and add an
‘updated’ attribute to the rank element:
>>> for rank in root.iter('rank'):
... new_rank = int(rank.text) + 1
... rank.text = str(new_rank)
... rank.set('updated', 'yes')
...
>>> tree.write('output.xml')
Our XML now looks like this:
2
2008
141100
5
2011
59900
69
2011
13600
We can remove elements using *note Element.remove(): 270c. Let’s say we
want to remove all countries with a rank higher than 50:
>>> for country in root.findall('country'):
... # using root.findall() to avoid removal during traversal
... rank = int(country.find('rank').text)
... if rank > 50:
... root.remove(country)
...
>>> tree.write('output.xml')
Note that concurrent modification while iterating can lead to problems,
just like when iterating and modifying Python lists or dicts.
Therefore, the example first collects all matching elements with
‘root.findall()’, and only then iterates over the list of matches.
Our XML now looks like this:
2
2008
141100
5
2011
59900
File: python.info, Node: Building XML documents, Next: Parsing XML with Namespaces, Prev: Modifying an XML File, Up: Tutorial
5.20.5.7 Building XML documents
...............................
The *note SubElement(): 270e. function also provides a convenient way to
create new sub-elements for a given element:
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> c = ET.SubElement(a, 'c')
>>> d = ET.SubElement(c, 'd')
>>> ET.dump(a)
File: python.info, Node: Parsing XML with Namespaces, Next: Additional resources, Prev: Building XML documents, Up: Tutorial
5.20.5.8 Parsing XML with Namespaces
....................................
If the XML input has namespaces(1), tags and attributes with prefixes in
the form ‘prefix:sometag’ get expanded to ‘{uri}sometag’ where the
`prefix' is replaced by the full `URI'. Also, if there is a default
namespace(2), that full URI gets prepended to all of the non-prefixed
tags.
Here is an XML example that incorporates two namespaces, one with the
prefix “fictional” and the other serving as the default namespace:
John Cleese
Lancelot
Archie Leach
Eric Idle
Sir Robin
Gunther
Commander Clement
One way to search and explore this XML example is to manually add the
URI to every tag or attribute in the xpath of a *note find(): 2706. or
*note findall(): 2705.:
root = fromstring(xml_text)
for actor in root.findall('{http://people.example.com}actor'):
name = actor.find('{http://people.example.com}name')
print(name.text)
for char in actor.findall('{http://characters.example.com}character'):
print(' |-->', char.text)
A better way to search the namespaced XML example is to create a
dictionary with your own prefixes and use those in the search functions:
ns = {'real_person': 'http://people.example.com',
'role': 'http://characters.example.com'}
for actor in root.findall('real_person:actor', ns):
name = actor.find('real_person:name', ns)
print(name.text)
for char in actor.findall('role:character', ns):
print(' |-->', char.text)
These two approaches both output:
John Cleese
|--> Lancelot
|--> Archie Leach
Eric Idle
|--> Sir Robin
|--> Gunther
|--> Commander Clement
---------- Footnotes ----------
(1) https://en.wikipedia.org/wiki/XML_namespace
(2) https://www.w3.org/TR/xml-names/#defaulting
File: python.info, Node: Additional resources, Prev: Parsing XML with Namespaces, Up: Tutorial
5.20.5.9 Additional resources
.............................
See ‘http://effbot.org/zone/element-index.htm’ for tutorials and links
to other docs.
File: python.info, Node: XPath support, Next: Reference<2>, Prev: Tutorial, Up: xml etree ElementTree — The ElementTree XML API
5.20.5.10 XPath support
.......................
This module provides limited support for XPath expressions(1) for
locating elements in a tree. The goal is to support a small subset of
the abbreviated syntax; a full XPath engine is outside the scope of the
module.
* Menu:
* Example: Example<10>.
* Supported XPath syntax::
---------- Footnotes ----------
(1) https://www.w3.org/TR/xpath
File: python.info, Node: Example<10>, Next: Supported XPath syntax, Up: XPath support
5.20.5.11 Example
.................
Here’s an example that demonstrates some of the XPath capabilities of
the module. We’ll be using the ‘countrydata’ XML document from the
*note Parsing XML: 26fe. section:
import xml.etree.ElementTree as ET
root = ET.fromstring(countrydata)
# Top-level elements
root.findall(".")
# All 'neighbor' grand-children of 'country' children of the top-level
# elements
root.findall("./country/neighbor")
# Nodes with name='Singapore' that have a 'year' child
root.findall(".//year/..[@name='Singapore']")
# 'year' nodes that are children of nodes with name='Singapore'
root.findall(".//*[@name='Singapore']/year")
# All 'neighbor' nodes that are the second child of their parent
root.findall(".//neighbor[2]")
For XML with namespaces, use the usual qualified ‘{namespace}tag’
notation:
# All dublin-core "title" tags in the document
root.findall(".//{http://purl.org/dc/elements/1.1/}title")
File: python.info, Node: Supported XPath syntax, Prev: Example<10>, Up: XPath support
5.20.5.12 Supported XPath syntax
................................
Syntax Meaning
---------------------------------------------------------------------------------------
‘tag’ Selects all child elements with the given tag. For
example, ‘spam’ selects all child elements named ‘spam’,
and ‘spam/egg’ selects all grandchildren named ‘egg’ in
all children named ‘spam’. ‘{namespace}*’ selects all
tags in the given namespace, ‘{*}spam’ selects tags
named ‘spam’ in any (or no) namespace, and ‘{}*’ only
selects tags that are not in a namespace.
Changed in version 3.8: Support for star-wildcards was
added.
‘*’ Selects all child elements, including comments and
processing instructions. For example, ‘*/egg’ selects
all grandchildren named ‘egg’.
‘.’ Selects the current node. This is mostly useful at the
beginning of the path, to indicate that it’s a relative
path.
‘//’ Selects all subelements, on all levels beneath the
current element. For example, ‘.//egg’ selects all
‘egg’ elements in the entire tree.
‘..’ Selects the parent element. Returns ‘None’ if the path
attempts to reach the ancestors of the start element
(the element ‘find’ was called on).
‘[@attrib]’ Selects all elements that have the given attribute.
‘[@attrib='value']’ Selects all elements for which the given attribute has
the given value. The value cannot contain quotes.
‘[tag]’ Selects all elements that have a child named ‘tag’.
Only immediate children are supported.
‘[.='text']’ Selects all elements whose complete text content,
including descendants, equals the given ‘text’.
New in version 3.7.
‘[tag='text']’ Selects all elements that have a child named ‘tag’ whose
complete text content, including descendants, equals the
given ‘text’.
‘[position]’ Selects all elements that are located at the given
position. The position can be either an integer (1 is
the first position), the expression ‘last()’ (for the
last position), or a position relative to the last
position (e.g. ‘last()-1’).
Predicates (expressions within square brackets) must be preceded by a
tag name, an asterisk, or another predicate. ‘position’ predicates must
be preceded by a tag name.
File: python.info, Node: Reference<2>, Next: XInclude support, Prev: XPath support, Up: xml etree ElementTree — The ElementTree XML API
5.20.5.13 Reference
...................
* Menu:
* Functions: Functions<6>.
File: python.info, Node: Functions<6>, Up: Reference<2>
5.20.5.14 Functions
...................
-- Function: xml.etree.ElementTree.canonicalize (xml_data=None, *,
out=None, from_file=None, **options)
C14N 2.0(1) transformation function.
Canonicalization is a way to normalise XML output in a way that
allows byte-by-byte comparisons and digital signatures. It reduced
the freedom that XML serializers have and instead generates a more
constrained XML representation. The main restrictions regard the
placement of namespace declarations, the ordering of attributes,
and ignorable whitespace.
This function takes an XML data string (`xml_data') or a file path
or file-like object (`from_file') as input, converts it to the
canonical form, and writes it out using the `out' file(-like)
object, if provided, or returns it as a text string if not. The
output file receives text, not bytes. It should therefore be
opened in text mode with ‘utf-8’ encoding.
Typical uses:
xml_data = "... "
print(canonicalize(xml_data))
with open("c14n_output.xml", mode='w', encoding='utf-8') as out_file:
canonicalize(xml_data, out=out_file)
with open("c14n_output.xml", mode='w', encoding='utf-8') as out_file:
canonicalize(from_file="inputfile.xml", out=out_file)
The configuration `options' are as follows:
- `with_comments': set to true to include comments (default:
false)
-
`strip_text': set to true to strip whitespace before and after text content
(default: false)
-
`rewrite_prefixes': set to true to replace namespace prefixes by “n{number}”
(default: false)
-
`qname_aware_tags': a set of qname aware tag names in which prefixes
should be replaced in text content (default: empty)
-
`qname_aware_attrs': a set of qname aware attribute names in which prefixes
should be replaced in text content (default: empty)
- `exclude_attrs': a set of attribute names that should not be
serialised
- `exclude_tags': a set of tag names that should not be
serialised
In the option list above, “a set” refers to any collection or
iterable of strings, no ordering is expected.
New in version 3.8.
-- Function: xml.etree.ElementTree.Comment (text=None)
Comment element factory. This factory function creates a special
element that will be serialized as an XML comment by the standard
serializer. The comment string can be either a bytestring or a
Unicode string. `text' is a string containing the comment string.
Returns an element instance representing a comment.
Note that *note XMLParser: 252. skips over comments in the input
instead of creating comment objects for them. An *note
ElementTree: 917. will only contain comment nodes if they have been
inserted into to the tree using one of the *note Element: ac4.
methods.
-- Function: xml.etree.ElementTree.dump (elem)
Writes an element tree or element structure to sys.stdout. This
function should be used for debugging only.
The exact output format is implementation dependent. In this
version, it’s written as an ordinary XML file.
`elem' is an element tree or an individual element.
Changed in version 3.8: The *note dump(): 2719. function now
preserves the attribute order specified by the user.
-- Function: xml.etree.ElementTree.fromstring (text, parser=None)
Parses an XML section from a string constant. Same as *note XML():
271a. `text' is a string containing XML data. `parser' is an
optional parser instance. If not given, the standard *note
XMLParser: 252. parser is used. Returns an *note Element: ac4.
instance.
-- Function: xml.etree.ElementTree.fromstringlist (sequence,
parser=None)
Parses an XML document from a sequence of string fragments.
`sequence' is a list or other sequence containing XML data
fragments. `parser' is an optional parser instance. If not given,
the standard *note XMLParser: 252. parser is used. Returns an
*note Element: ac4. instance.
New in version 3.2.
-- Function: xml.etree.ElementTree.iselement (element)
Check if an object appears to be a valid element object. `element'
is an element instance. Return ‘True’ if this is an element
object.
-- Function: xml.etree.ElementTree.iterparse (source, events=None,
parser=None)
Parses an XML section into an element tree incrementally, and
reports what’s going on to the user. `source' is a filename or
*note file object: b42. containing XML data. `events' is a
sequence of events to report back. The supported events are the
strings ‘"start"’, ‘"end"’, ‘"comment"’, ‘"pi"’, ‘"start-ns"’ and
‘"end-ns"’ (the “ns” events are used to get detailed namespace
information). If `events' is omitted, only ‘"end"’ events are
reported. `parser' is an optional parser instance. If not given,
the standard *note XMLParser: 252. parser is used. `parser' must
be a subclass of *note XMLParser: 252. and can only use the default
*note TreeBuilder: 253. as a target. Returns an *note iterator:
112e. providing ‘(event, elem)’ pairs.
Note that while *note iterparse(): 948. builds the tree
incrementally, it issues blocking reads on `source' (or the file it
names). As such, it’s unsuitable for applications where blocking
reads can’t be made. For fully non-blocking parsing, see *note
XMLPullParser: 913.
Note: *note iterparse(): 948. only guarantees that it has seen
the “>” character of a starting tag when it emits a “start”
event, so the attributes are defined, but the contents of the
text and tail attributes are undefined at that point. The
same applies to the element children; they may or may not be
present.
If you need a fully populated element, look for “end” events
instead.
Deprecated since version 3.4: The `parser' argument.
Changed in version 3.8: The ‘comment’ and ‘pi’ events were added.
-- Function: xml.etree.ElementTree.parse (source, parser=None)
Parses an XML section into an element tree. `source' is a filename
or file object containing XML data. `parser' is an optional parser
instance. If not given, the standard *note XMLParser: 252. parser
is used. Returns an *note ElementTree: 917. instance.
-- Function: xml.etree.ElementTree.ProcessingInstruction (target,
text=None)
PI element factory. This factory function creates a special
element that will be serialized as an XML processing instruction.
`target' is a string containing the PI target. `text' is a string
containing the PI contents, if given. Returns an element instance,
representing a processing instruction.
Note that *note XMLParser: 252. skips over processing instructions
in the input instead of creating comment objects for them. An
*note ElementTree: 917. will only contain processing instruction
nodes if they have been inserted into to the tree using one of the
*note Element: ac4. methods.
-- Function: xml.etree.ElementTree.register_namespace (prefix, uri)
Registers a namespace prefix. The registry is global, and any
existing mapping for either the given prefix or the namespace URI
will be removed. `prefix' is a namespace prefix. `uri' is a
namespace uri. Tags and attributes in this namespace will be
serialized with the given prefix, if at all possible.
New in version 3.2.
-- Function: xml.etree.ElementTree.SubElement (parent, tag, attrib={},
**extra)
Subelement factory. This function creates an element instance, and
appends it to an existing element.
The element name, attribute names, and attribute values can be
either bytestrings or Unicode strings. `parent' is the parent
element. `tag' is the subelement name. `attrib' is an optional
dictionary, containing element attributes. `extra' contains
additional attributes, given as keyword arguments. Returns an
element instance.
-- Function: xml.etree.ElementTree.tostring (element,
encoding="us-ascii", method="xml", *, xml_declaration=None,
default_namespace=None, short_empty_elements=True)
Generates a string representation of an XML element, including all
subelements. `element' is an *note Element: ac4. instance.
`encoding' (2) is the output encoding (default is US-ASCII). Use
‘encoding="unicode"’ to generate a Unicode string (otherwise, a
bytestring is generated). `method' is either ‘"xml"’, ‘"html"’ or
‘"text"’ (default is ‘"xml"’). `xml_declaration',
`default_namespace' and `short_empty_elements' has the same meaning
as in *note ElementTree.write(): 918. Returns an (optionally)
encoded string containing the XML data.
New in version 3.4: The `short_empty_elements' parameter.
New in version 3.8: The `xml_declaration' and `default_namespace'
parameters.
Changed in version 3.8: The *note tostring(): 915. function now
preserves the attribute order specified by the user.
-- Function: xml.etree.ElementTree.tostringlist (element,
encoding="us-ascii", method="xml", *, xml_declaration=None,
default_namespace=None, short_empty_elements=True)
Generates a string representation of an XML element, including all
subelements. `element' is an *note Element: ac4. instance.
`encoding' (3) is the output encoding (default is US-ASCII). Use
‘encoding="unicode"’ to generate a Unicode string (otherwise, a
bytestring is generated). `method' is either ‘"xml"’, ‘"html"’ or
‘"text"’ (default is ‘"xml"’). `xml_declaration',
`default_namespace' and `short_empty_elements' has the same meaning
as in *note ElementTree.write(): 918. Returns a list of
(optionally) encoded strings containing the XML data. It does not
guarantee any specific sequence, except that
‘b"".join(tostringlist(element)) == tostring(element)’.
New in version 3.2.
New in version 3.4: The `short_empty_elements' parameter.
New in version 3.8: The `xml_declaration' and `default_namespace'
parameters.
Changed in version 3.8: The *note tostringlist(): 916. function now
preserves the attribute order specified by the user.
-- Function: xml.etree.ElementTree.XML (text, parser=None)
Parses an XML section from a string constant. This function can be
used to embed “XML literals” in Python code. `text' is a string
containing XML data. `parser' is an optional parser instance. If
not given, the standard *note XMLParser: 252. parser is used.
Returns an *note Element: ac4. instance.
-- Function: xml.etree.ElementTree.XMLID (text, parser=None)
Parses an XML section from a string constant, and also returns a
dictionary which maps from element id:s to elements. `text' is a
string containing XML data. `parser' is an optional parser
instance. If not given, the standard *note XMLParser: 252. parser
is used. Returns a tuple containing an *note Element: ac4.
instance and a dictionary.
---------- Footnotes ----------
(1) https://www.w3.org/TR/xml-c14n2/
(2) (1) The encoding string included in XML output should conform to
the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
not. See
‘https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl’ and
‘https://www.iana.org/assignments/character-sets/character-sets.xhtml’.
(3) (1) The encoding string included in XML output should conform to
the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
not. See
‘https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl’ and
‘https://www.iana.org/assignments/character-sets/character-sets.xhtml’.
File: python.info, Node: XInclude support, Next: Reference<3>, Prev: Reference<2>, Up: xml etree ElementTree — The ElementTree XML API
5.20.5.15 XInclude support
..........................
This module provides limited support for XInclude directives(1), via the
‘xml.etree.ElementInclude’ helper module. This module can be used to
insert subtrees and text strings into element trees, based on
information in the tree.
* Menu:
* Example: Example<11>.
---------- Footnotes ----------
(1) https://www.w3.org/TR/xinclude/
File: python.info, Node: Example<11>, Up: XInclude support
5.20.5.16 Example
.................
Here’s an example that demonstrates use of the XInclude module. To
include an XML document in the current document, use the
‘{http://www.w3.org/2001/XInclude}include’ element and set the `parse'
attribute to ‘"xml"’, and use the `href' attribute to specify the
document to include.
By default, the `href' attribute is treated as a file name. You can use
custom loaders to override this behaviour. Also note that the standard
helper does not support XPointer syntax.
To process this file, load it as usual, and pass the root element to the
*note xml.etree.ElementTree: 137. module:
from xml.etree import ElementTree, ElementInclude
tree = ElementTree.parse("document.xml")
root = tree.getroot()
ElementInclude.include(root)
The ElementInclude module replaces the
‘{http://www.w3.org/2001/XInclude}include’ element with the root element
from the `source.xml' document. The result might look something like
this:
This is a paragraph.
If the `parse' attribute is omitted, it defaults to “xml”. The href
attribute is required.
To include a text document, use the
‘{http://www.w3.org/2001/XInclude}include’ element, and set the `parse'
attribute to “text”:
Copyright (c) .
The result might look something like:
Copyright (c) 2003.
File: python.info, Node: Reference<3>, Prev: XInclude support, Up: xml etree ElementTree — The ElementTree XML API
5.20.5.17 Reference
...................
* Menu:
* Functions: Functions<7>.
* Element Objects::
* ElementTree Objects::
* QName Objects::
* TreeBuilder Objects::
* XMLParser Objects::
* XMLPullParser Objects::
* Exceptions: Exceptions<15>.
File: python.info, Node: Functions<7>, Next: Element Objects, Up: Reference<3>
5.20.5.18 Functions
...................
-- Function: xml.etree.ElementInclude.default_loader (href, parse,
encoding=None)
Default loader. This default loader reads an included resource
from disk. `href' is a URL. `parse' is for parse mode either “xml”
or “text”. `encoding' is an optional text encoding. If not given,
encoding is ‘utf-8’. Returns the expanded resource. If the parse
mode is ‘"xml"’, this is an ElementTree instance. If the parse
mode is “text”, this is a Unicode string. If the loader fails, it
can return None or raise an exception.
-- Function: xml.etree.ElementInclude.include (elem, loader=None)
This function expands XInclude directives. `elem' is the root
element. `loader' is an optional resource loader. If omitted, it
defaults to *note default_loader(): 2725. If given, it should be a
callable that implements the same interface as *note
default_loader(): 2725. Returns the expanded resource. If the
parse mode is ‘"xml"’, this is an ElementTree instance. If the
parse mode is “text”, this is a Unicode string. If the loader
fails, it can return None or raise an exception.
File: python.info, Node: Element Objects, Next: ElementTree Objects, Prev: Functions<7>, Up: Reference<3>
5.20.5.19 Element Objects
.........................
-- Class: xml.etree.ElementTree.Element (tag, attrib={}, **extra)
Element class. This class defines the Element interface, and
provides a reference implementation of this interface.
The element name, attribute names, and attribute values can be
either bytestrings or Unicode strings. `tag' is the element name.
`attrib' is an optional dictionary, containing element attributes.
`extra' contains additional attributes, given as keyword arguments.
-- Attribute: tag
A string identifying what kind of data this element represents
(the element type, in other words).
-- Attribute: text
-- Attribute: tail
These attributes can be used to hold additional data
associated with the element. Their values are usually strings
but may be any application-specific object. If the element is
created from an XML file, the `text' attribute holds either
the text between the element’s start tag and its first child
or end tag, or ‘None’, and the `tail' attribute holds either
the text between the element’s end tag and the next tag, or
‘None’. For the XML data
12 3 4
the `a' element has ‘None’ for both `text' and `tail'
attributes, the `b' element has `text' ‘"1"’ and `tail' ‘"4"’,
the `c' element has `text' ‘"2"’ and `tail' ‘None’, and the
`d' element has `text' ‘None’ and `tail' ‘"3"’.
To collect the inner text of an element, see *note itertext():
b53, for example ‘"".join(element.itertext())’.
Applications may store arbitrary objects in these attributes.
-- Attribute: attrib
A dictionary containing the element’s attributes. Note that
while the `attrib' value is always a real mutable Python
dictionary, an ElementTree implementation may choose to use
another internal representation, and create the dictionary
only if someone asks for it. To take advantage of such
implementations, use the dictionary methods below whenever
possible.
The following dictionary-like methods work on the element
attributes.
-- Method: clear ()
Resets an element. This function removes all subelements,
clears all attributes, and sets the text and tail attributes
to ‘None’.
-- Method: get (key, default=None)
Gets the element attribute named `key'.
Returns the attribute value, or `default' if the attribute was
not found.
-- Method: items ()
Returns the element attributes as a sequence of (name, value)
pairs. The attributes are returned in an arbitrary order.
-- Method: keys ()
Returns the elements attribute names as a list. The names are
returned in an arbitrary order.
-- Method: set (key, value)
Set the attribute `key' on the element to `value'.
The following methods work on the element’s children (subelements).
-- Method: append (subelement)
Adds the element `subelement' to the end of this element’s
internal list of subelements. Raises *note TypeError: 192. if
`subelement' is not an *note Element: ac4.
-- Method: extend (subelements)
Appends `subelements' from a sequence object with zero or more
elements. Raises *note TypeError: 192. if a subelement is not
an *note Element: ac4.
New in version 3.2.
-- Method: find (match, namespaces=None)
Finds the first subelement matching `match'. `match' may be a
tag name or a *note path: 41a. Returns an element instance or
‘None’. `namespaces' is an optional mapping from namespace
prefix to full name. Pass ‘''’ as prefix to move all
unprefixed tag names in the expression into the given
namespace.
-- Method: findall (match, namespaces=None)
Finds all matching subelements, by tag name or *note path:
41a. Returns a list containing all matching elements in
document order. `namespaces' is an optional mapping from
namespace prefix to full name. Pass ‘''’ as prefix to move
all unprefixed tag names in the expression into the given
namespace.
-- Method: findtext (match, default=None, namespaces=None)
Finds text for the first subelement matching `match'. `match'
may be a tag name or a *note path: 41a. Returns the text
content of the first matching element, or `default' if no
element was found. Note that if the matching element has no
text content an empty string is returned. `namespaces' is an
optional mapping from namespace prefix to full name. Pass
‘''’ as prefix to move all unprefixed tag names in the
expression into the given namespace.
-- Method: getchildren ()
Deprecated since version 3.2, will be removed in version 3.9:
Use ‘list(elem)’ or iteration.
-- Method: getiterator (tag=None)
Deprecated since version 3.2, will be removed in version 3.9:
Use method *note Element.iter(): ce7. instead.
-- Method: insert (index, subelement)
Inserts `subelement' at the given position in this element.
Raises *note TypeError: 192. if `subelement' is not an *note
Element: ac4.
-- Method: iter (tag=None)
Creates a tree *note iterator: 112e. with the current element
as the root. The iterator iterates over this element and all
elements below it, in document (depth first) order. If `tag'
is not ‘None’ or ‘'*'’, only elements whose tag equals `tag'
are returned from the iterator. If the tree structure is
modified during iteration, the result is undefined.
New in version 3.2.
-- Method: iterfind (match, namespaces=None)
Finds all matching subelements, by tag name or *note path:
41a. Returns an iterable yielding all matching elements in
document order. `namespaces' is an optional mapping from
namespace prefix to full name.
New in version 3.2.
-- Method: itertext ()
Creates a text iterator. The iterator loops over this element
and all subelements, in document order, and returns all inner
text.
New in version 3.2.
-- Method: makeelement (tag, attrib)
Creates a new element object of the same type as this element.
Do not call this method, use the *note SubElement(): 270e.
factory function instead.
-- Method: remove (subelement)
Removes `subelement' from the element. Unlike the find*
methods this method compares elements based on the instance
identity, not on tag value or contents.
*note Element: ac4. objects also support the following sequence
type methods for working with subelements: *note __delitem__():
c63, *note __getitem__(): 27c, *note __setitem__(): c62, *note
__len__(): dcb.
Caution: Elements with no subelements will test as ‘False’. This
behavior will change in future versions. Use specific ‘len(elem)’
or ‘elem is None’ test instead.
element = root.find('foo')
if not element: # careful!
print("element not found, or element has no subelements")
if element is None:
print("element not found")
Prior to Python 3.8, the serialisation order of the XML attributes
of elements was artificially made predictable by sorting the
attributes by their name. Based on the now guaranteed ordering of
dicts, this arbitrary reordering was removed in Python 3.8 to
preserve the order in which attributes were originally parsed or
created by user code.
In general, user code should try not to depend on a specific
ordering of attributes, given that the XML Information Set(1)
explicitly excludes the attribute order from conveying information.
Code should be prepared to deal with any ordering on input. In
cases where deterministic XML output is required, e.g. for
cryptographic signing or test data sets, canonical serialisation is
available with the *note canonicalize(): 2717. function.
In cases where canonical output is not applicable but a specific
attribute order is still desirable on output, code should aim for
creating the attributes directly in the desired order, to avoid
perceptual mismatches for readers of the code. In cases where this
is difficult to achieve, a recipe like the following can be applied
prior to serialisation to enforce an order independently from the
Element creation:
def reorder_attributes(root):
for el in root.iter():
attrib = el.attrib
if len(attrib) > 1:
# adjust attribute order, e.g. by sorting
attribs = sorted(attrib.items())
attrib.clear()
attrib.update(attribs)
---------- Footnotes ----------
(1) https://www.w3.org/TR/xml-infoset/
File: python.info, Node: ElementTree Objects, Next: QName Objects, Prev: Element Objects, Up: Reference<3>
5.20.5.20 ElementTree Objects
.............................
-- Class: xml.etree.ElementTree.ElementTree (element=None, file=None)
ElementTree wrapper class. This class represents an entire element
hierarchy, and adds some extra support for serialization to and
from standard XML.
`element' is the root element. The tree is initialized with the
contents of the XML `file' if given.
-- Method: _setroot (element)
Replaces the root element for this tree. This discards the
current contents of the tree, and replaces it with the given
element. Use with care. `element' is an element instance.
-- Method: find (match, namespaces=None)
Same as *note Element.find(): 2706, starting at the root of
the tree.
-- Method: findall (match, namespaces=None)
Same as *note Element.findall(): 2705, starting at the root of
the tree.
-- Method: findtext (match, default=None, namespaces=None)
Same as *note Element.findtext(): 272f, starting at the root
of the tree.
-- Method: getiterator (tag=None)
Deprecated since version 3.2, will be removed in version 3.9:
Use method *note ElementTree.iter(): 273b. instead.
-- Method: getroot ()
Returns the root element for this tree.
-- Method: iter (tag=None)
Creates and returns a tree iterator for the root element. The
iterator loops over all elements in this tree, in section
order. `tag' is the tag to look for (default is to return all
elements).
-- Method: iterfind (match, namespaces=None)
Same as *note Element.iterfind(): b52, starting at the root of
the tree.
New in version 3.2.
-- Method: parse (source, parser=None)
Loads an external XML section into this element tree.
`source' is a file name or *note file object: b42. `parser'
is an optional parser instance. If not given, the standard
*note XMLParser: 252. parser is used. Returns the section
root element.
-- Method: write (file, encoding="us-ascii", xml_declaration=None,
default_namespace=None, method="xml", *,
short_empty_elements=True)
Writes the element tree to a file, as XML. `file' is a file
name, or a *note file object: b42. opened for writing.
`encoding' (1) is the output encoding (default is US-ASCII).
`xml_declaration' controls if an XML declaration should be
added to the file. Use ‘False’ for never, ‘True’ for always,
‘None’ for only if not US-ASCII or UTF-8 or Unicode (default
is ‘None’). `default_namespace' sets the default XML
namespace (for “xmlns”). `method' is either ‘"xml"’, ‘"html"’
or ‘"text"’ (default is ‘"xml"’). The keyword-only
`short_empty_elements' parameter controls the formatting of
elements that contain no content. If ‘True’ (the default),
they are emitted as a single self-closed tag, otherwise they
are emitted as a pair of start/end tags.
The output is either a string (*note str: 330.) or binary
(*note bytes: 331.). This is controlled by the `encoding'
argument. If `encoding' is ‘"unicode"’, the output is a
string; otherwise, it’s binary. Note that this may conflict
with the type of `file' if it’s an open *note file object:
b42.; make sure you do not try to write a string to a binary
stream and vice versa.
New in version 3.4: The `short_empty_elements' parameter.
Changed in version 3.8: The *note write(): 918. method now
preserves the attribute order specified by the user.
This is the XML file that is going to be manipulated:
Example page
Moved to example.org
or example.com .
Example of changing the attribute “target” of every link in first
paragraph:
>>> from xml.etree.ElementTree import ElementTree
>>> tree = ElementTree()
>>> tree.parse("index.xhtml")
>>> p = tree.find("body/p") # Finds first occurrence of tag p in body
>>> p
>>> links = list(p.iter("a")) # Returns list of all links
>>> links
[, ]
>>> for i in links: # Iterates through all found links
... i.attrib["target"] = "blank"
>>> tree.write("output.xhtml")
---------- Footnotes ----------
(1) (1) The encoding string included in XML output should conform to
the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
not. See
‘https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl’ and
‘https://www.iana.org/assignments/character-sets/character-sets.xhtml’.
File: python.info, Node: QName Objects, Next: TreeBuilder Objects, Prev: ElementTree Objects, Up: Reference<3>
5.20.5.21 QName Objects
.......................
-- Class: xml.etree.ElementTree.QName (text_or_uri, tag=None)
QName wrapper. This can be used to wrap a QName attribute value,
in order to get proper namespace handling on output. `text_or_uri'
is a string containing the QName value, in the form {uri}local, or,
if the tag argument is given, the URI part of a QName. If `tag' is
given, the first argument is interpreted as a URI, and this
argument is interpreted as a local name. *note QName: 2741.
instances are opaque.
File: python.info, Node: TreeBuilder Objects, Next: XMLParser Objects, Prev: QName Objects, Up: Reference<3>
5.20.5.22 TreeBuilder Objects
.............................
-- Class: xml.etree.ElementTree.TreeBuilder (element_factory=None, *,
comment_factory=None, pi_factory=None, insert_comments=False,
insert_pis=False)
Generic element structure builder. This builder converts a
sequence of start, data, end, comment and pi method calls to a
well-formed element structure. You can use this class to build an
element structure using a custom XML parser, or a parser for some
other XML-like format.
`element_factory', when given, must be a callable accepting two
positional arguments: a tag and a dict of attributes. It is
expected to return a new element instance.
The `comment_factory' and `pi_factory' functions, when given,
should behave like the *note Comment(): 2718. and *note
ProcessingInstruction(): 271d. functions to create comments and
processing instructions. When not given, the default factories
will be used. When `insert_comments' and/or `insert_pis' is true,
comments/pis will be inserted into the tree if they appear within
the root element (but not outside of it).
-- Method: close ()
Flushes the builder buffers, and returns the toplevel document
element. Returns an *note Element: ac4. instance.
-- Method: data (data)
Adds text to the current element. `data' is a string. This
should be either a bytestring, or a Unicode string.
-- Method: end (tag)
Closes the current element. `tag' is the element name.
Returns the closed element.
-- Method: start (tag, attrs)
Opens a new element. `tag' is the element name. `attrs' is a
dictionary containing element attributes. Returns the opened
element.
-- Method: comment (text)
Creates a comment with the given `text'. If ‘insert_comments’
is true, this will also add it to the tree.
New in version 3.8.
-- Method: pi (target, text)
Creates a comment with the given `target' name and `text'. If
‘insert_pis’ is true, this will also add it to the tree.
New in version 3.8.
In addition, a custom *note TreeBuilder: 253. object can provide
the following methods:
-- Method: doctype (name, pubid, system)
Handles a doctype declaration. `name' is the doctype name.
`pubid' is the public identifier. `system' is the system
identifier. This method does not exist on the default *note
TreeBuilder: 253. class.
New in version 3.2.
-- Method: start_ns (prefix, uri)
Is called whenever the parser encounters a new namespace
declaration, before the ‘start()’ callback for the opening
element that defines it. `prefix' is ‘''’ for the default
namespace and the declared namespace prefix name otherwise.
`uri' is the namespace URI.
New in version 3.8.
-- Method: end_ns (prefix)
Is called after the ‘end()’ callback of an element that
declared a namespace prefix mapping, with the name of the
`prefix' that went out of scope.
New in version 3.8.
-- Class: xml.etree.ElementTree.C14NWriterTarget (write, *,
with_comments=False, strip_text=False, rewrite_prefixes=False,
qname_aware_tags=None, qname_aware_attrs=None,
exclude_attrs=None, exclude_tags=None)
A C14N 2.0(1) writer. Arguments are the same as for the *note
canonicalize(): 2717. function. This class does not build a tree
but translates the callback events directly into a serialised form
using the `write' function.
New in version 3.8.
---------- Footnotes ----------
(1) https://www.w3.org/TR/xml-c14n2/
File: python.info, Node: XMLParser Objects, Next: XMLPullParser Objects, Prev: TreeBuilder Objects, Up: Reference<3>
5.20.5.23 XMLParser Objects
...........................
-- Class: xml.etree.ElementTree.XMLParser (*, target=None,
encoding=None)
This class is the low-level building block of the module. It uses
*note xml.parsers.expat: 138. for efficient, event-based parsing of
XML. It can be fed XML data incrementally with the *note feed():
274e. method, and parsing events are translated to a push API - by
invoking callbacks on the `target' object. If `target' is omitted,
the standard *note TreeBuilder: 253. is used. If `encoding' (1) is
given, the value overrides the encoding specified in the XML file.
Changed in version 3.8: Parameters are now *note keyword-only: 2ac.
The `html' argument no longer supported.
-- Method: close ()
Finishes feeding data to the parser. Returns the result of
calling the ‘close()’ method of the `target' passed during
construction; by default, this is the toplevel document
element.
-- Method: feed (data)
Feeds data to the parser. `data' is encoded data.
*note XMLParser.feed(): 274e. calls `target'’s ‘start(tag,
attrs_dict)’ method for each opening tag, its ‘end(tag)’ method for
each closing tag, and data is processed by method ‘data(data)’.
For further supported callback methods, see the *note TreeBuilder:
253. class. *note XMLParser.close(): 274f. calls `target'’s method
‘close()’. *note XMLParser: 252. can be used not only for building
a tree structure. This is an example of counting the maximum depth
of an XML file:
>>> from xml.etree.ElementTree import XMLParser
>>> class MaxDepth: # The target object of the parser
... maxDepth = 0
... depth = 0
... def start(self, tag, attrib): # Called for each opening tag.
... self.depth += 1
... if self.depth > self.maxDepth:
... self.maxDepth = self.depth
... def end(self, tag): # Called for each closing tag.
... self.depth -= 1
... def data(self, data):
... pass # We do not need to do anything with data.
... def close(self): # Called when all data has been parsed.
... return self.maxDepth
...
>>> target = MaxDepth()
>>> parser = XMLParser(target=target)
>>> exampleXml = """
...
...
...
...
...
...
...
...
...
... """
>>> parser.feed(exampleXml)
>>> parser.close()
4
---------- Footnotes ----------
(1) (1) The encoding string included in XML output should conform to
the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
not. See
‘https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl’ and
‘https://www.iana.org/assignments/character-sets/character-sets.xhtml’.
File: python.info, Node: XMLPullParser Objects, Next: Exceptions<15>, Prev: XMLParser Objects, Up: Reference<3>
5.20.5.24 XMLPullParser Objects
...............................
-- Class: xml.etree.ElementTree.XMLPullParser (events=None)
A pull parser suitable for non-blocking applications. Its
input-side API is similar to that of *note XMLParser: 252, but
instead of pushing calls to a callback target, *note XMLPullParser:
913. collects an internal list of parsing events and lets the user
read from it. `events' is a sequence of events to report back.
The supported events are the strings ‘"start"’, ‘"end"’,
‘"comment"’, ‘"pi"’, ‘"start-ns"’ and ‘"end-ns"’ (the “ns” events
are used to get detailed namespace information). If `events' is
omitted, only ‘"end"’ events are reported.
-- Method: feed (data)
Feed the given bytes data to the parser.
-- Method: close ()
Signal the parser that the data stream is terminated. Unlike
*note XMLParser.close(): 274f, this method always returns
*note None: 157. Any events not yet retrieved when the parser
is closed can still be read with *note read_events(): 2703.
-- Method: read_events ()
Return an iterator over the events which have been encountered
in the data fed to the parser. The iterator yields ‘(event,
elem)’ pairs, where `event' is a string representing the type
of event (e.g. ‘"end"’) and `elem' is the encountered *note
Element: ac4. object, or other context value as follows.
* ‘start’, ‘end’: the current Element.
* ‘comment’, ‘pi’: the current comment / processing
instruction
* ‘start-ns’: a tuple ‘(prefix, uri)’ naming the declared
namespace mapping.
* ‘end-ns’: *note None: 157. (this may change in a future
version)
Events provided in a previous call to *note read_events():
2703. will not be yielded again. Events are consumed from the
internal queue only when they are retrieved from the iterator,
so multiple readers iterating in parallel over iterators
obtained from *note read_events(): 2703. will have
unpredictable results.
Note: *note XMLPullParser: 913. only guarantees that it has
seen the “>” character of a starting tag when it emits a
“start” event, so the attributes are defined, but the contents
of the text and tail attributes are undefined at that point.
The same applies to the element children; they may or may not
be present.
If you need a fully populated element, look for “end” events
instead.
New in version 3.4.
Changed in version 3.8: The ‘comment’ and ‘pi’ events were added.
File: python.info, Node: Exceptions<15>, Prev: XMLPullParser Objects, Up: Reference<3>
5.20.5.25 Exceptions
....................
-- Class: xml.etree.ElementTree.ParseError
XML parse error, raised by the various parsing methods in this
module when parsing fails. The string representation of an
instance of this exception will contain a user-friendly error
message. In addition, it will have the following attributes
available:
-- Attribute: code
A numeric error code from the expat parser. See the
documentation of *note xml.parsers.expat: 138. for the list of
error codes and their meanings.
-- Attribute: position
A tuple of `line', `column' numbers, specifying where the
error occurred.
File: python.info, Node: xml dom — The Document Object Model API, Next: xml dom minidom — Minimal DOM implementation, Prev: xml etree ElementTree — The ElementTree XML API, Up: Structured Markup Processing Tools
5.20.6 ‘xml.dom’ — The Document Object Model API
------------------------------------------------
`Source code:' Lib/xml/dom/__init__.py(1)
__________________________________________________________________
The Document Object Model, or “DOM,” is a cross-language API from the
World Wide Web Consortium (W3C) for accessing and modifying XML
documents. A DOM implementation presents an XML document as a tree
structure, or allows client code to build such a structure from scratch.
It then gives access to the structure through a set of objects which
provided well-known interfaces.
The DOM is extremely useful for random-access applications. SAX only
allows you a view of one bit of the document at a time. If you are
looking at one SAX element, you have no access to another. If you are
looking at a text node, you have no access to a containing element.
When you write a SAX application, you need to keep track of your
program’s position in the document somewhere in your own code. SAX does
not do it for you. Also, if you need to look ahead in the XML document,
you are just out of luck.
Some applications are simply impossible in an event driven model with no
access to a tree. Of course you could build some sort of tree yourself
in SAX events, but the DOM allows you to avoid writing that code. The
DOM is a standard tree representation for XML data.
The Document Object Model is being defined by the W3C in stages, or
“levels” in their terminology. The Python mapping of the API is
substantially based on the DOM Level 2 recommendation.
DOM applications typically start by parsing some XML into a DOM. How
this is accomplished is not covered at all by DOM Level 1, and Level 2
provides only limited improvements: There is a ‘DOMImplementation’
object class which provides access to ‘Document’ creation methods, but
no way to access an XML reader/parser/Document builder in an
implementation-independent way. There is also no well-defined way to
access these methods without an existing ‘Document’ object. In Python,
each DOM implementation will provide a function *note
getDOMImplementation(): 2758. DOM Level 3 adds a Load/Store
specification, which defines an interface to the reader, but this is not
yet available in the Python standard library.
Once you have a DOM document object, you can access the parts of your
XML document through its properties and methods. These properties are
defined in the DOM specification; this portion of the reference manual
describes the interpretation of the specification in Python.
The specification provided by the W3C defines the DOM API for Java,
ECMAScript, and OMG IDL. The Python mapping defined here is based in
large part on the IDL version of the specification, but strict
compliance is not required (though implementations are free to support
the strict mapping from IDL). See section *note Conformance: 2759. for a
detailed discussion of mapping requirements.
See also
........
Document Object Model (DOM) Level 2 Specification(2)
The W3C recommendation upon which the Python DOM API is based.
Document Object Model (DOM) Level 1 Specification(3)
The W3C recommendation for the DOM supported by *note
xml.dom.minidom: 135.
Python Language Mapping Specification(4)
This specifies the mapping from OMG IDL to Python.
* Menu:
* Module Contents: Module Contents<4>.
* Objects in the DOM::
* Conformance::
---------- Footnotes ----------
(1)
https://github.com/python/cpython/tree/3.8/Lib/xml/dom/__init__.py
(2) https://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
(3) https://www.w3.org/TR/REC-DOM-Level-1/
(4) http://www.omg.org/cgi-bin/doc?formal/02-11-05.pdf
File: python.info, Node: Module Contents<4>, Next: Objects in the DOM, Up: xml dom — The Document Object Model API
5.20.6.1 Module Contents
........................
The *note xml.dom: 134. contains the following functions:
-- Function: xml.dom.registerDOMImplementation (name, factory)
Register the `factory' function with the name `name'. The factory
function should return an object which implements the
‘DOMImplementation’ interface. The factory function can return the
same object every time, or a new one for each call, as appropriate
for the specific implementation (e.g. if that implementation
supports some customization).
-- Function: xml.dom.getDOMImplementation (name=None, features=())
Return a suitable DOM implementation. The `name' is either
well-known, the module name of a DOM implementation, or ‘None’. If
it is not ‘None’, imports the corresponding module and returns a
‘DOMImplementation’ object if the import succeeds. If no name is
given, and if the environment variable ‘PYTHON_DOM’ is set, this
variable is used to find the implementation.
If name is not given, this examines the available implementations
to find one with the required feature set. If no implementation
can be found, raise an *note ImportError: 334. The features list
must be a sequence of ‘(feature, version)’ pairs which are passed
to the ‘hasFeature()’ method on available ‘DOMImplementation’
objects.
Some convenience constants are also provided:
-- Data: xml.dom.EMPTY_NAMESPACE
The value used to indicate that no namespace is associated with a
node in the DOM. This is typically found as the ‘namespaceURI’ of a
node, or used as the `namespaceURI' parameter to a
namespaces-specific method.
-- Data: xml.dom.XML_NAMESPACE
The namespace URI associated with the reserved prefix ‘xml’, as
defined by Namespaces in XML(1) (section 4).
-- Data: xml.dom.XMLNS_NAMESPACE
The namespace URI for namespace declarations, as defined by
Document Object Model (DOM) Level 2 Core Specification(2) (section
1.1.8).
-- Data: xml.dom.XHTML_NAMESPACE
The URI of the XHTML namespace as defined by XHTML 1.0: The
Extensible HyperText Markup Language(3) (section 3.1.1).
In addition, *note xml.dom: 134. contains a base ‘Node’ class and the
DOM exception classes. The ‘Node’ class provided by this module does
not implement any of the methods or attributes defined by the DOM
specification; concrete DOM implementations must provide those. The
‘Node’ class provided as part of this module does provide the constants
used for the ‘nodeType’ attribute on concrete ‘Node’ objects; they are
located within the class rather than at the module level to conform with
the DOM specifications.
---------- Footnotes ----------
(1) https://www.w3.org/TR/REC-xml-names/
(2) https://www.w3.org/TR/DOM-Level-2-Core/core.html
(3) https://www.w3.org/TR/xhtml1/
File: python.info, Node: Objects in the DOM, Next: Conformance, Prev: Module Contents<4>, Up: xml dom — The Document Object Model API
5.20.6.2 Objects in the DOM
...........................
The definitive documentation for the DOM is the DOM specification from
the W3C.
Note that DOM attributes may also be manipulated as nodes instead of as
simple strings. It is fairly rare that you must do this, however, so
this usage is not yet documented.
Interface Section Purpose
-------------------------------------------------------------------------------------------------------------------
‘DOMImplementation’ *note DOMImplementation Objects: 2762. Interface to the underlying
implementation.
‘Node’ *note Node Objects: 2763. Base interface for most objects in
a document.
‘NodeList’ *note NodeList Objects: 2764. Interface for a sequence of nodes.
‘DocumentType’ *note DocumentType Objects: 2765. Information about the declarations
needed to process a document.
‘Document’ *note Document Objects: 2766. Object which represents an entire
document.
‘Element’ *note Element Objects: 2767. Element nodes in the document
hierarchy.
‘Attr’ *note Attr Objects: 2768. Attribute value nodes on element
nodes.
‘Comment’ *note Comment Objects: 2769. Representation of comments in the
source document.
‘Text’ *note Text and CDATASection Objects: 276a.Nodes containing textual content
from the document.
‘ProcessingInstruction’ *note ProcessingInstruction Objects: 276b.Processing instruction
representation.
An additional section describes the exceptions defined for working with
the DOM in Python.
* Menu:
* DOMImplementation Objects::
* Node Objects::
* NodeList Objects::
* DocumentType Objects::
* Document Objects::
* Element Objects: Element Objects<2>.
* Attr Objects::
* NamedNodeMap Objects::
* Comment Objects::
* Text and CDATASection Objects::
* ProcessingInstruction Objects::
* Exceptions: Exceptions<16>.
File: python.info, Node: DOMImplementation Objects, Next: Node Objects, Up: Objects in the DOM
5.20.6.3 DOMImplementation Objects
..................................
The ‘DOMImplementation’ interface provides a way for applications to
determine the availability of particular features in the DOM they are
using. DOM Level 2 added the ability to create new ‘Document’ and
‘DocumentType’ objects using the ‘DOMImplementation’ as well.
-- Method: DOMImplementation.hasFeature (feature, version)
Return ‘True’ if the feature identified by the pair of strings
`feature' and `version' is implemented.
-- Method: DOMImplementation.createDocument (namespaceUri,
qualifiedName, doctype)
Return a new ‘Document’ object (the root of the DOM), with a child
‘Element’ object having the given `namespaceUri' and
`qualifiedName'. The `doctype' must be a ‘DocumentType’ object
created by *note createDocumentType(): 276f, or ‘None’. In the
Python DOM API, the first two arguments can also be ‘None’ in order
to indicate that no ‘Element’ child is to be created.
-- Method: DOMImplementation.createDocumentType (qualifiedName,
publicId, systemId)
Return a new ‘DocumentType’ object that encapsulates the given
`qualifiedName', `publicId', and `systemId' strings, representing
the information contained in an XML document type declaration.
File: python.info, Node: Node Objects, Next: NodeList Objects, Prev: DOMImplementation Objects, Up: Objects in the DOM
5.20.6.4 Node Objects
.....................
All of the components of an XML document are subclasses of ‘Node’.
-- Attribute: Node.nodeType
An integer representing the node type. Symbolic constants for the
types are on the ‘Node’ object: ‘ELEMENT_NODE’, ‘ATTRIBUTE_NODE’,
‘TEXT_NODE’, ‘CDATA_SECTION_NODE’, ‘ENTITY_NODE’,
‘PROCESSING_INSTRUCTION_NODE’, ‘COMMENT_NODE’, ‘DOCUMENT_NODE’,
‘DOCUMENT_TYPE_NODE’, ‘NOTATION_NODE’. This is a read-only
attribute.
-- Attribute: Node.parentNode
The parent of the current node, or ‘None’ for the document node.
The value is always a ‘Node’ object or ‘None’. For ‘Element’
nodes, this will be the parent element, except for the root
element, in which case it will be the ‘Document’ object. For
‘Attr’ nodes, this is always ‘None’. This is a read-only
attribute.
-- Attribute: Node.attributes
A ‘NamedNodeMap’ of attribute objects. Only elements have actual
values for this; others provide ‘None’ for this attribute. This is
a read-only attribute.
-- Attribute: Node.previousSibling
The node that immediately precedes this one with the same parent.
For instance the element with an end-tag that comes just before the
`self' element’s start-tag. Of course, XML documents are made up
of more than just elements so the previous sibling could be text, a
comment, or something else. If this node is the first child of the
parent, this attribute will be ‘None’. This is a read-only
attribute.
-- Attribute: Node.nextSibling
The node that immediately follows this one with the same parent.
See also *note previousSibling: 2774. If this is the last child of
the parent, this attribute will be ‘None’. This is a read-only
attribute.
-- Attribute: Node.childNodes
A list of nodes contained within this node. This is a read-only
attribute.
-- Attribute: Node.firstChild
The first child of the node, if there are any, or ‘None’. This is
a read-only attribute.
-- Attribute: Node.lastChild
The last child of the node, if there are any, or ‘None’. This is a
read-only attribute.
-- Attribute: Node.localName
The part of the ‘tagName’ following the colon if there is one, else
the entire ‘tagName’. The value is a string.
-- Attribute: Node.prefix
The part of the ‘tagName’ preceding the colon if there is one, else
the empty string. The value is a string, or ‘None’.
-- Attribute: Node.namespaceURI
The namespace associated with the element name. This will be a
string or ‘None’. This is a read-only attribute.
-- Attribute: Node.nodeName
This has a different meaning for each node type; see the DOM
specification for details. You can always get the information you
would get here from another property such as the ‘tagName’ property
for elements or the ‘name’ property for attributes. For all node
types, the value of this attribute will be either a string or
‘None’. This is a read-only attribute.
-- Attribute: Node.nodeValue
This has a different meaning for each node type; see the DOM
specification for details. The situation is similar to that with
*note nodeName: 277c. The value is a string or ‘None’.
-- Method: Node.hasAttributes ()
Return ‘True’ if the node has any attributes.
-- Method: Node.hasChildNodes ()
Return ‘True’ if the node has any child nodes.
-- Method: Node.isSameNode (other)
Return ‘True’ if `other' refers to the same node as this node.
This is especially useful for DOM implementations which use any
sort of proxy architecture (because more than one object can refer
to the same node).
Note: This is based on a proposed DOM Level 3 API which is
still in the “working draft” stage, but this particular
interface appears uncontroversial. Changes from the W3C will
not necessarily affect this method in the Python DOM interface
(though any new W3C API for this would also be supported).
-- Method: Node.appendChild (newChild)
Add a new child node to this node at the end of the list of
children, returning `newChild'. If the node was already in the
tree, it is removed first.
-- Method: Node.insertBefore (newChild, refChild)
Insert a new child node before an existing child. It must be the
case that `refChild' is a child of this node; if not, *note
ValueError: 1fb. is raised. `newChild' is returned. If `refChild'
is ‘None’, it inserts `newChild' at the end of the children’s list.
-- Method: Node.removeChild (oldChild)
Remove a child node. `oldChild' must be a child of this node; if
not, *note ValueError: 1fb. is raised. `oldChild' is returned on
success. If `oldChild' will not be used further, its ‘unlink()’
method should be called.
-- Method: Node.replaceChild (newChild, oldChild)
Replace an existing node with a new node. It must be the case that
`oldChild' is a child of this node; if not, *note ValueError: 1fb.
is raised.
-- Method: Node.normalize ()
Join adjacent text nodes so that all stretches of text are stored
as single ‘Text’ instances. This simplifies processing text from a
DOM tree for many applications.
-- Method: Node.cloneNode (deep)
Clone this node. Setting `deep' means to clone all child nodes as
well. This returns the clone.
File: python.info, Node: NodeList Objects, Next: DocumentType Objects, Prev: Node Objects, Up: Objects in the DOM
5.20.6.5 NodeList Objects
.........................
A ‘NodeList’ represents a sequence of nodes. These objects are used in
two ways in the DOM Core recommendation: an ‘Element’ object provides
one as its list of child nodes, and the ‘getElementsByTagName()’ and
‘getElementsByTagNameNS()’ methods of ‘Node’ return objects with this
interface to represent query results.
The DOM Level 2 recommendation defines one method and one attribute for
these objects:
-- Method: NodeList.item (i)
Return the `i'’th item from the sequence, if there is one, or
‘None’. The index `i' is not allowed to be less than zero or
greater than or equal to the length of the sequence.
-- Attribute: NodeList.length
The number of nodes in the sequence.
In addition, the Python DOM interface requires that some additional
support is provided to allow ‘NodeList’ objects to be used as Python
sequences. All ‘NodeList’ implementations must include support for
*note __len__(): dcb. and *note __getitem__(): 27c.; this allows
iteration over the ‘NodeList’ in *note for: c30. statements and proper
support for the *note len(): 150. built-in function.
If a DOM implementation supports modification of the document, the
‘NodeList’ implementation must also support the *note __setitem__():
c62. and *note __delitem__(): c63. methods.
File: python.info, Node: DocumentType Objects, Next: Document Objects, Prev: NodeList Objects, Up: Objects in the DOM
5.20.6.6 DocumentType Objects
.............................
Information about the notations and entities declared by a document
(including the external subset if the parser uses it and can provide the
information) is available from a ‘DocumentType’ object. The
‘DocumentType’ for a document is available from the ‘Document’ object’s
‘doctype’ attribute; if there is no ‘DOCTYPE’ declaration for the
document, the document’s ‘doctype’ attribute will be set to ‘None’
instead of an instance of this interface.
‘DocumentType’ is a specialization of ‘Node’, and adds the following
attributes:
-- Attribute: DocumentType.publicId
The public identifier for the external subset of the document type
definition. This will be a string or ‘None’.
-- Attribute: DocumentType.systemId
The system identifier for the external subset of the document type
definition. This will be a URI as a string, or ‘None’.
-- Attribute: DocumentType.internalSubset
A string giving the complete internal subset from the document.
This does not include the brackets which enclose the subset. If
the document has no internal subset, this should be ‘None’.
-- Attribute: DocumentType.name
The name of the root element as given in the ‘DOCTYPE’ declaration,
if present.
-- Attribute: DocumentType.entities
This is a ‘NamedNodeMap’ giving the definitions of external
entities. For entity names defined more than once, only the first
definition is provided (others are ignored as required by the XML
recommendation). This may be ‘None’ if the information is not
provided by the parser, or if no entities are defined.
-- Attribute: DocumentType.notations
This is a ‘NamedNodeMap’ giving the definitions of notations. For
notation names defined more than once, only the first definition is
provided (others are ignored as required by the XML
recommendation). This may be ‘None’ if the information is not
provided by the parser, or if no notations are defined.
File: python.info, Node: Document Objects, Next: Element Objects<2>, Prev: DocumentType Objects, Up: Objects in the DOM
5.20.6.7 Document Objects
.........................
A ‘Document’ represents an entire XML document, including its
constituent elements, attributes, processing instructions, comments etc.
Remember that it inherits properties from ‘Node’.
-- Attribute: Document.documentElement
The one and only root element of the document.
-- Method: Document.createElement (tagName)
Create and return a new element node. The element is not inserted
into the document when it is created. You need to explicitly
insert it with one of the other methods such as ‘insertBefore()’ or
‘appendChild()’.
-- Method: Document.createElementNS (namespaceURI, tagName)
Create and return a new element with a namespace. The `tagName'
may have a prefix. The element is not inserted into the document
when it is created. You need to explicitly insert it with one of
the other methods such as ‘insertBefore()’ or ‘appendChild()’.
-- Method: Document.createTextNode (data)
Create and return a text node containing the data passed as a
parameter. As with the other creation methods, this one does not
insert the node into the tree.
-- Method: Document.createComment (data)
Create and return a comment node containing the data passed as a
parameter. As with the other creation methods, this one does not
insert the node into the tree.
-- Method: Document.createProcessingInstruction (target, data)
Create and return a processing instruction node containing the
`target' and `data' passed as parameters. As with the other
creation methods, this one does not insert the node into the tree.
-- Method: Document.createAttribute (name)
Create and return an attribute node. This method does not
associate the attribute node with any particular element. You must
use ‘setAttributeNode()’ on the appropriate ‘Element’ object to use
the newly created attribute instance.
-- Method: Document.createAttributeNS (namespaceURI, qualifiedName)
Create and return an attribute node with a namespace. The
`tagName' may have a prefix. This method does not associate the
attribute node with any particular element. You must use
‘setAttributeNode()’ on the appropriate ‘Element’ object to use the
newly created attribute instance.
-- Method: Document.getElementsByTagName (tagName)
Search for all descendants (direct children, children’s children,
etc.) with a particular element type name.
-- Method: Document.getElementsByTagNameNS (namespaceURI, localName)
Search for all descendants (direct children, children’s children,
etc.) with a particular namespace URI and localname. The
localname is the part of the namespace after the prefix.
File: python.info, Node: Element Objects<2>, Next: Attr Objects, Prev: Document Objects, Up: Objects in the DOM
5.20.6.8 Element Objects
........................
‘Element’ is a subclass of ‘Node’, so inherits all the attributes of
that class.
-- Attribute: Element.tagName
The element type name. In a namespace-using document it may have
colons in it. The value is a string.
-- Method: Element.getElementsByTagName (tagName)
Same as equivalent method in the ‘Document’ class.
-- Method: Element.getElementsByTagNameNS (namespaceURI, localName)
Same as equivalent method in the ‘Document’ class.
-- Method: Element.hasAttribute (name)
Return ‘True’ if the element has an attribute named by `name'.
-- Method: Element.hasAttributeNS (namespaceURI, localName)
Return ‘True’ if the element has an attribute named by
`namespaceURI' and `localName'.
-- Method: Element.getAttribute (name)
Return the value of the attribute named by `name' as a string. If
no such attribute exists, an empty string is returned, as if the
attribute had no value.
-- Method: Element.getAttributeNode (attrname)
Return the ‘Attr’ node for the attribute named by `attrname'.
-- Method: Element.getAttributeNS (namespaceURI, localName)
Return the value of the attribute named by `namespaceURI' and
`localName' as a string. If no such attribute exists, an empty
string is returned, as if the attribute had no value.
-- Method: Element.getAttributeNodeNS (namespaceURI, localName)
Return an attribute value as a node, given a `namespaceURI' and
`localName'.
-- Method: Element.removeAttribute (name)
Remove an attribute by name. If there is no matching attribute, a
*note NotFoundErr: 27a7. is raised.
-- Method: Element.removeAttributeNode (oldAttr)
Remove and return `oldAttr' from the attribute list, if present.
If `oldAttr' is not present, *note NotFoundErr: 27a7. is raised.
-- Method: Element.removeAttributeNS (namespaceURI, localName)
Remove an attribute by name. Note that it uses a localName, not a
qname. No exception is raised if there is no matching attribute.
-- Method: Element.setAttribute (name, value)
Set an attribute value from a string.
-- Method: Element.setAttributeNode (newAttr)
Add a new attribute node to the element, replacing an existing
attribute if necessary if the ‘name’ attribute matches. If a
replacement occurs, the old attribute node will be returned. If
`newAttr' is already in use, *note InuseAttributeErr: 27ac. will be
raised.
-- Method: Element.setAttributeNodeNS (newAttr)
Add a new attribute node to the element, replacing an existing
attribute if necessary if the ‘namespaceURI’ and ‘localName’
attributes match. If a replacement occurs, the old attribute node
will be returned. If `newAttr' is already in use, *note
InuseAttributeErr: 27ac. will be raised.
-- Method: Element.setAttributeNS (namespaceURI, qname, value)
Set an attribute value from a string, given a `namespaceURI' and a
`qname'. Note that a qname is the whole attribute name. This is
different than above.
File: python.info, Node: Attr Objects, Next: NamedNodeMap Objects, Prev: Element Objects<2>, Up: Objects in the DOM
5.20.6.9 Attr Objects
.....................
‘Attr’ inherits from ‘Node’, so inherits all its attributes.
-- Attribute: Attr.name
The attribute name. In a namespace-using document it may include a
colon.
-- Attribute: Attr.localName
The part of the name following the colon if there is one, else the
entire name. This is a read-only attribute.
-- Attribute: Attr.prefix
The part of the name preceding the colon if there is one, else the
empty string.
-- Attribute: Attr.value
The text value of the attribute. This is a synonym for the
‘nodeValue’ attribute.
File: python.info, Node: NamedNodeMap Objects, Next: Comment Objects, Prev: Attr Objects, Up: Objects in the DOM
5.20.6.10 NamedNodeMap Objects
..............................
‘NamedNodeMap’ does `not' inherit from ‘Node’.
-- Attribute: NamedNodeMap.length
The length of the attribute list.
-- Method: NamedNodeMap.item (index)
Return an attribute with a particular index. The order you get the
attributes in is arbitrary but will be consistent for the life of a
DOM. Each item is an attribute node. Get its value with the
‘value’ attribute.
There are also experimental methods that give this class more mapping
behavior. You can use them or you can use the standardized
‘getAttribute*()’ family of methods on the ‘Element’ objects.
File: python.info, Node: Comment Objects, Next: Text and CDATASection Objects, Prev: NamedNodeMap Objects, Up: Objects in the DOM
5.20.6.11 Comment Objects
.........................
‘Comment’ represents a comment in the XML document. It is a subclass of
‘Node’, but cannot have child nodes.
-- Attribute: Comment.data
The content of the comment as a string. The attribute contains all
characters between the leading ‘’, but
does not include them.
File: python.info, Node: Text and CDATASection Objects, Next: ProcessingInstruction Objects, Prev: Comment Objects, Up: Objects in the DOM
5.20.6.12 Text and CDATASection Objects
.......................................
The ‘Text’ interface represents text in the XML document. If the parser
and DOM implementation support the DOM’s XML extension, portions of the
text enclosed in CDATA marked sections are stored in ‘CDATASection’
objects. These two interfaces are identical, but provide different
values for the ‘nodeType’ attribute.
These interfaces extend the ‘Node’ interface. They cannot have child
nodes.
-- Attribute: Text.data
The content of the text node as a string.
Note: The use of a ‘CDATASection’ node does not indicate that the
node represents a complete CDATA marked section, only that the
content of the node was part of a CDATA section. A single CDATA
section may be represented by more than one node in the document
tree. There is no way to determine whether two adjacent
‘CDATASection’ nodes represent different CDATA marked sections.
File: python.info, Node: ProcessingInstruction Objects, Next: Exceptions<16>, Prev: Text and CDATASection Objects, Up: Objects in the DOM
5.20.6.13 ProcessingInstruction Objects
.......................................
Represents a processing instruction in the XML document; this inherits
from the ‘Node’ interface and cannot have child nodes.
-- Attribute: ProcessingInstruction.target
The content of the processing instruction up to the first
whitespace character. This is a read-only attribute.
-- Attribute: ProcessingInstruction.data
The content of the processing instruction following the first
whitespace character.
File: python.info, Node: Exceptions<16>, Prev: ProcessingInstruction Objects, Up: Objects in the DOM
5.20.6.14 Exceptions
....................
The DOM Level 2 recommendation defines a single exception, *note
DOMException: 27c1, and a number of constants that allow applications to
determine what sort of error occurred. *note DOMException: 27c1.
instances carry a *note code: 1b. attribute that provides the
appropriate value for the specific exception.
The Python DOM interface provides the constants, but also expands the
set of exceptions so that a specific exception exists for each of the
exception codes defined by the DOM. The implementations must raise the
appropriate specific exception, each of which carries the appropriate
value for the *note code: 1b. attribute.
-- Exception: xml.dom.DOMException
Base exception class used for all specific DOM exceptions. This
exception class cannot be directly instantiated.
-- Exception: xml.dom.DomstringSizeErr
Raised when a specified range of text does not fit into a string.
This is not known to be used in the Python DOM implementations, but
may be received from DOM implementations not written in Python.
-- Exception: xml.dom.HierarchyRequestErr
Raised when an attempt is made to insert a node where the node type
is not allowed.
-- Exception: xml.dom.IndexSizeErr
Raised when an index or size parameter to a method is negative or
exceeds the allowed values.
-- Exception: xml.dom.InuseAttributeErr
Raised when an attempt is made to insert an ‘Attr’ node that is
already present elsewhere in the document.
-- Exception: xml.dom.InvalidAccessErr
Raised if a parameter or an operation is not supported on the
underlying object.
-- Exception: xml.dom.InvalidCharacterErr
This exception is raised when a string parameter contains a
character that is not permitted in the context it’s being used in
by the XML 1.0 recommendation. For example, attempting to create
an ‘Element’ node with a space in the element type name will cause
this error to be raised.
-- Exception: xml.dom.InvalidModificationErr
Raised when an attempt is made to modify the type of a node.
-- Exception: xml.dom.InvalidStateErr
Raised when an attempt is made to use an object that is not defined
or is no longer usable.
-- Exception: xml.dom.NamespaceErr
If an attempt is made to change any object in a way that is not
permitted with regard to the Namespaces in XML(1) recommendation,
this exception is raised.
-- Exception: xml.dom.NotFoundErr
Exception when a node does not exist in the referenced context.
For example, ‘NamedNodeMap.removeNamedItem()’ will raise this if
the node passed in does not exist in the map.
-- Exception: xml.dom.NotSupportedErr
Raised when the implementation does not support the requested type
of object or operation.
-- Exception: xml.dom.NoDataAllowedErr
This is raised if data is specified for a node which does not
support data.
-- Exception: xml.dom.NoModificationAllowedErr
Raised on attempts to modify an object where modifications are not
allowed (such as for read-only nodes).
-- Exception: xml.dom.SyntaxErr
Raised when an invalid or illegal string is specified.
-- Exception: xml.dom.WrongDocumentErr
Raised when a node is inserted in a different document than it
currently belongs to, and the implementation does not support
migrating the node from one document to the other.
The exception codes defined in the DOM recommendation map to the
exceptions described above according to this table:
Constant Exception
---------------------------------------------------------------------------------
‘DOMSTRING_SIZE_ERR’ *note DomstringSizeErr: 27c2.
‘HIERARCHY_REQUEST_ERR’ *note HierarchyRequestErr: 27c3.
‘INDEX_SIZE_ERR’ *note IndexSizeErr: 27c4.
‘INUSE_ATTRIBUTE_ERR’ *note InuseAttributeErr: 27ac.
‘INVALID_ACCESS_ERR’ *note InvalidAccessErr: 27c5.
‘INVALID_CHARACTER_ERR’ *note InvalidCharacterErr: 27c6.
‘INVALID_MODIFICATION_ERR’ *note InvalidModificationErr: 27c7.
‘INVALID_STATE_ERR’ *note InvalidStateErr: 27c8.
‘NAMESPACE_ERR’ *note NamespaceErr: 27c9.
‘NOT_FOUND_ERR’ *note NotFoundErr: 27a7.
‘NOT_SUPPORTED_ERR’ *note NotSupportedErr: 27ca.
‘NO_DATA_ALLOWED_ERR’ *note NoDataAllowedErr: 27cb.
‘NO_MODIFICATION_ALLOWED_ERR’ *note NoModificationAllowedErr: 27cc.
‘SYNTAX_ERR’ *note SyntaxErr: 27cd.
‘WRONG_DOCUMENT_ERR’ *note WrongDocumentErr: 27ce.
---------- Footnotes ----------
(1) https://www.w3.org/TR/REC-xml-names/
File: python.info, Node: Conformance, Prev: Objects in the DOM, Up: xml dom — The Document Object Model API
5.20.6.15 Conformance
.....................
This section describes the conformance requirements and relationships
between the Python DOM API, the W3C DOM recommendations, and the OMG IDL
mapping for Python.
* Menu:
* Type Mapping::
* Accessor Methods::
File: python.info, Node: Type Mapping, Next: Accessor Methods, Up: Conformance
5.20.6.16 Type Mapping
......................
The IDL types used in the DOM specification are mapped to Python types
according to the following table.
IDL Type Python Type
-----------------------------------------------------------------------
‘boolean’ ‘bool’ or ‘int’
‘int’ ‘int’
‘long int’ ‘int’
‘unsigned int’ ‘int’
‘DOMString’ ‘str’ or ‘bytes’
‘null’ ‘None’
File: python.info, Node: Accessor Methods, Prev: Type Mapping, Up: Conformance
5.20.6.17 Accessor Methods
..........................
The mapping from OMG IDL to Python defines accessor functions for IDL
‘attribute’ declarations in much the way the Java mapping does. Mapping
the IDL declarations
readonly attribute string someValue;
attribute string anotherValue;
yields three accessor functions: a “get” method for ‘someValue’
(‘_get_someValue()’), and “get” and “set” methods for ‘anotherValue’
(‘_get_anotherValue()’ and ‘_set_anotherValue()’). The mapping, in
particular, does not require that the IDL attributes are accessible as
normal Python attributes: ‘object.someValue’ is `not' required to work,
and may raise an *note AttributeError: 39b.
The Python DOM API, however, `does' require that normal attribute access
work. This means that the typical surrogates generated by Python IDL
compilers are not likely to work, and wrapper objects may be needed on
the client if the DOM objects are accessed via CORBA. While this does
require some additional consideration for CORBA DOM clients, the
implementers with experience using DOM over CORBA from Python do not
consider this a problem. Attributes that are declared ‘readonly’ may
not restrict write access in all DOM implementations.
In the Python DOM API, accessor functions are not required. If
provided, they should take the form defined by the Python IDL mapping,
but these methods are considered unnecessary since the attributes are
accessible directly from Python. “Set” accessors should never be
provided for ‘readonly’ attributes.
The IDL definitions do not fully embody the requirements of the W3C DOM
API, such as the notion of certain objects, such as the return value of
‘getElementsByTagName()’, being “live”. The Python DOM API does not
require implementations to enforce such requirements.
File: python.info, Node: xml dom minidom — Minimal DOM implementation, Next: xml dom pulldom — Support for building partial DOM trees, Prev: xml dom — The Document Object Model API, Up: Structured Markup Processing Tools
5.20.7 ‘xml.dom.minidom’ — Minimal DOM implementation
-----------------------------------------------------
`Source code:' Lib/xml/dom/minidom.py(1)
__________________________________________________________________
*note xml.dom.minidom: 135. is a minimal implementation of the Document
Object Model interface, with an API similar to that in other languages.
It is intended to be simpler than the full DOM and also significantly
smaller. Users who are not already proficient with the DOM should
consider using the *note xml.etree.ElementTree: 137. module for their
XML processing instead.
Warning: The *note xml.dom.minidom: 135. module is not secure
against maliciously constructed data. If you need to parse
untrusted or unauthenticated data see *note XML vulnerabilities:
26f5.
DOM applications typically start by parsing some XML into a DOM. With
*note xml.dom.minidom: 135, this is done through the parse functions:
from xml.dom.minidom import parse, parseString
dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name
datasource = open('c:\\temp\\mydata.xml')
dom2 = parse(datasource) # parse an open file
dom3 = parseString('Some data some more data ')
The *note parse(): 27d6. function can take either a filename or an open
file object.
-- Function: xml.dom.minidom.parse (filename_or_file, parser=None,
bufsize=None)
Return a ‘Document’ from the given input. `filename_or_file' may
be either a file name, or a file-like object. `parser', if given,
must be a SAX2 parser object. This function will change the
document handler of the parser and activate namespace support;
other parser configuration (like setting an entity resolver) must
have been done in advance.
If you have XML in a string, you can use the *note parseString(): 27d7.
function instead:
-- Function: xml.dom.minidom.parseString (string, parser=None)
Return a ‘Document’ that represents the `string'. This method
creates an *note io.StringIO: 82d. object for the string and passes
that on to *note parse(): 27d6.
Both functions return a ‘Document’ object representing the content of
the document.
What the *note parse(): 27d6. and *note parseString(): 27d7. functions
do is connect an XML parser with a “DOM builder” that can accept parse
events from any SAX parser and convert them into a DOM tree. The name
of the functions are perhaps misleading, but are easy to grasp when
learning the interfaces. The parsing of the document will be completed
before these functions return; it’s simply that these functions do not
provide a parser implementation themselves.
You can also create a ‘Document’ by calling a method on a “DOM
Implementation” object. You can get this object either by calling the
‘getDOMImplementation()’ function in the *note xml.dom: 134. package or
the *note xml.dom.minidom: 135. module. Once you have a ‘Document’, you
can add child nodes to it to populate the DOM:
from xml.dom.minidom import getDOMImplementation
impl = getDOMImplementation()
newdoc = impl.createDocument(None, "some_tag", None)
top_element = newdoc.documentElement
text = newdoc.createTextNode('Some textual content.')
top_element.appendChild(text)
Once you have a DOM document object, you can access the parts of your
XML document through its properties and methods. These properties are
defined in the DOM specification. The main property of the document
object is the ‘documentElement’ property. It gives you the main element
in the XML document: the one that holds all others. Here is an example
program:
dom3 = parseString("Some data ")
assert dom3.documentElement.tagName == "myxml"
When you are finished with a DOM tree, you may optionally call the
‘unlink()’ method to encourage early cleanup of the now-unneeded
objects. ‘unlink()’ is an *note xml.dom.minidom: 135.-specific
extension to the DOM API that renders the node and its descendants are
essentially useless. Otherwise, Python’s garbage collector will
eventually take care of the objects in the tree.
See also
........
Document Object Model (DOM) Level 1 Specification(2)
The W3C recommendation for the DOM supported by *note
xml.dom.minidom: 135.
* Menu:
* DOM Objects::
* DOM Example::
* minidom and the DOM standard::
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/xml/dom/minidom.py
(2) https://www.w3.org/TR/REC-DOM-Level-1/
File: python.info, Node: DOM Objects, Next: DOM Example, Up: xml dom minidom — Minimal DOM implementation
5.20.7.1 DOM Objects
....................
The definition of the DOM API for Python is given as part of the *note
xml.dom: 134. module documentation. This section lists the differences
between the API and *note xml.dom.minidom: 135.
-- Method: Node.unlink ()
Break internal references within the DOM so that it will be garbage
collected on versions of Python without cyclic GC. Even when cyclic
GC is available, using this can make large amounts of memory
available sooner, so calling this on DOM objects as soon as they
are no longer needed is good practice. This only needs to be
called on the ‘Document’ object, but may be called on child nodes
to discard children of that node.
You can avoid calling this method explicitly by using the *note
with: 6e9. statement. The following code will automatically unlink
`dom' when the ‘with’ block is exited:
with xml.dom.minidom.parse(datasource) as dom:
... # Work with dom.
-- Method: Node.writexml (writer, indent="", addindent="", newl="")
Write XML to the writer object. The writer receives texts but not
bytes as input, it should have a ‘write()’ method which matches
that of the file object interface. The `indent' parameter is the
indentation of the current node. The `addindent' parameter is the
incremental indentation to use for subnodes of the current one.
The `newl' parameter specifies the string to use to terminate
newlines.
For the ‘Document’ node, an additional keyword argument `encoding'
can be used to specify the encoding field of the XML header.
Changed in version 3.8: The *note writexml(): 27db. method now
preserves the attribute order specified by the user.
-- Method: Node.toxml (encoding=None)
Return a string or byte string containing the XML represented by
the DOM node.
With an explicit `encoding' (1) argument, the result is a byte
string in the specified encoding. With no `encoding' argument, the
result is a Unicode string, and the XML declaration in the
resulting string does not specify an encoding. Encoding this
string in an encoding other than UTF-8 is likely incorrect, since
UTF-8 is the default encoding of XML.
Changed in version 3.8: The *note toxml(): 27dc. method now
preserves the attribute order specified by the user.
-- Method: Node.toprettyxml (indent="\t", newl="\n", encoding=None)
Return a pretty-printed version of the document. `indent'
specifies the indentation string and defaults to a tabulator;
`newl' specifies the string emitted at the end of each line and
defaults to ‘\n’.
The `encoding' argument behaves like the corresponding argument of
*note toxml(): 27dc.
Changed in version 3.8: The *note toprettyxml(): 27dd. method now
preserves the attribute order specified by the user.
---------- Footnotes ----------
(1) (1) The encoding name included in the XML output should conform
to the appropriate standards. For example, “UTF-8” is valid, but “UTF8”
is not valid in an XML document’s declaration, even though Python
accepts it as an encoding name. See
‘https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl’ and
‘https://www.iana.org/assignments/character-sets/character-sets.xhtml’.
File: python.info, Node: DOM Example, Next: minidom and the DOM standard, Prev: DOM Objects, Up: xml dom minidom — Minimal DOM implementation
5.20.7.2 DOM Example
....................
This example program is a fairly realistic example of a simple program.
In this particular case, we do not take much advantage of the
flexibility of the DOM.
import xml.dom.minidom
document = """\
Demo slideshow
Slide title
This is a demo
Of a program for processing slides
Another demo slide
It is important
To have more than
one slide
"""
dom = xml.dom.minidom.parseString(document)
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def handleSlideshow(slideshow):
print("")
handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
slides = slideshow.getElementsByTagName("slide")
handleToc(slides)
handleSlides(slides)
print("")
def handleSlides(slides):
for slide in slides:
handleSlide(slide)
def handleSlide(slide):
handleSlideTitle(slide.getElementsByTagName("title")[0])
handlePoints(slide.getElementsByTagName("point"))
def handleSlideshowTitle(title):
print("%s " % getText(title.childNodes))
def handleSlideTitle(title):
print("%s " % getText(title.childNodes))
def handlePoints(points):
print("")
for point in points:
handlePoint(point)
print(" ")
def handlePoint(point):
print("%s " % getText(point.childNodes))
def handleToc(slides):
for slide in slides:
title = slide.getElementsByTagName("title")[0]
print("%s
" % getText(title.childNodes))
handleSlideshow(dom)
File: python.info, Node: minidom and the DOM standard, Prev: DOM Example, Up: xml dom minidom — Minimal DOM implementation
5.20.7.3 minidom and the DOM standard
.....................................
The *note xml.dom.minidom: 135. module is essentially a DOM
1.0-compatible DOM with some DOM 2 features (primarily namespace
features).
Usage of the DOM interface in Python is straight-forward. The following
mapping rules apply:
* Interfaces are accessed through instance objects. Applications
should not instantiate the classes themselves; they should use the
creator functions available on the ‘Document’ object. Derived
interfaces support all operations (and attributes) from the base
interfaces, plus any new operations.
* Operations are used as methods. Since the DOM uses only *note in:
7a3. parameters, the arguments are passed in normal order (from
left to right). There are no optional arguments. ‘void’
operations return ‘None’.
* IDL attributes map to instance attributes. For compatibility with
the OMG IDL language mapping for Python, an attribute ‘foo’ can
also be accessed through accessor methods ‘_get_foo()’ and
‘_set_foo()’. ‘readonly’ attributes must not be changed; this is
not enforced at runtime.
* The types ‘short int’, ‘unsigned int’, ‘unsigned long long’, and
‘boolean’ all map to Python integer objects.
* The type ‘DOMString’ maps to Python strings. *note
xml.dom.minidom: 135. supports either bytes or strings, but will
normally produce strings. Values of type ‘DOMString’ may also be
‘None’ where allowed to have the IDL ‘null’ value by the DOM
specification from the W3C.
* ‘const’ declarations map to variables in their respective scope
(e.g. ‘xml.dom.minidom.Node.PROCESSING_INSTRUCTION_NODE’); they
must not be changed.
* ‘DOMException’ is currently not supported in *note xml.dom.minidom:
135. Instead, *note xml.dom.minidom: 135. uses standard Python
exceptions such as *note TypeError: 192. and *note AttributeError:
39b.
* ‘NodeList’ objects are implemented using Python’s built-in list
type. These objects provide the interface defined in the DOM
specification, but with earlier versions of Python they do not
support the official API. They are, however, much more “Pythonic”
than the interface defined in the W3C recommendations.
The following interfaces have no implementation in *note
xml.dom.minidom: 135.:
* ‘DOMTimeStamp’
* ‘EntityReference’
Most of these reflect information in the XML document that is not of
general utility to most DOM users.
File: python.info, Node: xml dom pulldom — Support for building partial DOM trees, Next: xml sax — Support for SAX2 parsers, Prev: xml dom minidom — Minimal DOM implementation, Up: Structured Markup Processing Tools
5.20.8 ‘xml.dom.pulldom’ — Support for building partial DOM trees
-----------------------------------------------------------------
`Source code:' Lib/xml/dom/pulldom.py(1)
__________________________________________________________________
The *note xml.dom.pulldom: 136. module provides a “pull parser” which
can also be asked to produce DOM-accessible fragments of the document
where necessary. The basic concept involves pulling “events” from a
stream of incoming XML and processing them. In contrast to SAX which
also employs an event-driven processing model together with callbacks,
the user of a pull parser is responsible for explicitly pulling events
from the stream, looping over those events until either processing is
finished or an error condition occurs.
Warning: The *note xml.dom.pulldom: 136. module is not secure
against maliciously constructed data. If you need to parse
untrusted or unauthenticated data see *note XML vulnerabilities:
26f5.
Changed in version 3.7.1: The SAX parser no longer processes general
external entities by default to increase security by default. To enable
processing of external entities, pass a custom parser instance in:
from xml.dom.pulldom import parse
from xml.sax import make_parser
from xml.sax.handler import feature_external_ges
parser = make_parser()
parser.setFeature(feature_external_ges, True)
parse(filename, parser=parser)
Example:
from xml.dom import pulldom
doc = pulldom.parse('sales_items.xml')
for event, node in doc:
if event == pulldom.START_ELEMENT and node.tagName == 'item':
if int(node.getAttribute('price')) > 50:
doc.expandNode(node)
print(node.toxml())
‘event’ is a constant and can be one of:
* ‘START_ELEMENT’
* ‘END_ELEMENT’
* ‘COMMENT’
* ‘START_DOCUMENT’
* ‘END_DOCUMENT’
* ‘CHARACTERS’
* ‘PROCESSING_INSTRUCTION’
* ‘IGNORABLE_WHITESPACE’
‘node’ is an object of type ‘xml.dom.minidom.Document’,
‘xml.dom.minidom.Element’ or ‘xml.dom.minidom.Text’.
Since the document is treated as a “flat” stream of events, the document
“tree” is implicitly traversed and the desired elements are found
regardless of their depth in the tree. In other words, one does not
need to consider hierarchical issues such as recursive searching of the
document nodes, although if the context of elements were important, one
would either need to maintain some context-related state (i.e.
remembering where one is in the document at any given point) or to make
use of the *note DOMEventStream.expandNode(): 27e4. method and switch to
DOM-related processing.
-- Class: xml.dom.pulldom.PullDom (documentFactory=None)
Subclass of *note xml.sax.handler.ContentHandler: 27e6.
-- Class: xml.dom.pulldom.SAX2DOM (documentFactory=None)
Subclass of *note xml.sax.handler.ContentHandler: 27e6.
-- Function: xml.dom.pulldom.parse (stream_or_string, parser=None,
bufsize=None)
Return a *note DOMEventStream: 27d. from the given input.
`stream_or_string' may be either a file name, or a file-like
object. `parser', if given, must be an *note XMLReader: 27e9.
object. This function will change the document handler of the
parser and activate namespace support; other parser configuration
(like setting an entity resolver) must have been done in advance.
If you have XML in a string, you can use the *note parseString(): 27ea.
function instead:
-- Function: xml.dom.pulldom.parseString (string, parser=None)
Return a *note DOMEventStream: 27d. that represents the (Unicode)
`string'.
-- Data: xml.dom.pulldom.default_bufsize
Default value for the `bufsize' parameter to *note parse(): 27e8.
The value of this variable can be changed before calling *note
parse(): 27e8. and the new value will take effect.
* Menu:
* DOMEventStream Objects::
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/xml/dom/pulldom.py
File: python.info, Node: DOMEventStream Objects, Up: xml dom pulldom — Support for building partial DOM trees
5.20.8.1 DOMEventStream Objects
...............................
-- Class: xml.dom.pulldom.DOMEventStream (stream, parser, bufsize)
Deprecated since version 3.8: Support for *note sequence protocol:
27c. is deprecated.
-- Method: getEvent ()
Return a tuple containing `event' and the current `node' as
‘xml.dom.minidom.Document’ if event equals ‘START_DOCUMENT’,
‘xml.dom.minidom.Element’ if event equals ‘START_ELEMENT’ or
‘END_ELEMENT’ or ‘xml.dom.minidom.Text’ if event equals
‘CHARACTERS’. The current node does not contain information
about its children, unless *note expandNode(): 27e4. is
called.
-- Method: expandNode (node)
Expands all children of `node' into `node'. Example:
from xml.dom import pulldom
xml = 'Foo Some text
and more
'
doc = pulldom.parseString(xml)
for event, node in doc:
if event == pulldom.START_ELEMENT and node.tagName == 'p':
# Following statement only prints '
'
print(node.toxml())
doc.expandNode(node)
# Following statement prints node with all its children 'Some text
and more
'
print(node.toxml())
-- Method: reset ()
File: python.info, Node: xml sax — Support for SAX2 parsers, Next: xml sax handler — Base classes for SAX handlers, Prev: xml dom pulldom — Support for building partial DOM trees, Up: Structured Markup Processing Tools
5.20.9 ‘xml.sax’ — Support for SAX2 parsers
-------------------------------------------
`Source code:' Lib/xml/sax/__init__.py(1)
__________________________________________________________________
The *note xml.sax: 13b. package provides a number of modules which
implement the Simple API for XML (SAX) interface for Python. The
package itself provides the SAX exceptions and the convenience functions
which will be most used by users of the SAX API.
Warning: The *note xml.sax: 13b. module is not secure against
maliciously constructed data. If you need to parse untrusted or
unauthenticated data see *note XML vulnerabilities: 26f5.
Changed in version 3.7.1: The SAX parser no longer processes general
external entities by default to increase security. Before, the parser
created network connections to fetch remote files or loaded local files
from the file system for DTD and entities. The feature can be enabled
again with method *note setFeature(): 27f2. on the parser object and
argument *note feature_external_ges: 27f3.
The convenience functions are:
-- Function: xml.sax.make_parser (parser_list=[])
Create and return a SAX *note XMLReader: 27e9. object. The first
parser found will be used. If `parser_list' is provided, it must
be an iterable of strings which name modules that have a function
named ‘create_parser()’. Modules listed in `parser_list' will be
used before modules in the default list of parsers.
Changed in version 3.8: The `parser_list' argument can be any
iterable, not just a list.
-- Function: xml.sax.parse (filename_or_stream, handler,
error_handler=handler.ErrorHandler())
Create a SAX parser and use it to parse a document. The document,
passed in as `filename_or_stream', can be a filename or a file
object. The `handler' parameter needs to be a SAX *note
ContentHandler: 27e6. instance. If `error_handler' is given, it
must be a SAX *note ErrorHandler: 27f6. instance; if omitted, *note
SAXParseException: 27f7. will be raised on all errors. There is no
return value; all work must be done by the `handler' passed in.
-- Function: xml.sax.parseString (string, handler,
error_handler=handler.ErrorHandler())
Similar to *note parse(): 27f5, but parses from a buffer `string'
received as a parameter. `string' must be a *note str: 330.
instance or a *note bytes-like object: 5e8.
Changed in version 3.5: Added support of *note str: 330. instances.
A typical SAX application uses three kinds of objects: readers, handlers
and input sources. “Reader” in this context is another term for parser,
i.e. some piece of code that reads the bytes or characters from the
input source, and produces a sequence of events. The events then get
distributed to the handler objects, i.e. the reader invokes a method on
the handler. A SAX application must therefore obtain a reader object,
create or open the input sources, create the handlers, and connect these
objects all together. As the final step of preparation, the reader is
called to parse the input. During parsing, methods on the handler
objects are called based on structural and syntactic events from the
input data.
For these objects, only the interfaces are relevant; they are normally
not instantiated by the application itself. Since Python does not have
an explicit notion of interface, they are formally introduced as
classes, but applications may use implementations which do not inherit
from the provided classes. The *note InputSource: 792, *note Locator:
27f8, ‘Attributes’, ‘AttributesNS’, and *note XMLReader: 27e9.
interfaces are defined in the module *note xml.sax.xmlreader: 13e. The
handler interfaces are defined in *note xml.sax.handler: 13c. For
convenience, *note InputSource: 792. (which is often instantiated
directly) and the handler classes are also available from *note xml.sax:
13b. These interfaces are described below.
In addition to these classes, *note xml.sax: 13b. provides the following
exception classes.
-- Exception: xml.sax.SAXException (msg, exception=None)
Encapsulate an XML error or warning. This class can contain basic
error or warning information from either the XML parser or the
application: it can be subclassed to provide additional
functionality or to add localization. Note that although the
handlers defined in the *note ErrorHandler: 27f6. interface receive
instances of this exception, it is not required to actually raise
the exception — it is also useful as a container for information.
When instantiated, `msg' should be a human-readable description of
the error. The optional `exception' parameter, if given, should be
‘None’ or an exception that was caught by the parsing code and is
being passed along as information.
This is the base class for the other SAX exception classes.
-- Exception: xml.sax.SAXParseException (msg, exception, locator)
Subclass of *note SAXException: 27f9. raised on parse errors.
Instances of this class are passed to the methods of the SAX *note
ErrorHandler: 27f6. interface to provide information about the
parse error. This class supports the SAX *note Locator: 27f8.
interface as well as the *note SAXException: 27f9. interface.
-- Exception: xml.sax.SAXNotRecognizedException (msg, exception=None)
Subclass of *note SAXException: 27f9. raised when a SAX *note
XMLReader: 27e9. is confronted with an unrecognized feature or
property. SAX applications and extensions may use this class for
similar purposes.
-- Exception: xml.sax.SAXNotSupportedException (msg, exception=None)
Subclass of *note SAXException: 27f9. raised when a SAX *note
XMLReader: 27e9. is asked to enable a feature that is not
supported, or to set a property to a value that the implementation
does not support. SAX applications and extensions may use this
class for similar purposes.
See also
........
SAX: The Simple API for XML(2)
This site is the focal point for the definition of the SAX API. It
provides a Java implementation and online documentation. Links to
implementations and historical information are also available.
Module *note xml.sax.handler: 13c.
Definitions of the interfaces for application-provided objects.
Module *note xml.sax.saxutils: 13d.
Convenience functions for use in SAX applications.
Module *note xml.sax.xmlreader: 13e.
Definitions of the interfaces for parser-provided objects.
* Menu:
* SAXException Objects::
---------- Footnotes ----------
(1)
https://github.com/python/cpython/tree/3.8/Lib/xml/sax/__init__.py
(2) http://www.saxproject.org/
File: python.info, Node: SAXException Objects, Up: xml sax — Support for SAX2 parsers
5.20.9.1 SAXException Objects
.............................
The *note SAXException: 27f9. exception class supports the following
methods:
-- Method: SAXException.getMessage ()
Return a human-readable message describing the error condition.
-- Method: SAXException.getException ()
Return an encapsulated exception object, or ‘None’.
File: python.info, Node: xml sax handler — Base classes for SAX handlers, Next: xml sax saxutils — SAX Utilities, Prev: xml sax — Support for SAX2 parsers, Up: Structured Markup Processing Tools
5.20.10 ‘xml.sax.handler’ — Base classes for SAX handlers
---------------------------------------------------------
`Source code:' Lib/xml/sax/handler.py(1)
__________________________________________________________________
The SAX API defines four kinds of handlers: content handlers, DTD
handlers, error handlers, and entity resolvers. Applications normally
only need to implement those interfaces whose events they are interested
in; they can implement the interfaces in a single object or in multiple
objects. Handler implementations should inherit from the base classes
provided in the module *note xml.sax.handler: 13c, so that all methods
get default implementations.
-- Class: xml.sax.handler.ContentHandler
This is the main callback interface in SAX, and the one most
important to applications. The order of events in this interface
mirrors the order of the information in the document.
-- Class: xml.sax.handler.DTDHandler
Handle DTD events.
This interface specifies only those DTD events required for basic
parsing (unparsed entities and attributes).
-- Class: xml.sax.handler.EntityResolver
Basic interface for resolving entities. If you create an object
implementing this interface, then register the object with your
Parser, the parser will call the method in your object to resolve
all external entities.
-- Class: xml.sax.handler.ErrorHandler
Interface used by the parser to present error and warning messages
to the application. The methods of this object control whether
errors are immediately converted to exceptions or are handled in
some other way.
In addition to these classes, *note xml.sax.handler: 13c. provides
symbolic constants for the feature and property names.
-- Data: xml.sax.handler.feature_namespaces
value: ‘"http://xml.org/sax/features/namespaces"’
true: Perform Namespace processing.
false: Optionally do not perform Namespace processing (implies namespace-prefixes; default).
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.feature_namespace_prefixes
value: ‘"http://xml.org/sax/features/namespace-prefixes"’
true: Report the original prefixed names and attributes used for Namespace declarations.
false: Do not report attributes used for Namespace declarations, and optionally do not report original prefixed names (default).
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.feature_string_interning
value: ‘"http://xml.org/sax/features/string-interning"’
true: All element names, prefixes, attribute names, Namespace URIs, and local names are interned using the built-in intern function.
false: Names are not necessarily interned, although they may be (default).
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.feature_validation
value: ‘"http://xml.org/sax/features/validation"’
true: Report all validation errors (implies external-general-entities and external-parameter-entities).
false: Do not report validation errors.
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.feature_external_ges
value: ‘"http://xml.org/sax/features/external-general-entities"’
true: Include all external general (text) entities.
false: Do not include external general entities.
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.feature_external_pes
value: ‘"http://xml.org/sax/features/external-parameter-entities"’
true: Include all external parameter entities, including the external DTD subset.
false: Do not include any external parameter entities, even the external DTD subset.
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.all_features
List of all features.
-- Data: xml.sax.handler.property_lexical_handler
value: ‘"http://xml.org/sax/properties/lexical-handler"’
data type: xml.sax.sax2lib.LexicalHandler (not supported in Python 2)
description: An optional extension handler for lexical events like comments.
access: read/write
-- Data: xml.sax.handler.property_declaration_handler
value: ‘"http://xml.org/sax/properties/declaration-handler"’
data type: xml.sax.sax2lib.DeclHandler (not supported in Python 2)
description: An optional extension handler for DTD-related events other than notations and unparsed entities.
access: read/write
-- Data: xml.sax.handler.property_dom_node
value: ‘"http://xml.org/sax/properties/dom-node"’
data type: org.w3c.dom.Node (not supported in Python 2)
description: When parsing, the current DOM node being visited if this is a DOM iterator; when not parsing, the root DOM node for iteration.
access: (parsing) read-only; (not parsing) read/write
-- Data: xml.sax.handler.property_xml_string
value: ‘"http://xml.org/sax/properties/xml-string"’
data type: String
description: The literal string of characters that was the source for the current event.
access: read-only
-- Data: xml.sax.handler.all_properties
List of all known property names.
* Menu:
* ContentHandler Objects::
* DTDHandler Objects::
* EntityResolver Objects::
* ErrorHandler Objects::
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/xml/sax/handler.py
File: python.info, Node: ContentHandler Objects, Next: DTDHandler Objects, Up: xml sax handler — Base classes for SAX handlers
5.20.10.1 ContentHandler Objects
................................
Users are expected to subclass *note ContentHandler: 27e6. to support
their application. The following methods are called by the parser on
the appropriate events in the input document:
-- Method: ContentHandler.setDocumentLocator (locator)
Called by the parser to give the application a locator for locating
the origin of document events.
SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply the
locator to the application by invoking this method before invoking
any of the other methods in the DocumentHandler interface.
The locator allows the application to determine the end position of
any document-related event, even if the parser is not reporting an
error. Typically, the application will use this information for
reporting its own errors (such as character content that does not
match an application’s business rules). The information returned
by the locator is probably not sufficient for use with a search
engine.
Note that the locator will return correct information only during
the invocation of the events in this interface. The application
should not attempt to use it at any other time.
-- Method: ContentHandler.startDocument ()
Receive notification of the beginning of a document.
The SAX parser will invoke this method only once, before any other
methods in this interface or in DTDHandler (except for *note
setDocumentLocator(): 2811.).
-- Method: ContentHandler.endDocument ()
Receive notification of the end of a document.
The SAX parser will invoke this method only once, and it will be
the last method invoked during the parse. The parser shall not
invoke this method until it has either abandoned parsing (because
of an unrecoverable error) or reached the end of input.
-- Method: ContentHandler.startPrefixMapping (prefix, uri)
Begin the scope of a prefix-URI Namespace mapping.
The information from this event is not necessary for normal
Namespace processing: the SAX XML reader will automatically replace
prefixes for element and attribute names when the
‘feature_namespaces’ feature is enabled (the default).
There are cases, however, when applications need to use prefixes in
character data or in attribute values, where they cannot safely be
expanded automatically; the *note startPrefixMapping(): 2814. and
*note endPrefixMapping(): 2815. events supply the information to
the application to expand prefixes in those contexts itself, if
necessary.
Note that *note startPrefixMapping(): 2814. and *note
endPrefixMapping(): 2815. events are not guaranteed to be properly
nested relative to each-other: all *note startPrefixMapping():
2814. events will occur before the corresponding *note
startElement(): 2816. event, and all *note endPrefixMapping():
2815. events will occur after the corresponding *note endElement():
2817. event, but their order is not guaranteed.
-- Method: ContentHandler.endPrefixMapping (prefix)
End the scope of a prefix-URI mapping.
See *note startPrefixMapping(): 2814. for details. This event will
always occur after the corresponding *note endElement(): 2817.
event, but the order of *note endPrefixMapping(): 2815. events is
not otherwise guaranteed.
-- Method: ContentHandler.startElement (name, attrs)
Signals the start of an element in non-namespace mode.
The `name' parameter contains the raw XML 1.0 name of the element
type as a string and the `attrs' parameter holds an object of the
‘Attributes’ interface (see *note The Attributes Interface: 2818.)
containing the attributes of the element. The object passed as
`attrs' may be re-used by the parser; holding on to a reference to
it is not a reliable way to keep a copy of the attributes. To keep
a copy of the attributes, use the *note copy(): 26. method of the
`attrs' object.
-- Method: ContentHandler.endElement (name)
Signals the end of an element in non-namespace mode.
The `name' parameter contains the name of the element type, just as
with the *note startElement(): 2816. event.
-- Method: ContentHandler.startElementNS (name, qname, attrs)
Signals the start of an element in namespace mode.
The `name' parameter contains the name of the element type as a
‘(uri, localname)’ tuple, the `qname' parameter contains the raw
XML 1.0 name used in the source document, and the `attrs' parameter
holds an instance of the ‘AttributesNS’ interface (see *note The
AttributesNS Interface: 281a.) containing the attributes of the
element. If no namespace is associated with the element, the `uri'
component of `name' will be ‘None’. The object passed as `attrs'
may be re-used by the parser; holding on to a reference to it is
not a reliable way to keep a copy of the attributes. To keep a
copy of the attributes, use the *note copy(): 26. method of the
`attrs' object.
Parsers may set the `qname' parameter to ‘None’, unless the
‘feature_namespace_prefixes’ feature is activated.
-- Method: ContentHandler.endElementNS (name, qname)
Signals the end of an element in namespace mode.
The `name' parameter contains the name of the element type, just as
with the *note startElementNS(): 2819. method, likewise the `qname'
parameter.
-- Method: ContentHandler.characters (content)
Receive notification of character data.
The Parser will call this method to report each chunk of character
data. SAX parsers may return all contiguous character data in a
single chunk, or they may split it into several chunks; however,
all of the characters in any single event must come from the same
external entity so that the Locator provides useful information.
`content' may be a string or bytes instance; the ‘expat’ reader
module always produces strings.
Note: The earlier SAX 1 interface provided by the Python XML
Special Interest Group used a more Java-like interface for
this method. Since most parsers used from Python did not take
advantage of the older interface, the simpler signature was
chosen to replace it. To convert old code to the new
interface, use `content' instead of slicing content with the
old `offset' and `length' parameters.
-- Method: ContentHandler.ignorableWhitespace (whitespace)
Receive notification of ignorable whitespace in element content.
Validating Parsers must use this method to report each chunk of
ignorable whitespace (see the W3C XML 1.0 recommendation, section
2.10): non-validating parsers may also use this method if they are
capable of parsing and using content models.
SAX parsers may return all contiguous whitespace in a single chunk,
or they may split it into several chunks; however, all of the
characters in any single event must come from the same external
entity, so that the Locator provides useful information.
-- Method: ContentHandler.processingInstruction (target, data)
Receive notification of a processing instruction.
The Parser will invoke this method once for each processing
instruction found: note that processing instructions may occur
before or after the main document element.
A SAX parser should never report an XML declaration (XML 1.0,
section 2.8) or a text declaration (XML 1.0, section 4.3.1) using
this method.
-- Method: ContentHandler.skippedEntity (name)
Receive notification of a skipped entity.
The Parser will invoke this method once for each entity skipped.
Non-validating processors may skip entities if they have not seen
the declarations (because, for example, the entity was declared in
an external DTD subset). All processors may skip external
entities, depending on the values of the ‘feature_external_ges’ and
the ‘feature_external_pes’ properties.
File: python.info, Node: DTDHandler Objects, Next: EntityResolver Objects, Prev: ContentHandler Objects, Up: xml sax handler — Base classes for SAX handlers
5.20.10.2 DTDHandler Objects
............................
*note DTDHandler: 2802. instances provide the following methods:
-- Method: DTDHandler.notationDecl (name, publicId, systemId)
Handle a notation declaration event.
-- Method: DTDHandler.unparsedEntityDecl (name, publicId, systemId,
ndata)
Handle an unparsed entity declaration event.
File: python.info, Node: EntityResolver Objects, Next: ErrorHandler Objects, Prev: DTDHandler Objects, Up: xml sax handler — Base classes for SAX handlers
5.20.10.3 EntityResolver Objects
................................
-- Method: EntityResolver.resolveEntity (publicId, systemId)
Resolve the system identifier of an entity and return either the
system identifier to read from as a string, or an InputSource to
read from. The default implementation returns `systemId'.
File: python.info, Node: ErrorHandler Objects, Prev: EntityResolver Objects, Up: xml sax handler — Base classes for SAX handlers
5.20.10.4 ErrorHandler Objects
..............................
Objects with this interface are used to receive error and warning
information from the *note XMLReader: 27e9. If you create an object
that implements this interface, then register the object with your *note
XMLReader: 27e9, the parser will call the methods in your object to
report all warnings and errors. There are three levels of errors
available: warnings, (possibly) recoverable errors, and unrecoverable
errors. All methods take a ‘SAXParseException’ as the only parameter.
Errors and warnings may be converted to an exception by raising the
passed-in exception object.
-- Method: ErrorHandler.error (exception)
Called when the parser encounters a recoverable error. If this
method does not raise an exception, parsing may continue, but
further document information should not be expected by the
application. Allowing the parser to continue may allow additional
errors to be discovered in the input document.
-- Method: ErrorHandler.fatalError (exception)
Called when the parser encounters an error it cannot recover from;
parsing is expected to terminate when this method returns.
-- Method: ErrorHandler.warning (exception)
Called when the parser presents minor warning information to the
application. Parsing is expected to continue when this method
returns, and document information will continue to be passed to the
application. Raising an exception in this method will cause
parsing to end.
File: python.info, Node: xml sax saxutils — SAX Utilities, Next: xml sax xmlreader — Interface for XML parsers, Prev: xml sax handler — Base classes for SAX handlers, Up: Structured Markup Processing Tools
5.20.11 ‘xml.sax.saxutils’ — SAX Utilities
------------------------------------------
`Source code:' Lib/xml/sax/saxutils.py(1)
__________________________________________________________________
The module *note xml.sax.saxutils: 13d. contains a number of classes and
functions that are commonly useful when creating SAX applications,
either in direct use, or as base classes.
-- Function: xml.sax.saxutils.escape (data, entities={})
Escape ‘'&'’, ‘'<'’, and ‘'>'’ in a string of data.
You can escape other strings of data by passing a dictionary as the
optional `entities' parameter. The keys and values must all be
strings; each key will be replaced with its corresponding value.
The characters ‘'&'’, ‘'<'’ and ‘'>'’ are always escaped, even if
`entities' is provided.
-- Function: xml.sax.saxutils.unescape (data, entities={})
Unescape ‘'&'’, ‘'<'’, and ‘'>'’ in a string of data.
You can unescape other strings of data by passing a dictionary as
the optional `entities' parameter. The keys and values must all be
strings; each key will be replaced with its corresponding value.
‘'&'’, ‘'<'’, and ‘'>'’ are always unescaped, even if
`entities' is provided.
-- Function: xml.sax.saxutils.quoteattr (data, entities={})
Similar to *note escape(): 282e, but also prepares `data' to be
used as an attribute value. The return value is a quoted version
of `data' with any additional required replacements. *note
quoteattr(): 2830. will select a quote character based on the
content of `data', attempting to avoid encoding any quote
characters in the string. If both single- and double-quote
characters are already in `data', the double-quote characters will
be encoded and `data' will be wrapped in double-quotes. The
resulting string can be used directly as an attribute value:
>>> print("" % quoteattr("ab ' cd \" ef"))
This function is useful when generating attribute values for HTML
or any SGML using the reference concrete syntax.
-- Class: xml.sax.saxutils.XMLGenerator (out=None,
encoding='iso-8859-1', short_empty_elements=False)
This class implements the *note ContentHandler: 27e6. interface by
writing SAX events back into an XML document. In other words,
using an *note XMLGenerator: 2831. as the content handler will
reproduce the original document being parsed. `out' should be a
file-like object which will default to `sys.stdout'. `encoding' is
the encoding of the output stream which defaults to ‘'iso-8859-1'’.
`short_empty_elements' controls the formatting of elements that
contain no content: if ‘False’ (the default) they are emitted as a
pair of start/end tags, if set to ‘True’ they are emitted as a
single self-closed tag.
New in version 3.2: The `short_empty_elements' parameter.
-- Class: xml.sax.saxutils.XMLFilterBase (base)
This class is designed to sit between an *note XMLReader: 27e9. and
the client application’s event handlers. By default, it does
nothing but pass requests up to the reader and events on to the
handlers unmodified, but subclasses can override specific methods
to modify the event stream or the configuration requests as they
pass through.
-- Function: xml.sax.saxutils.prepare_input_source (source, base='')
This function takes an input source and an optional base URL and
returns a fully resolved *note InputSource: 792. object ready for
reading. The input source can be given as a string, a file-like
object, or an *note InputSource: 792. object; parsers will use this
function to implement the polymorphic `source' argument to their
‘parse()’ method.
---------- Footnotes ----------
(1)
https://github.com/python/cpython/tree/3.8/Lib/xml/sax/saxutils.py
File: python.info, Node: xml sax xmlreader — Interface for XML parsers, Next: xml parsers expat — Fast XML parsing using Expat, Prev: xml sax saxutils — SAX Utilities, Up: Structured Markup Processing Tools
5.20.12 ‘xml.sax.xmlreader’ — Interface for XML parsers
-------------------------------------------------------
`Source code:' Lib/xml/sax/xmlreader.py(1)
__________________________________________________________________
SAX parsers implement the *note XMLReader: 27e9. interface. They are
implemented in a Python module, which must provide a function
‘create_parser()’. This function is invoked by *note
xml.sax.make_parser(): 27f4. with no arguments to create a new parser
object.
-- Class: xml.sax.xmlreader.XMLReader
Base class which can be inherited by SAX parsers.
-- Class: xml.sax.xmlreader.IncrementalParser
In some cases, it is desirable not to parse an input source at
once, but to feed chunks of the document as they get available.
Note that the reader will normally not read the entire file, but
read it in chunks as well; still ‘parse()’ won’t return until the
entire document is processed. So these interfaces should be used
if the blocking behaviour of ‘parse()’ is not desirable.
When the parser is instantiated it is ready to begin accepting data
from the feed method immediately. After parsing has been finished
with a call to close the reset method must be called to make the
parser ready to accept new data, either from feed or using the
parse method.
Note that these methods must `not' be called during parsing, that
is, after parse has been called and before it returns.
By default, the class also implements the parse method of the
XMLReader interface using the feed, close and reset methods of the
IncrementalParser interface as a convenience to SAX 2.0 driver
writers.
-- Class: xml.sax.xmlreader.Locator
Interface for associating a SAX event with a document location. A
locator object will return valid results only during calls to
DocumentHandler methods; at any other time, the results are
unpredictable. If information is not available, methods may return
‘None’.
-- Class: xml.sax.xmlreader.InputSource (system_id=None)
Encapsulation of the information needed by the *note XMLReader:
27e9. to read entities.
This class may include information about the public identifier,
system identifier, byte stream (possibly with character encoding
information) and/or the character stream of an entity.
Applications will create objects of this class for use in the *note
XMLReader.parse(): 2837. method and for returning from
EntityResolver.resolveEntity.
An *note InputSource: 792. belongs to the application, the *note
XMLReader: 27e9. is not allowed to modify *note InputSource: 792.
objects passed to it from the application, although it may make
copies and modify those.
-- Class: xml.sax.xmlreader.AttributesImpl (attrs)
This is an implementation of the ‘Attributes’ interface (see
section *note The Attributes Interface: 2818.). This is a
dictionary-like object which represents the element attributes in a
‘startElement()’ call. In addition to the most useful dictionary
operations, it supports a number of other methods as described by
the interface. Objects of this class should be instantiated by
readers; `attrs' must be a dictionary-like object containing a
mapping from attribute names to attribute values.
-- Class: xml.sax.xmlreader.AttributesNSImpl (attrs, qnames)
Namespace-aware variant of *note AttributesImpl: 2838, which will
be passed to ‘startElementNS()’. It is derived from *note
AttributesImpl: 2838, but understands attribute names as two-tuples
of `namespaceURI' and `localname'. In addition, it provides a
number of methods expecting qualified names as they appear in the
original document. This class implements the ‘AttributesNS’
interface (see section *note The AttributesNS Interface: 281a.).
* Menu:
* XMLReader Objects::
* IncrementalParser Objects::
* Locator Objects::
* InputSource Objects::
* The Attributes Interface::
* The AttributesNS Interface::
---------- Footnotes ----------
(1)
https://github.com/python/cpython/tree/3.8/Lib/xml/sax/xmlreader.py
File: python.info, Node: XMLReader Objects, Next: IncrementalParser Objects, Up: xml sax xmlreader — Interface for XML parsers
5.20.12.1 XMLReader Objects
...........................
The *note XMLReader: 27e9. interface supports the following methods:
-- Method: XMLReader.parse (source)
Process an input source, producing SAX events. The `source' object
can be a system identifier (a string identifying the input source –
typically a file name or a URL), a *note pathlib.Path: 201. or
*note path-like: 36a. object, or an *note InputSource: 792. object.
When *note parse(): 2837. returns, the input is completely
processed, and the parser object can be discarded or reset.
Changed in version 3.5: Added support of character streams.
Changed in version 3.8: Added support of path-like objects.
-- Method: XMLReader.getContentHandler ()
Return the current *note ContentHandler: 27e6.
-- Method: XMLReader.setContentHandler (handler)
Set the current *note ContentHandler: 27e6. If no *note
ContentHandler: 27e6. is set, content events will be discarded.
-- Method: XMLReader.getDTDHandler ()
Return the current *note DTDHandler: 2802.
-- Method: XMLReader.setDTDHandler (handler)
Set the current *note DTDHandler: 2802. If no *note DTDHandler:
2802. is set, DTD events will be discarded.
-- Method: XMLReader.getEntityResolver ()
Return the current *note EntityResolver: 2803.
-- Method: XMLReader.setEntityResolver (handler)
Set the current *note EntityResolver: 2803. If no *note
EntityResolver: 2803. is set, attempts to resolve an external
entity will result in opening the system identifier for the entity,
and fail if it is not available.
-- Method: XMLReader.getErrorHandler ()
Return the current *note ErrorHandler: 27f6.
-- Method: XMLReader.setErrorHandler (handler)
Set the current error handler. If no *note ErrorHandler: 27f6. is
set, errors will be raised as exceptions, and warnings will be
printed.
-- Method: XMLReader.setLocale (locale)
Allow an application to set the locale for errors and warnings.
SAX parsers are not required to provide localization for errors and
warnings; if they cannot support the requested locale, however,
they must raise a SAX exception. Applications may request a locale
change in the middle of a parse.
-- Method: XMLReader.getFeature (featurename)
Return the current setting for feature `featurename'. If the
feature is not recognized, ‘SAXNotRecognizedException’ is raised.
The well-known featurenames are listed in the module *note
xml.sax.handler: 13c.
-- Method: XMLReader.setFeature (featurename, value)
Set the `featurename' to `value'. If the feature is not
recognized, ‘SAXNotRecognizedException’ is raised. If the feature
or its setting is not supported by the parser,
`SAXNotSupportedException' is raised.
-- Method: XMLReader.getProperty (propertyname)
Return the current setting for property `propertyname'. If the
property is not recognized, a ‘SAXNotRecognizedException’ is
raised. The well-known propertynames are listed in the module
*note xml.sax.handler: 13c.
-- Method: XMLReader.setProperty (propertyname, value)
Set the `propertyname' to `value'. If the property is not
recognized, ‘SAXNotRecognizedException’ is raised. If the property
or its setting is not supported by the parser,
`SAXNotSupportedException' is raised.
File: python.info, Node: IncrementalParser Objects, Next: Locator Objects, Prev: XMLReader Objects, Up: xml sax xmlreader — Interface for XML parsers
5.20.12.2 IncrementalParser Objects
...................................
Instances of *note IncrementalParser: 2836. offer the following
additional methods:
-- Method: IncrementalParser.feed (data)
Process a chunk of `data'.
-- Method: IncrementalParser.close ()
Assume the end of the document. That will check well-formedness
conditions that can be checked only at the end, invoke handlers,
and may clean up resources allocated during parsing.
-- Method: IncrementalParser.reset ()
This method is called after close has been called to reset the
parser so that it is ready to parse new documents. The results of
calling parse or feed after close without calling reset are
undefined.
File: python.info, Node: Locator Objects, Next: InputSource Objects, Prev: IncrementalParser Objects, Up: xml sax xmlreader — Interface for XML parsers
5.20.12.3 Locator Objects
.........................
Instances of *note Locator: 27f8. provide these methods:
-- Method: Locator.getColumnNumber ()
Return the column number where the current event begins.
-- Method: Locator.getLineNumber ()
Return the line number where the current event begins.
-- Method: Locator.getPublicId ()
Return the public identifier for the current event.
-- Method: Locator.getSystemId ()
Return the system identifier for the current event.
File: python.info, Node: InputSource Objects, Next: The Attributes Interface, Prev: Locator Objects, Up: xml sax xmlreader — Interface for XML parsers
5.20.12.4 InputSource Objects
.............................
-- Method: InputSource.setPublicId (id)
Sets the public identifier of this *note InputSource: 792.
-- Method: InputSource.getPublicId ()
Returns the public identifier of this *note InputSource: 792.
-- Method: InputSource.setSystemId (id)
Sets the system identifier of this *note InputSource: 792.
-- Method: InputSource.getSystemId ()
Returns the system identifier of this *note InputSource: 792.
-- Method: InputSource.setEncoding (encoding)
Sets the character encoding of this *note InputSource: 792.
The encoding must be a string acceptable for an XML encoding
declaration (see section 4.3.3 of the XML recommendation).
The encoding attribute of the *note InputSource: 792. is ignored if
the *note InputSource: 792. also contains a character stream.
-- Method: InputSource.getEncoding ()
Get the character encoding of this InputSource.
-- Method: InputSource.setByteStream (bytefile)
Set the byte stream (a *note binary file: 1966.) for this input
source.
The SAX parser will ignore this if there is also a character stream
specified, but it will use a byte stream in preference to opening a
URI connection itself.
If the application knows the character encoding of the byte stream,
it should set it with the setEncoding method.
-- Method: InputSource.getByteStream ()
Get the byte stream for this input source.
The getEncoding method will return the character encoding for this
byte stream, or ‘None’ if unknown.
-- Method: InputSource.setCharacterStream (charfile)
Set the character stream (a *note text file: f3a.) for this input
source.
If there is a character stream specified, the SAX parser will
ignore any byte stream and will not attempt to open a URI
connection to the system identifier.
-- Method: InputSource.getCharacterStream ()
Get the character stream for this input source.
File: python.info, Node: The Attributes Interface, Next: The AttributesNS Interface, Prev: InputSource Objects, Up: xml sax xmlreader — Interface for XML parsers
5.20.12.5 The ‘Attributes’ Interface
....................................
‘Attributes’ objects implement a portion of the *note mapping protocol:
b39, including the methods ‘copy()’, ‘get()’, *note __contains__(): d28,
‘items()’, ‘keys()’, and ‘values()’. The following methods are also
provided:
-- Method: Attributes.getLength ()
Return the number of attributes.
-- Method: Attributes.getNames ()
Return the names of the attributes.
-- Method: Attributes.getType (name)
Returns the type of the attribute `name', which is normally
‘'CDATA'’.
-- Method: Attributes.getValue (name)
Return the value of attribute `name'.
File: python.info, Node: The AttributesNS Interface, Prev: The Attributes Interface, Up: xml sax xmlreader — Interface for XML parsers
5.20.12.6 The ‘AttributesNS’ Interface
......................................
This interface is a subtype of the ‘Attributes’ interface (see section
*note The Attributes Interface: 2818.). All methods supported by that
interface are also available on ‘AttributesNS’ objects.
The following methods are also available:
-- Method: AttributesNS.getValueByQName (name)
Return the value for a qualified name.
-- Method: AttributesNS.getNameByQName (name)
Return the ‘(namespace, localname)’ pair for a qualified `name'.
-- Method: AttributesNS.getQNameByName (name)
Return the qualified name for a ‘(namespace, localname)’ pair.
-- Method: AttributesNS.getQNames ()
Return the qualified names of all attributes.
File: python.info, Node: xml parsers expat — Fast XML parsing using Expat, Prev: xml sax xmlreader — Interface for XML parsers, Up: Structured Markup Processing Tools
5.20.13 ‘xml.parsers.expat’ — Fast XML parsing using Expat
----------------------------------------------------------
__________________________________________________________________
Warning: The ‘pyexpat’ module is not secure against maliciously
constructed data. If you need to parse untrusted or
unauthenticated data see *note XML vulnerabilities: 26f5.
The *note xml.parsers.expat: 138. module is a Python interface to the
Expat non-validating XML parser. The module provides a single extension
type, ‘xmlparser’, that represents the current state of an XML parser.
After an ‘xmlparser’ object has been created, various attributes of the
object can be set to handler functions. When an XML document is then
fed to the parser, the handler functions are called for the character
data and markup in the XML document.
This module uses the ‘pyexpat’ module to provide access to the Expat
parser. Direct use of the ‘pyexpat’ module is deprecated.
This module provides one exception and one type object:
-- Exception: xml.parsers.expat.ExpatError
The exception raised when Expat reports an error. See section
*note ExpatError Exceptions: 286b. for more information on
interpreting Expat errors.
-- Exception: xml.parsers.expat.error
Alias for *note ExpatError: c0d.
-- Data: xml.parsers.expat.XMLParserType
The type of the return values from the *note ParserCreate(): 286e.
function.
The *note xml.parsers.expat: 138. module contains two functions:
-- Function: xml.parsers.expat.ErrorString (errno)
Returns an explanatory string for a given error number `errno'.
-- Function: xml.parsers.expat.ParserCreate (encoding=None,
namespace_separator=None)
Creates and returns a new ‘xmlparser’ object. `encoding', if
specified, must be a string naming the encoding used by the XML
data. Expat doesn’t support as many encodings as Python does, and
its repertoire of encodings can’t be extended; it supports UTF-8,
UTF-16, ISO-8859-1 (Latin1), and ASCII. If `encoding' (1) is given
it will override the implicit or explicit encoding of the document.
Expat can optionally do XML namespace processing for you, enabled
by providing a value for `namespace_separator'. The value must be
a one-character string; a *note ValueError: 1fb. will be raised if
the string has an illegal length (‘None’ is considered the same as
omission). When namespace processing is enabled, element type
names and attribute names that belong to a namespace will be
expanded. The element name passed to the element handlers
‘StartElementHandler’ and ‘EndElementHandler’ will be the
concatenation of the namespace URI, the namespace separator
character, and the local part of the name. If the namespace
separator is a zero byte (‘chr(0)’) then the namespace URI and the
local part will be concatenated without any separator.
For example, if `namespace_separator' is set to a space character
(‘' '’) and the following document is parsed:
‘StartElementHandler’ will receive the following strings for each
element:
http://default-namespace.org/ root
http://www.python.org/ns/ elem1
elem2
Due to limitations in the ‘Expat’ library used by ‘pyexpat’, the
‘xmlparser’ instance returned can only be used to parse a single
XML document. Call ‘ParserCreate’ for each document to provide
unique parser instances.
See also
........
The Expat XML Parser(2)
Home page of the Expat project.
* Menu:
* XMLParser Objects: XMLParser Objects<2>.
* ExpatError Exceptions::
* Example: Example<12>.
* Content Model Descriptions::
* Expat error constants::
---------- Footnotes ----------
(1) (1) The encoding string included in XML output should conform to
the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
not. See
‘https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl’ and
‘https://www.iana.org/assignments/character-sets/character-sets.xhtml’.
(2) http://www.libexpat.org/
File: python.info, Node: XMLParser Objects<2>, Next: ExpatError Exceptions, Up: xml parsers expat — Fast XML parsing using Expat
5.20.13.1 XMLParser Objects
...........................
‘xmlparser’ objects have the following methods:
-- Method: xmlparser.Parse (data[, isfinal])
Parses the contents of the string `data', calling the appropriate
handler functions to process the parsed data. `isfinal' must be
true on the final call to this method; it allows the parsing of a
single file in fragments, not the submission of multiple files.
`data' can be the empty string at any time.
-- Method: xmlparser.ParseFile (file)
Parse XML data reading from the object `file'. `file' only needs
to provide the ‘read(nbytes)’ method, returning the empty string
when there’s no more data.
-- Method: xmlparser.SetBase (base)
Sets the base to be used for resolving relative URIs in system
identifiers in declarations. Resolving relative identifiers is
left to the application: this value will be passed through as the
`base' argument to the *note ExternalEntityRefHandler(): 2875,
*note NotationDeclHandler(): 2876, and *note
UnparsedEntityDeclHandler(): 2877. functions.
-- Method: xmlparser.GetBase ()
Returns a string containing the base set by a previous call to
*note SetBase(): 2874, or ‘None’ if *note SetBase(): 2874. hasn’t
been called.
-- Method: xmlparser.GetInputContext ()
Returns the input data that generated the current event as a
string. The data is in the encoding of the entity which contains
the text. When called while an event handler is not active, the
return value is ‘None’.
-- Method: xmlparser.ExternalEntityParserCreate (context[, encoding])
Create a “child” parser which can be used to parse an external
parsed entity referred to by content parsed by the parent parser.
The `context' parameter should be the string passed to the *note
ExternalEntityRefHandler(): 2875. handler function, described
below. The child parser is created with the *note
ordered_attributes: 287b. and *note specified_attributes: 287c. set
to the values of this parser.
-- Method: xmlparser.SetParamEntityParsing (flag)
Control parsing of parameter entities (including the external DTD
subset). Possible `flag' values are
‘XML_PARAM_ENTITY_PARSING_NEVER’,
‘XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE’ and
‘XML_PARAM_ENTITY_PARSING_ALWAYS’. Return true if setting the flag
was successful.
-- Method: xmlparser.UseForeignDTD ([flag])
Calling this with a true value for `flag' (the default) will cause
Expat to call the *note ExternalEntityRefHandler: 2875. with *note
None: 157. for all arguments to allow an alternate DTD to be
loaded. If the document does not contain a document type
declaration, the *note ExternalEntityRefHandler: 2875. will still
be called, but the *note StartDoctypeDeclHandler: 287f. and *note
EndDoctypeDeclHandler: 2880. will not be called.
Passing a false value for `flag' will cancel a previous call that
passed a true value, but otherwise has no effect.
This method can only be called before the *note Parse(): 2872. or
*note ParseFile(): 2873. methods are called; calling it after
either of those have been called causes *note ExpatError: c0d. to
be raised with the *note code: 1b. attribute set to
‘errors.codes[errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING]’.
‘xmlparser’ objects have the following attributes:
-- Attribute: xmlparser.buffer_size
The size of the buffer used when *note buffer_text: 2882. is true.
A new buffer size can be set by assigning a new integer value to
this attribute. When the size is changed, the buffer will be
flushed.
-- Attribute: xmlparser.buffer_text
Setting this to true causes the ‘xmlparser’ object to buffer
textual content returned by Expat to avoid multiple calls to the
*note CharacterDataHandler(): 2883. callback whenever possible.
This can improve performance substantially since Expat normally
breaks character data into chunks at every line ending. This
attribute is false by default, and may be changed at any time.
-- Attribute: xmlparser.buffer_used
If *note buffer_text: 2882. is enabled, the number of bytes stored
in the buffer. These bytes represent UTF-8 encoded text. This
attribute has no meaningful interpretation when *note buffer_text:
2882. is false.
-- Attribute: xmlparser.ordered_attributes
Setting this attribute to a non-zero integer causes the attributes
to be reported as a list rather than a dictionary. The attributes
are presented in the order found in the document text. For each
attribute, two list entries are presented: the attribute name and
the attribute value. (Older versions of this module also used this
format.) By default, this attribute is false; it may be changed at
any time.
-- Attribute: xmlparser.specified_attributes
If set to a non-zero integer, the parser will report only those
attributes which were specified in the document instance and not
those which were derived from attribute declarations. Applications
which set this need to be especially careful to use what additional
information is available from the declarations as needed to comply
with the standards for the behavior of XML processors. By default,
this attribute is false; it may be changed at any time.
The following attributes contain values relating to the most recent
error encountered by an ‘xmlparser’ object, and will only have correct
values once a call to ‘Parse()’ or ‘ParseFile()’ has raised an *note
xml.parsers.expat.ExpatError: c0d. exception.
-- Attribute: xmlparser.ErrorByteIndex
Byte index at which an error occurred.
-- Attribute: xmlparser.ErrorCode
Numeric code specifying the problem. This value can be passed to
the *note ErrorString(): 286f. function, or compared to one of the
constants defined in the ‘errors’ object.
-- Attribute: xmlparser.ErrorColumnNumber
Column number at which an error occurred.
-- Attribute: xmlparser.ErrorLineNumber
Line number at which an error occurred.
The following attributes contain values relating to the current parse
location in an ‘xmlparser’ object. During a callback reporting a parse
event they indicate the location of the first of the sequence of
characters that generated the event. When called outside of a callback,
the position indicated will be just past the last parse event
(regardless of whether there was an associated callback).
-- Attribute: xmlparser.CurrentByteIndex
Current byte index in the parser input.
-- Attribute: xmlparser.CurrentColumnNumber
Current column number in the parser input.
-- Attribute: xmlparser.CurrentLineNumber
Current line number in the parser input.
Here is the list of handlers that can be set. To set a handler on an
‘xmlparser’ object `o', use ‘o.handlername = func’. `handlername' must
be taken from the following list, and `func' must be a callable object
accepting the correct number of arguments. The arguments are all
strings, unless otherwise stated.
-- Method: xmlparser.XmlDeclHandler (version, encoding, standalone)
Called when the XML declaration is parsed. The XML declaration is
the (optional) declaration of the applicable version of the XML
recommendation, the encoding of the document text, and an optional
“standalone” declaration. `version' and `encoding' will be
strings, and `standalone' will be ‘1’ if the document is declared
standalone, ‘0’ if it is declared not to be standalone, or ‘-1’ if
the standalone clause was omitted. This is only available with
Expat version 1.95.0 or newer.
-- Method: xmlparser.StartDoctypeDeclHandler (doctypeName, systemId,
publicId, has_internal_subset)
Called when Expat begins parsing the document type declaration
(‘'’.
-- Method: xmlparser.StartCdataSectionHandler ()
Called at the start of a CDATA section. This and *note
EndCdataSectionHandler: 2893. are needed to be able to identify the
syntactical start and end for CDATA sections.
-- Method: xmlparser.EndCdataSectionHandler ()
Called at the end of a CDATA section.
-- Method: xmlparser.DefaultHandler (data)
Called for any characters in the XML document for which no
applicable handler has been specified. This means characters that
are part of a construct which could be reported, but for which no
handler has been supplied.
-- Method: xmlparser.DefaultHandlerExpand (data)
This is the same as the *note DefaultHandler(): 2898, but doesn’t
inhibit expansion of internal entities. The entity reference will
not be passed to the default handler.
-- Method: xmlparser.NotStandaloneHandler ()
Called if the XML document hasn’t been declared as being a
standalone document. This happens when there is an external subset
or a reference to a parameter entity, but the XML declaration does
not set standalone to ‘yes’ in an XML declaration. If this handler
returns ‘0’, then the parser will raise an
‘XML_ERROR_NOT_STANDALONE’ error. If this handler is not set, no
exception is raised by the parser for this condition.
-- Method: xmlparser.ExternalEntityRefHandler (context, base, systemId,
publicId)
Called for references to external entities. `base' is the current
base, as set by a previous call to *note SetBase(): 2874. The
public and system identifiers, `systemId' and `publicId', are
strings if given; if the public identifier is not given, `publicId'
will be ‘None’. The `context' value is opaque and should only be
used as described below.
For external entities to be parsed, this handler must be
implemented. It is responsible for creating the sub-parser using
‘ExternalEntityParserCreate(context)’, initializing it with the
appropriate callbacks, and parsing the entity. This handler should
return an integer; if it returns ‘0’, the parser will raise an
‘XML_ERROR_EXTERNAL_ENTITY_HANDLING’ error, otherwise parsing will
continue.
If this handler is not provided, external entities are reported by
the *note DefaultHandler: 2898. callback, if provided.
File: python.info, Node: ExpatError Exceptions, Next: Example<12>, Prev: XMLParser Objects<2>, Up: xml parsers expat — Fast XML parsing using Expat
5.20.13.2 ExpatError Exceptions
...............................
*note ExpatError: c0d. exceptions have a number of interesting
attributes:
-- Attribute: ExpatError.code
Expat’s internal error number for the specific error. The *note
errors.messages: 289d. dictionary maps these error numbers to
Expat’s error messages. For example:
from xml.parsers.expat import ParserCreate, ExpatError, errors
p = ParserCreate()
try:
p.Parse(some_xml_document)
except ExpatError as err:
print("Error:", errors.messages[err.code])
The *note errors: 139. module also provides error message constants
and a dictionary *note codes: 289e. mapping these messages back to
the error codes, see below.
-- Attribute: ExpatError.lineno
Line number on which the error was detected. The first line is
numbered ‘1’.
-- Attribute: ExpatError.offset
Character offset into the line where the error occurred. The first
column is numbered ‘0’.
File: python.info, Node: Example<12>, Next: Content Model Descriptions, Prev: ExpatError Exceptions, Up: xml parsers expat — Fast XML parsing using Expat
5.20.13.3 Example
.................
The following program defines three handlers that just print out their
arguments.
import xml.parsers.expat
# 3 handler functions
def start_element(name, attrs):
print('Start element:', name, attrs)
def end_element(name):
print('End element:', name)
def char_data(data):
print('Character data:', repr(data))
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data
p.Parse("""
Text goes here
More text
""", 1)
The output from this program is:
Start element: parent {'id': 'top'}
Start element: child1 {'name': 'paul'}
Character data: 'Text goes here'
End element: child1
Character data: '\n'
Start element: child2 {'name': 'fred'}
Character data: 'More text'
End element: child2
Character data: '\n'
End element: parent
File: python.info, Node: Content Model Descriptions, Next: Expat error constants, Prev: Example<12>, Up: xml parsers expat — Fast XML parsing using Expat
5.20.13.4 Content Model Descriptions
....................................
Content models are described using nested tuples. Each tuple contains
four values: the type, the quantifier, the name, and a tuple of
children. Children are simply additional content model descriptions.
The values of the first two fields are constants defined in the *note
xml.parsers.expat.model: 13a. module. These constants can be collected
in two groups: the model type group and the quantifier group.
The constants in the model type group are:
-- Data: xml.parsers.expat.model.XML_CTYPE_ANY
The element named by the model name was declared to have a content
model of ‘ANY’.
-- Data: xml.parsers.expat.model.XML_CTYPE_CHOICE
The named element allows a choice from a number of options; this is
used for content models such as ‘(A | B | C)’.
-- Data: xml.parsers.expat.model.XML_CTYPE_EMPTY
Elements which are declared to be ‘EMPTY’ have this model type.
-- Data: xml.parsers.expat.model.XML_CTYPE_MIXED
-- Data: xml.parsers.expat.model.XML_CTYPE_NAME
-- Data: xml.parsers.expat.model.XML_CTYPE_SEQ
Models which represent a series of models which follow one after
the other are indicated with this model type. This is used for
models such as ‘(A, B, C)’.
The constants in the quantifier group are:
-- Data: xml.parsers.expat.model.XML_CQUANT_NONE
No modifier is given, so it can appear exactly once, as for ‘A’.
-- Data: xml.parsers.expat.model.XML_CQUANT_OPT
The model is optional: it can appear once or not at all, as for
‘A?’.
-- Data: xml.parsers.expat.model.XML_CQUANT_PLUS
The model must occur one or more times (like ‘A+’).
-- Data: xml.parsers.expat.model.XML_CQUANT_REP
The model must occur zero or more times, as for ‘A*’.
File: python.info, Node: Expat error constants, Prev: Content Model Descriptions, Up: xml parsers expat — Fast XML parsing using Expat
5.20.13.5 Expat error constants
...............................
The following constants are provided in the *note
xml.parsers.expat.errors: 139. module. These constants are useful in
interpreting some of the attributes of the ‘ExpatError’ exception
objects raised when an error has occurred. Since for backwards
compatibility reasons, the constants’ value is the error `message' and
not the numeric error `code', you do this by comparing its *note code:
1b. attribute with ‘errors.codes[errors.XML_ERROR_`CONSTANT_NAME']’.
The ‘errors’ module has the following attributes:
-- Data: xml.parsers.expat.errors.codes
A dictionary mapping string descriptions to their error codes.
New in version 3.2.
-- Data: xml.parsers.expat.errors.messages
A dictionary mapping numeric error codes to their string
descriptions.
New in version 3.2.
-- Data: xml.parsers.expat.errors.XML_ERROR_ASYNC_ENTITY
-- Data:
xml.parsers.expat.errors.XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
An entity reference in an attribute value referred to an external
entity instead of an internal entity.
-- Data: xml.parsers.expat.errors.XML_ERROR_BAD_CHAR_REF
A character reference referred to a character which is illegal in
XML (for example, character ‘0’, or ‘‘’’).
-- Data: xml.parsers.expat.errors.XML_ERROR_BINARY_ENTITY_REF
An entity reference referred to an entity which was declared with a
notation, so cannot be parsed.
-- Data: xml.parsers.expat.errors.XML_ERROR_DUPLICATE_ATTRIBUTE
An attribute was used more than once in a start tag.
-- Data: xml.parsers.expat.errors.XML_ERROR_INCORRECT_ENCODING
-- Data: xml.parsers.expat.errors.XML_ERROR_INVALID_TOKEN
Raised when an input byte could not properly be assigned to a
character; for example, a NUL byte (value ‘0’) in a UTF-8 input
stream.
-- Data: xml.parsers.expat.errors.XML_ERROR_JUNK_AFTER_DOC_ELEMENT
Something other than whitespace occurred after the document
element.
-- Data: xml.parsers.expat.errors.XML_ERROR_MISPLACED_XML_PI
An XML declaration was found somewhere other than the start of the
input data.
-- Data: xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS
The document contains no elements (XML requires all documents to
contain exactly one top-level element)..
-- Data: xml.parsers.expat.errors.XML_ERROR_NO_MEMORY
Expat was not able to allocate memory internally.
-- Data: xml.parsers.expat.errors.XML_ERROR_PARAM_ENTITY_REF
A parameter entity reference was found where it was not allowed.
-- Data: xml.parsers.expat.errors.XML_ERROR_PARTIAL_CHAR
An incomplete character was found in the input.
-- Data: xml.parsers.expat.errors.XML_ERROR_RECURSIVE_ENTITY_REF
An entity reference contained another reference to the same entity;
possibly via a different name, and possibly indirectly.
-- Data: xml.parsers.expat.errors.XML_ERROR_SYNTAX
Some unspecified syntax error was encountered.
-- Data: xml.parsers.expat.errors.XML_ERROR_TAG_MISMATCH
An end tag did not match the innermost open start tag.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNCLOSED_TOKEN
Some token (such as a start tag) was not closed before the end of
the stream or the next token was encountered.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNDEFINED_ENTITY
A reference was made to an entity which was not defined.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNKNOWN_ENCODING
The document encoding is not supported by Expat.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNCLOSED_CDATA_SECTION
A CDATA marked section was not closed.
-- Data: xml.parsers.expat.errors.XML_ERROR_EXTERNAL_ENTITY_HANDLING
-- Data: xml.parsers.expat.errors.XML_ERROR_NOT_STANDALONE
The parser determined that the document was not “standalone” though
it declared itself to be in the XML declaration, and the
‘NotStandaloneHandler’ was set and returned ‘0’.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNEXPECTED_STATE
-- Data: xml.parsers.expat.errors.XML_ERROR_ENTITY_DECLARED_IN_PE
-- Data: xml.parsers.expat.errors.XML_ERROR_FEATURE_REQUIRES_XML_DTD
An operation was requested that requires DTD support to be compiled
in, but Expat was configured without DTD support. This should
never be reported by a standard build of the *note
xml.parsers.expat: 138. module.
-- Data:
xml.parsers.expat.errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING
A behavioral change was requested after parsing started that can
only be changed before parsing has started. This is (currently)
only raised by ‘UseForeignDTD()’.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNBOUND_PREFIX
An undeclared prefix was found when namespace processing was
enabled.
-- Data: xml.parsers.expat.errors.XML_ERROR_UNDECLARING_PREFIX
The document attempted to remove the namespace declaration
associated with a prefix.
-- Data: xml.parsers.expat.errors.XML_ERROR_INCOMPLETE_PE
A parameter entity contained incomplete markup.
-- Data: xml.parsers.expat.errors.XML_ERROR_XML_DECL
The document contained no document element at all.
-- Data: xml.parsers.expat.errors.XML_ERROR_TEXT_DECL
There was an error parsing a text declaration in an external
entity.
-- Data: xml.parsers.expat.errors.XML_ERROR_PUBLICID
Characters were found in the public id that are not allowed.
-- Data: xml.parsers.expat.errors.XML_ERROR_SUSPENDED
The requested operation was made on a suspended parser, but isn’t
allowed. This includes attempts to provide additional input or to
stop the parser.
-- Data: xml.parsers.expat.errors.XML_ERROR_NOT_SUSPENDED
An attempt to resume the parser was made when the parser had not
been suspended.
-- Data: xml.parsers.expat.errors.XML_ERROR_ABORTED
This should not be reported to Python applications.
-- Data: xml.parsers.expat.errors.XML_ERROR_FINISHED
The requested operation was made on a parser which was finished
parsing input, but isn’t allowed. This includes attempts to
provide additional input or to stop the parser.
-- Data: xml.parsers.expat.errors.XML_ERROR_SUSPEND_PE
File: python.info, Node: Internet Protocols and Support, Next: Multimedia Services, Prev: Structured Markup Processing Tools, Up: The Python Standard Library
5.21 Internet Protocols and Support
===================================
The modules described in this chapter implement Internet protocols and
support for related technology. They are all implemented in Python.
Most of these modules require the presence of the system-dependent
module *note socket: ef, which is currently supported on most popular
platforms. Here is an overview:
* Menu:
* webbrowser — Convenient Web-browser controller::
* cgi — Common Gateway Interface support::
* cgitb — Traceback manager for CGI scripts::
* wsgiref — WSGI Utilities and Reference Implementation::
* urllib — URL handling modules::
* urllib.request — Extensible library for opening URLs: urllib request — Extensible library for opening URLs.
* urllib.response — Response classes used by urllib: urllib response — Response classes used by urllib.
* urllib.parse — Parse URLs into components: urllib parse — Parse URLs into components.
* urllib.error — Exception classes raised by urllib.request: urllib error — Exception classes raised by urllib request.
* urllib.robotparser — Parser for robots.txt: urllib robotparser — Parser for robots txt.
* http — HTTP modules::
* http.client — HTTP protocol client: http client — HTTP protocol client.
* ftplib — FTP protocol client::
* poplib — POP3 protocol client::
* imaplib — IMAP4 protocol client::
* nntplib — NNTP protocol client::
* smtplib — SMTP protocol client::
* smtpd — SMTP Server::
* telnetlib — Telnet client::
* uuid — UUID objects according to RFC 4122::
* socketserver — A framework for network servers::
* http.server — HTTP servers: http server — HTTP servers.
* http.cookies — HTTP state management: http cookies — HTTP state management.
* http.cookiejar — Cookie handling for HTTP clients: http cookiejar — Cookie handling for HTTP clients.
* xmlrpc — XMLRPC server and client modules::
* xmlrpc.client — XML-RPC client access: xmlrpc client — XML-RPC client access.
* xmlrpc.server — Basic XML-RPC servers: xmlrpc server — Basic XML-RPC servers.
* ipaddress — IPv4/IPv6 manipulation library::
File: python.info, Node: webbrowser — Convenient Web-browser controller, Next: cgi — Common Gateway Interface support, Up: Internet Protocols and Support
5.21.1 ‘webbrowser’ — Convenient Web-browser controller
-------------------------------------------------------
`Source code:' Lib/webbrowser.py(1)
__________________________________________________________________
The *note webbrowser: 129. module provides a high-level interface to
allow displaying Web-based documents to users. Under most
circumstances, simply calling the *note open(): 28d1. function from this
module will do the right thing.
Under Unix, graphical browsers are preferred under X11, but text-mode
browsers will be used if graphical browsers are not available or an X11
display isn’t available. If text-mode browsers are used, the calling
process will block until the user exits the browser.
If the environment variable ‘BROWSER’ exists, it is interpreted as the
*note os.pathsep: ff0.-separated list of browsers to try ahead of the
platform defaults. When the value of a list part contains the string
‘%s’, then it is interpreted as a literal browser command line to be
used with the argument URL substituted for ‘%s’; if the part does not
contain ‘%s’, it is simply interpreted as the name of the browser to
launch. (2)
For non-Unix platforms, or when a remote browser is available on Unix,
the controlling process will not wait for the user to finish with the
browser, but allow the remote browser to maintain its own windows on the
display. If remote browsers are not available on Unix, the controlling
process will launch a new browser and wait.
The script ‘webbrowser’ can be used as a command-line interface for the
module. It accepts a URL as the argument. It accepts the following
optional parameters: ‘-n’ opens the URL in a new browser window, if
possible; ‘-t’ opens the URL in a new browser page (“tab”). The options
are, naturally, mutually exclusive. Usage example:
python -m webbrowser -t "http://www.python.org"
The following exception is defined:
-- Exception: webbrowser.Error
Exception raised when a browser control error occurs.
The following functions are defined:
-- Function: webbrowser.open (url, new=0, autoraise=True)
Display `url' using the default browser. If `new' is 0, the `url'
is opened in the same browser window if possible. If `new' is 1, a
new browser window is opened if possible. If `new' is 2, a new
browser page (“tab”) is opened if possible. If `autoraise' is
‘True’, the window is raised if possible (note that under many
window managers this will occur regardless of the setting of this
variable).
Note that on some platforms, trying to open a filename using this
function, may work and start the operating system’s associated
program. However, this is neither supported nor portable.
Raises an *note auditing event: fd1. ‘webbrowser.open’ with
argument ‘url’.
-- Function: webbrowser.open_new (url)
Open `url' in a new window of the default browser, if possible,
otherwise, open `url' in the only browser window.
-- Function: webbrowser.open_new_tab (url)
Open `url' in a new page (“tab”) of the default browser, if
possible, otherwise equivalent to *note open_new(): 28d3.
-- Function: webbrowser.get (using=None)
Return a controller object for the browser type `using'. If
`using' is ‘None’, return a controller for a default browser
appropriate to the caller’s environment.
-- Function: webbrowser.register (name, constructor, instance=None, *,
preferred=False)
Register the browser type `name'. Once a browser type is
registered, the *note get(): 28d5. function can return a controller
for that browser type. If `instance' is not provided, or is
‘None’, `constructor' will be called without parameters to create
an instance when needed. If `instance' is provided, `constructor'
will never be called, and may be ‘None’.
Setting `preferred' to ‘True’ makes this browser a preferred result
for a *note get(): 28d5. call with no argument. Otherwise, this
entry point is only useful if you plan to either set the ‘BROWSER’
variable or call *note get(): 28d5. with a nonempty argument
matching the name of a handler you declare.
Changed in version 3.7: `preferred' keyword-only parameter was
added.
A number of browser types are predefined. This table gives the type
names that may be passed to the *note get(): 28d5. function and the
corresponding instantiations for the controller classes, all defined in
this module.
Type Name Class Name Notes
---------------------------------------------------------------------------------------
‘'mozilla'’ ‘Mozilla('mozilla')’
‘'firefox'’ ‘Mozilla('mozilla')’
‘'netscape'’ ‘Mozilla('netscape')’
‘'galeon'’ ‘Galeon('galeon')’
‘'epiphany'’ ‘Galeon('epiphany')’
‘'skipstone'’ ‘BackgroundBrowser('skipstone')’
‘'kfmclient'’ ‘Konqueror()’ (1)
‘'konqueror'’ ‘Konqueror()’ (1)
‘'kfm'’ ‘Konqueror()’ (1)
‘'mosaic'’ ‘BackgroundBrowser('mosaic')’
‘'opera'’ ‘Opera()’
‘'grail'’ ‘Grail()’
‘'links'’ ‘GenericBrowser('links')’
‘'elinks'’ ‘Elinks('elinks')’
‘'lynx'’ ‘GenericBrowser('lynx')’
‘'w3m'’ ‘GenericBrowser('w3m')’
‘'windows-default'’ ‘WindowsDefault’ (2)
‘'macosx'’ ‘MacOSX('default')’ (3)
‘'safari'’ ‘MacOSX('safari')’ (3)
‘'google-chrome'’ ‘Chrome('google-chrome')’
‘'chrome'’ ‘Chrome('chrome')’
‘'chromium'’ ‘Chromium('chromium')’
‘'chromium-browser'’ ‘Chromium('chromium-browser')’
Notes:
1. “Konqueror” is the file manager for the KDE desktop environment for
Unix, and only makes sense to use if KDE is running. Some way of
reliably detecting KDE would be nice; the ‘KDEDIR’ variable is not
sufficient. Note also that the name “kfm” is used even when using
the ‘konqueror’ command with KDE 2 — the implementation selects the
best strategy for running Konqueror.
2. Only on Windows platforms.
3. Only on Mac OS X platform.
New in version 3.3: Support for Chrome/Chromium has been added.
Here are some simple examples:
url = 'http://docs.python.org/'
# Open URL in a new tab, if a browser window is already open.
webbrowser.open_new_tab(url)
# Open URL in new window, raising the window if possible.
webbrowser.open_new(url)
* Menu:
* Browser Controller Objects::
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/webbrowser.py
(2) (1) Executables named here without a full path will be searched
in the directories given in the ‘PATH’ environment variable.
File: python.info, Node: Browser Controller Objects, Up: webbrowser — Convenient Web-browser controller
5.21.1.1 Browser Controller Objects
...................................
Browser controllers provide these methods which parallel three of the
module-level convenience functions:
-- Method: controller.open (url, new=0, autoraise=True)
Display `url' using the browser handled by this controller. If
`new' is 1, a new browser window is opened if possible. If `new'
is 2, a new browser page (“tab”) is opened if possible.
-- Method: controller.open_new (url)
Open `url' in a new window of the browser handled by this
controller, if possible, otherwise, open `url' in the only browser
window. Alias *note open_new(): 28d3.
-- Method: controller.open_new_tab (url)
Open `url' in a new page (“tab”) of the browser handled by this
controller, if possible, otherwise equivalent to *note open_new():
28d3.
File: python.info, Node: cgi — Common Gateway Interface support, Next: cgitb — Traceback manager for CGI scripts, Prev: webbrowser — Convenient Web-browser controller, Up: Internet Protocols and Support
5.21.2 ‘cgi’ — Common Gateway Interface support
-----------------------------------------------
`Source code:' Lib/cgi.py(1)
__________________________________________________________________
Support module for Common Gateway Interface (CGI) scripts.
This module defines a number of utilities for use by CGI scripts written
in Python.
* Menu:
* Introduction: Introduction<10>.
* Using the cgi module::
* Higher Level Interface::
* Functions: Functions<8>.
* Caring about security::
* Installing your CGI script on a Unix system::
* Testing your CGI script::
* Debugging CGI scripts::
* Common problems and solutions::
---------- Footnotes ----------
(1) https://github.com/python/cpython/tree/3.8/Lib/cgi.py
File: python.info, Node: Introduction<10>, Next: Using the cgi module, Up: cgi — Common Gateway Interface support
5.21.2.1 Introduction
.....................
A CGI script is invoked by an HTTP server, usually to process user input
submitted through an HTML ‘