What's New In Python 3.2
************************

著者:
   Raymond Hettinger

This article explains the new features in Python 3.2 as compared to
3.1. Python 3.2 was released on February 20, 2011. It focuses on a few
highlights and gives a few examples.  For full details, see the
Misc/NEWS file.

参考: **PEP 392** - Python 3.2 リリーススケジュール


PEP 384: 安定 ABI の定義
========================

かつて、ある Python バージョンでビルドされた拡張モジュールは、ほかの
Python バージョンでは使えないことがありました。特に Windows では、全て
の Python 公式リリースごとに、使いたい全ての拡張モジュールを再ビルドす
る必要がありました。これは、拡張モジュールが Python インタプリタの内部
実装に自由にアクセス出来たことからの結果でした。

Python 3.2 より、このことに関する別のアプローチが利用可能になりました
。("Py_LIMITED_API" を定義することで) 制限された API のみ使うようにす
る拡張モジュールは、数多くの内部実装を利用出来ない代わりに将来のいくつ
ものリリースで安定であることを約束された API 集合だけ使うことになりま
す。結果として、このモードで 3.2 のためにビルドされた拡張モジュールは
3.3、3.4、…でも動作するでしょう。なお、メモリ構造の詳細を曝すような拡
張モジュールでは、相変わらず全ての Python 公式リリースごとに再ビルドす
る必要があるでしょう。

参考:

  **PEP 384** - 安定 ABI の定義
     PEP written by Martin von Löwis.


PEP 389: argparse コマンドライン解析モジュール
==============================================

コマンドライン解析のために、新たに "argparse" モジュールが "optparse"
の制限を克服するものとして導入されました。optparse には (オプションだ
けでなく) 位置引数のサポートはなく、サブコマンドも作れず、必須のオプシ
ョンやその他オプション指定と検証に共通するパターンについてのサポートも
ありませんでした。

このモジュールはコミュニティにおいて既に広く行き渡っていたサードパーテ
ィのモジュールでした。その先駆者たちよりももっと完全な機能性を持ったこ
とで、 "argparse" モジュールは今ではコマンドライン解析での推奨モジュー
ルとなりました。古いモジュールは、それに依存しているレガシーなコードが
大量にあるため、今後も利用可能なまま保たれます。

以下に注釈付きで示したパーサは、有限個の選択肢から選ばせ、 *metavar*
でヘルプ表示を指定し、一つ以上の位置引数があるかを検証し、必須オプショ
ンを設けている実例になっています:

   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')

コマンド文字列とともにこのパーサを呼び出してみます:

   >>> 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'

このパーサが自動的に作り出したヘルプはこのようになります:

   >>> 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

"argparse" 機能でとりわけ素晴らしいのはサブパーサを定義出来ることです
。それらはそれぞれ自身固有の引数パターンを持ち、ヘルプ表示をします:

   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

参考:

  **PEP 389** - 新しいコマンドライン解析モジュール
     PEP written by Steven Bethard.

  optparse からのアップグレード に "optparse" との違いが詳しく書かれて
  います。


PEP 391:  logging の辞書ベースの設定
====================================

The "logging" 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.

もっと柔軟な設定をサポートするために、新たに
"logging.config.dictConfig()" が追加されました。これはロギングの設定を
通常の Python 辞書で指定します。設定オプションにはフォーマッタ、ハンド
ラ、フィルタ、そしてロガーが含まれます。以下は設定辞書の動作する実例で
す:

   {"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"]}}

その辞書が "conf.json" ファイルに格納されているのであれば、コードから
このように呼び出してロード出来ます:

   >>> 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

参考:

  **PEP 391** - logging の辞書ベースの設定
     PEP written by Vinay Sajip.


PEP 3148:  "concurrent.futures" モジュール
==========================================

同時並行性 (concurrency) を生成・管理するコードはトップレベルの新しい
名前空間 *concurrent* に集められることになりました。その最初のメンバー
が *futures* パッケージです。これはスレッドとプロセス管理の統一的な高
水準インターフェイスを提供します。

"concurrent.futures" の設計は Java の *java.util.concurrent* パッケー
ジに触発されました。そのモデルでは、実行する呼び出しとその結果は
"Future" オブジェクトで表現され、これはスレッド、プロセス、遠隔手続呼
び出しの共通的な側面を抽象化しています。そのオブジェクトはステータスの
チェック (実行中あるいは完了かどうか)、タイムアウト、キャンセル、コー
ルバックの追加、そして結果や例外へのアクセスをサポートします。

この新モジュールで提供される最も基本となるのが、呼び出しの実行と管理の
ための 2 つの executor クラスです。これら executor の目標は、これまで
にもあった道具立てでの並列呼び出しを、もっと簡単にすることです。リソー
スプールをお膳立てしたり呼び出しを実行したり、結果のキューを作ったり、
あるいはタイムアウトのハンドラを追加したり、スレッドやプロセスや遠隔手
続呼び出しの総数を制限したり、といったことを、これらを使えば、その労力
を小さく出来ます。

理想的にはそれぞれのアプリケーションは、プロセスとスレッドの制限が確実
に管理されるように、複数コンポーネントをまたがって単一の executor を共
有すべきです。そうしておけば、個々のコンポーネントがリソース管理につい
ての競合する戦略を持つ場合に起こる設計の難題は解決出来ます。

どちらの executor クラスとも 3 つのメソッドを持つ共通インターフェイス
を持ちます。 "submit()" メソッドは呼び出し可能オブジェクトをスケジュー
ルし、 "Future" オブジェクトを返します。 "map()" メソッドは一気に複数
の非同期呼び出しをスケジュールします。 "shutdown()" メソッドはリソース
を解放します。これらクラスは *context manager* なので "with" 文ととも
に使うことが出来、現時点で保留中の future が実行を終えた際にリソースが
自動的に解放されることを保障出来ます。

以下はファイルコピーを行う 4 つの並列スレッドを実行する、
"ThreadPoolExecutor" の単純な使用例です:

   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')

参考:

  **PEP 3148** - Futures -- 計算の非同期な実行
     PEP written by Brian Quinlan.

  ThreadPoolExecutor の例 はスレッド並列でウェブページを取得する実例コ
  ードです。

  "ProcessPoolExecutor" の実例を 並列で素数計算するコード例 として挙げ
  ています。


PEP 3147:  PYC リポジトリーディレクトリ
=======================================

バイトコードを *.pyc* ファイルとしてキャッシュする Python の方法は、複
数の Python インタプリタ環境ではうまく働きませんでした。一つのインタプ
リタがもう一つのインタプリタによって作られたキャッシュファイルに出会う
と、そのインタプリタはソースを再コンパイルしてキャッシュファイルを上書
きするわけですから、これではキャッシュの価値は台無しです。

「pyc ファイト」問題は、複数バージョンの Python を載せる Linux ディス
トリビューションにありふれたこととなったよりももっと表立って来ました。
これら衝突は Unladen Swallow のような CPython 以外の実装でも起こります
。

この問題を解決するために、Python のインポート機構はそれぞれのインタプ
リタごとに区別された名前を使うように拡張されました。Python 3.2、Python
3.3、 Unladen Swallow でそれぞれ競合するファイル名 "mymodule.pyc" を使
うのではなく、それぞれ "mymodule.cpython-32.pyc",
"mymodule.cpython-33.pyc", "mymodule.unladen10.pyc" を使うようになりま
した。同時にこれらの新しいファイルがソースディレクトリを散らかすのを防
ぐために、 *pyc* ファイルはパッケージディレクトリの下の "__pycache__"
ディレクトリに収集するようになりました。

ファイル名と格納先ディレクトリは別にして、この新方式にはプログラマ目線
での目に見える変化が少しあります:

* インポートされたモジュールには属性 "__cached__" が追加されて、これに
  はインポートされたファイルの実際のファイル名を記憶しています:

  >>> import collections
  >>> collections.__cached__ 
  'c:/py32/lib/__pycache__/collections.cpython-32.pyc'

* The tag that is unique to each interpreter is accessible from the
  "imp" 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 "imp" 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'

* "py_compile" と "compileall" モジュールはこの新命名規則とターゲット
  ディレクトリを反映するよう更新されました。 *compileall* のコマンドラ
  インからの起動には新しいオプションが追加されました。 "-i" はコンパイ
  ルするファイルとディレクトリのリストを指定します。 "-b" はバイトコー
  ドファイルを *__pycache__* ではなく伝統的な場所に書き出します。

* The "importlib.abc" module has been updated with new *abstract base
  classes* 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).

参考:

  **PEP 3147** - PYC リポジトリーディレクトリー
     PEP written by Barry Warsaw.


PEP 3149: ABI バージョンでタグ付けされた .so ファイル
=====================================================

PYC レポジトリーディレクトリでは複数バイトコードのキャッシュファイルを
共存できます。この PEP は共有オブジェクトについての同じようなメカニズ
ムを実装します。それには共通のディレクトリを使い、個々のバージョンごと
の名前の区別をします。

共通ディレクトリは "pyshared" です。また、ファイル名は Python 実装 (例
えば CPython、PyPy、Jython など)、メジャー・マイナーバージョン番号やオ
プショナルなビルドフラグ (例えばデバッグビルドでの "d"、 pymalloc ビル
ドの "m"、ワイド幅 Unicode の "u") を特定することで区別するように命名
されます。任意のパッケージ "foo" について、配布パッケージがインストー
ルされるとこれらファイルは例えばこのような具合でしょう:

   /usr/share/pyshared/foo.cpython-32m.so
   /usr/share/pyshared/foo.cpython-33md.so

Python 自身からこのタグに、 "sysconfig" モジュールにある関数でアクセス
出来ます:

   >>> 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'

参考:

  **PEP 3149** - ABI バージョンでタグ付けされた .so ファイル
     PEP written by Barry Warsaw.


PEP 3333: Python Web Server Gateway Interface v1.0.1
====================================================

この情報 PEP は、 WSGI プロトコルによってどのようにバイト列/テキストの
問題が処理されるべきかを明確化しています。HTTP プロトコルそのものはバ
イト列指向であっても、Python 3 にあってほとんど "str" 型で便利に文字列
処理出来るようにすることが挑戦でした。

この PEP では、リクエスト/レスポンスヘッダとメタデータに使われるいわゆ
る *ネイティブ文字列(native strings)* と、リクエストとレスポンスのボデ
ィに使われる *バイト列文字列(byte strings)* を区別します。

The *native strings* are always of type "str" 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** with respect to encoding. That is, they must either be
*ISO-8859-1* characters or use **RFC 2047** MIME encoding.

WSGI アプリケーションを Python 2 から移植する開発者は、とりわけ以下に
注意してください:

* Python 2 でヘッダとしてネイティブ文字列を既に使っているアプリケーシ
  ョンは変更の必要はありません。(---訳注: 原文は「If the app already
  used strings for headers ...」。native strings としないと意味が通り
  ません。 ---)

* そうではなくアプリケーションが出力ヘッダをエンコードしていたり、入力
  ヘッダをデコードしていたのであれば、ヘッダは Latin-1 に再エンコード
  する必要があります。たとえば "h.encode('utf-8')" により utf-8 でエン
  コードされた出力ヘッダは、バイト列からネイティブ文字列に
  "h.encode('utf-8').decode('latin-1')" で変換する必要があります。(---
  訳注: この原文は h が元々 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 "wsgiref" module has a
new function, "wsgiref.handlers.read_environ()" for transcoding CGI
variables from "os.environ" into native strings and returning a new
dictionary.

参考:

  **PEP 3333** - Python Web Server Gateway Interface v1.0.1
     PEP written by Phillip Eby.


その他の言語変更
================

Python 言語コアに小さな変更がいくつか行われました:

* "format()" と "str.format()" のフォーマット文字 **#** が拡張されまし
  た。以前から二進、八進、十六進整数に対して順にそれぞれ '0b', '0o',
  '0x' を冠する出力をしていましたが、これは浮動小数点数、複素数、
  Decimal に対しても処理するようになり、これはあとに続く桁がなくても必
  ず小数点を出力します。

  >>> format(20, '#o')
  '0o24'
  >>> format(12.34, '#5.0f')
  '  12.'

  (Suggested by Mark Dickinson and implemented by Eric Smith in
  bpo-7094.)

* 新たに追加された "str.format_map()" メソッドも既存の "str.format()"
  メソッドの能力を拡大します。これは任意の *mapping* を受け付けます。
  この新規メソッドは Python の数多くの「辞書的な」オブジェクトとともに
  文字列書式化を使うことを可能にします。たとえば "defaultdict",
  "Shelf", "ConfigParser", あるいは "dbm" などで使えるということです。
  カスタムな "dict" のサブクラスと一緒に使うのも便利で、例えばルックア
  ップの前にキーを正規化してみたり、未知のキーを "__missing__()" メソ
  ッドで補ってみたりなど出来ます:

     >>> 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 <name>, welcome to <location>'

   (Suggested by Raymond Hettinger and implemented by Eric Smith in
   bpo-6081.)

* 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
  "sys.flags" 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).

* The "hasattr()" function works by calling "getattr()" and detecting
  whether an exception is raised.  This technique allows it to detect
  methods created dynamically by "__getattr__()" or
  "__getattribute__()" 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 "AttributeError" 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.)

* 浮動小数点数または複素数に対する "str()" は "repr()" 表現と同じにな
  りました。以前は "str()" 形式のほうが短い表現になっていましたがこれ
  は単に困惑させるものでした。 "repr()" がデフォルトで可能ななかでの一
  番短い表現をするようになったので、 "str()" があえて短い表現を取ろう
  とする必要性がなくなりました。

  >>> import math
  >>> repr(math.pi)
  '3.141592653589793'
  >>> str(math.pi)
  '3.141592653589793'

  (Proposed and implemented by Mark Dickinson; bpo-9337.)

* "memoryview" objects now have a "release()" 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.)

* 以前は、入れ子になったブロック内である名前が自由変数として現れる場合
  、局所名前空間からその名前を削除することは不正でした。

     def outer(x):
         def inner():
             return x
         inner()
         del x

  これは可能になりました。 "except" 節の対象が消去されることを思い出せ
  ば、 Python 2.6 では動き、Python 3.1 では "SyntaxError" を送出するこ
  のコードは再び動きます:

     def f():
         def print_error():
             print(e)
         try:
             something
         except Exception as e:
             print_error()
             # implicit "del e" here

  (bpo-4617 を参照してください。)

* Struct sequence types are now subclasses of tuple. This means that C
  structures like those returned by "os.stat()", "time.gmtime()", and
  "sys.version_info" now work like a *named tuple* 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.)

* 警告の制御を以前より簡単に出来るようになりました。環境変数
  "PYTHONWARNINGS" を使えば、コマンドラインで "-W" を使うより簡単です:

     $ export PYTHONWARNINGS='ignore::RuntimeWarning::,once::UnicodeWarning::'

  (Suggested by Barry Warsaw and implemented by Philip Jenvey in
  bpo-7301.)

* 新たな警告カテゴリ "ResourceWarning" が追加されました。これはリソー
  ス消費やクリーンアップについての潜在的な問題が検出されると発行されま
  す。これは通常のリリースビルドではデフォルトでは黙殺しますが、
  "warnings" モジュールで意味付けを変更 (訳注: 警告フィルタ 参照) する
  かコマンドラインオプションで有効に出来ます。

  A "ResourceWarning" is issued at interpreter shutdown if the
  "gc.garbage" list isn't empty, and if "gc.DEBUG_UNCOLLECTABLE" is
  set, all uncollectable objects are printed.  This is meant to make
  the programmer aware that their code contains object finalization
  issues.

  "ResourceWarning" は明示的にクローズされなかった *file object* が破
  壊される際にも発行されます。そのようなオブジェクトのデアロケータは背
  後の OS リソース (大抵はファイル記述子) を閉じることを保障はしますが
  、オブジェクトのデアロケートの遅れは様々な問題を産み落とし、とりわけ
  Windows でこれは顕著です。コマンドラインからこの警告を有効にするには
  例えばこのようにします:

     $ 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 and
  bpo-477863.)

* "range" objects now support *index* and *count* methods. This is
  part of an effort to make more objects fully implement the
  "collections.Sequence" *abstract base class*.  As a result, the
  language will have a more uniform API.  In addition, "range" objects
  now support slicing and negative indices, even with values larger
  than "sys.maxsize".  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, by Alexander
  Belopolsky in bpo-2690, and by Nick Coghlan in bpo-10889.)

* Py2.x から "callable()" 組み込み関数が復活しました。 この関数は
  "isinstance(x, collections.Callable)" のように式内で *抽象基底クラス
  * を使用するよりも、簡潔で可読性の高い手段を提供します:

  >>> callable(max)
  True
  >>> callable(20)
  False

  (bpo-10518 を参照してください。)

* Python のインポート機構が、パス名に非 ASCII 文字を含むディレクトリに
  インストールされたモジュールをロードできるようになりました。これはユ
  ーザ名に非 ASCII 文字を含むユーザにとっての、ホームディレクトリにま
  つわる腹立たしい問題を解決しました。

   (Required extensive work by Victor Stinner in bpo-9425.)


新たなモジュール、改良されたモジュール、非推奨のモジュール
==========================================================

Python の標準ライブラリは保守の努力と質の向上を大きく受けています。

The biggest news for Python 3.2 is that the "email" package, "mailbox"
module, and "nntplib" 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.

標準ライブラリ全体を通じて、エンコーディング問題とテキスト対バイト列問
題に関してより注意深い配慮がなされました。とりわけオペレーティングシス
テムとの連携では、Windows の MBCS エンコーディングやロケールに配慮する
エンコーディング、あるいは UTF-8 を使う非 ASCII のデータ交換はより良く
行えるようになりました。

もうひとつの目覚しい勝利は、 *SSL* 接続とセキュリティ証明書サポートの
大幅な改善です。

さらに、以前よりももっと多くのクラスが今では *context manager* を実装
し、 "with" 文を使った便利で信頼出来るリソースのクリーンアップをサポー
トしています。


email
-----

R. David Murray による多大なる尽力により、Python 3 における "email" パ
ッケージの適応可能性の問題は、ほとんど修正されました。問題は、email が
典型的には "str" テキストよりも "bytes" 形式で格納され、読まれることで
あり、一つの email の中に複数のエンコーディングを含みうることでした。
ですから email パッケージは email メッセージの解析と生成を bytes フォ
ーマットで拡張される必要がありました。

* 新規関数 "message_from_bytes()", "message_from_binary_file()" 、それ
  に新規クラス "BytesFeedParser", "BytesParser" が、バイナリメッセージ
  データをパースしてモデルオブジェクトに読み込みます。

* bytes の入力をモデルに与えると "get_payload()" は、デフォルトでは *8
  ビット* の *Content-Transfer-Encoding* MIME ヘッダをもったメッセージ
  ボディをそのヘッダで指定された文字セットを使い、デコードして結果文字
  列を返します。

* bytes の入力をモデルに与えると "Generator" は、 *8 ビット* の
  *Content-Transfer-Encoding* MIME ヘッダをもったメッセージボディを、
  *7 ビット* の *Content-Transfer-Encoding* を持つものに変換します。

  エンコードされていない非 ASCII バイト列を持つヘッダは *未知の 8 ビッ
  ト* 文字セットを使った **RFC 2047** エンコードであると見做されます。

* 新規クラス "BytesGenerator" は出力としてバイト列を生成します。これは
  モデルを構築するのに使った入力にあった非 ASCII データをそのままにし
  、 *8ビットの* *Content-Transfer-Encoding* を持つメッセージボディに
  含めます。

* "smtplib" モジュールの "SMTP" クラスが "sendmail()" メソッドの *msg*
  引数としてバイト列文字列を受け付けるようになりました。新しく追加され
  たメソッド "send_message()" は *msg* 引数に "Message" オブジェクトを
  受け付けます。このメソッドは *msg* にあたえたオブジェクトから
  *from_addr* アドレスと *to_addrs* アドレスを直接取ることが出来ます。

(Proposed and implemented by R. David Murray, bpo-4661 and bpo-10321.)


elementtree
-----------

The "xml.etree.ElementTree" package and its "xml.etree.cElementTree"
counterpart have been updated to version 1.3.

有用な関数とメソッドが幾つか新たに追加されました:

* "xml.etree.ElementTree.fromstringlist()"、断片のシーケンスから XML
  文書を組み立てます

* "xml.etree.ElementTree.register_namespace()"、大域名前空間接頭辞を登
  録するのに使用します

* "xml.etree.ElementTree.tostringlist()"、全サブリストを含めた文字列表
  記に使用します

* "xml.etree.ElementTree.Element.extend()"、ゼロ以上の要素のシークエン
  スを追加するのに使用します

* "xml.etree.ElementTree.Element.iterfind()"、要素と子要素を検索します

* "xml.etree.ElementTree.Element.itertext()"、要素と子要素を走るテキス
  トイテレータを作成します

* "xml.etree.ElementTree.TreeBuilder.end()"、現在の要素を閉じます

* "xml.etree.ElementTree.TreeBuilder.doctype()"、doctype 定義を処理し
  ます

2つのメソッドが非推奨になりました:

* "xml.etree.ElementTree.getchildren()" use "list(elem)" instead.

* "xml.etree.ElementTree.getiterator()" use "Element.iter" instead.

For details of the update, see Introducing ElementTree on Fredrik
Lundh's website.

(Contributed by Florent Xicluna and Fredrik Lundh, bpo-6472.)


functools
---------

* "functools" モジュールに関数呼び出しをキャッシュするデコレータが追加
  されています。 "functools.lru_cache()" を使うと、同じ結果が期待され
  る場合の外部リソースへの問い合わせ繰り返しを防ぐことができます。

  たとえばデータベース問い合わせ関数をキャッシュするデコレータで修飾す
  れば、人気の検索のデータベースアクセスを省けます:

  >>> 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

  効果的なキャッシュサイズを選択する助けとなるよう、ラップされた関数は
  キャッシュの統計を追跡する道具にもなっています:

  >>> get_phone_number.cache_info()     
  CacheInfo(hits=4805, misses=980, maxsize=300, currsize=300)

  テーブル phonelist が更新されたのであれば、内容が古くなったキャッシ
  ュをクリアできます:

  >>> get_phone_number.cache_clear()

  (Contributed by Raymond Hettinger and incorporating design ideas
  from Jim Baker, Miki Tebeka, and Nick Coghlan; see recipe 498245,
  recipe 577479, bpo-10586, and bpo-10593.)

* The "functools.wraps()" 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.

  上の例の場合、キャッシュは元の関数を復元することで削除できます:

  >>> get_phone_number = get_phone_number.__wrapped__    # uncached function

  (By Nick Coghlan and Terrence Cole; bpo-9567, bpo-3445, and
  bpo-8814.)

* 拡張比較 (rich comparison) メソッドを持つクラスを書く助けとなるよう
  、新たなデコレータ "functools.total_ordering()" が追加されました。こ
  れは既存の等式と不等式メソッドを使って残りのメソッドを埋めます。

  例えば *__eq__* と *__lt__* を与えると、 "total_ordering()" は
  *__le__*, *__gt__*, *__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()))

  *total_ordering* デコレータが残りの比較メソッドを自動的に補います。

  (Contributed by Raymond Hettinger.)

* Python 2 からのプログラムの移植のために使える、
  "functools.cmp_to_key()" 関数が追加されました。これは旧式の比較関数
  をモダンな *key function* に変換します:

  >>> # locale-aware sort order
  >>> sorted(iterable, key=cmp_to_key(locale.strcoll)) 

  ソートの例と簡単なチュートリアルは ソートのテクニック を参照して下さ
  い。

  (Contributed by Raymond Hettinger.)


itertools
---------

* "itertools" モジュールに、 APL の *scan* 演算子や NumPy の
  *accumulate* 関数をモデルにした "accumulate()" が新しく追加されまし
  た:

  >>> 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]

  "accumulate()" の使用例が examples for the random module にあります
  。

  (Contributed by Raymond Hettinger and incorporating design
  suggestions from Mark Dickinson.)


collections
-----------

* "collections.Counter" クラスに 2 つの形式のインプレイス減算が追加さ
  れました。 saturating subtraction (訳注: 結果を [min, max] に収める
  演算) を行う既存の *-=* 演算子に加え、新しく追加された "subtract()"
  メソッドは通常の減算を行います。前者は要素数に正しか持たない 多重集
  合 に用いるのに適していて、後者は要素数に負を許容するユースケースに
  より適しています。

  >>> 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.)

* "collections.OrderedDict" クラスに "move_to_end()" メソッド (訳注:
  「end」は「端」) が追加されました。これは既存のキーを受け取って、順
  序付けされたシーケンスの最初の位置か最後の位置に移動します。

  デフォルトでは要素は最後の位置に移動します。これはエントリを "od[k]
  = od.pop(k)" で入れ替えるのと同じです。

  高速な端への移動操作はエントリの並べなおしに便利です。例えば、最も最
  近使った順序でエントリを年齢付けすることによってアクセス順を追跡する
  のに、順序付けされた辞書を使えます。

  >>> 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.)

* "collections.deque" クラスに 2 つのメソッド "count()" と "reverse()"
  が追加されました。これでさらに "list" との置換可能性が増しました:

  >>> 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.)


threading
---------

"threading" モジュールに新たに同期クラス "Barrier" が追加されました。
これは多重のスレッドに、それら全てが共通のバリアポイントに到達するまで
待たせます。バリアは、複数の事前条件を持ったタスクを、その前任タスクの
全てが完了するまで走らせたくない場合に有用です。

バリアは任意の数のスレッドに対して使えます。これは Rendezvous を一般化
したもので、それは 2 つのスレッドに対してのみに定義されていました。

二段階の周期バリアとしての実装なので、 "Barrier" オブジェクトはループ
内での利用に適しています。分けられた *filling (満杯にする)* 段階と
*draining (排出する)* 段階は、スレッドのどれか一つが元に戻ってバリアに
再び入る前に、全てのスレッドが解放 (排出) されることを保障します。バリ
アはそれぞれの周期のあとに完全にリセットされます。

バリアは例えばこのように使います:

   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()

この例ではバリアは、全ての投票所が閉じるまではどの投票サイトでも得票を
数えないルールを強制しています。バリアを使ったソリューションが
"threading.Thread.join()" でのそれといかに似ているか気付くと思います。
ですがバリアポイントを通過した後に、スレッドは生き続けて (投票を集計す
る (summarizing ballots) という) 仕事を続けます。

いずれかの前任タスクがハングしたり遅延したりしうるのであれば、バリアを
*timeout* 付きで作ることができます。全ての前任タスクがバリアポイントに
到達する前にタイムアウトの期間が経過すると、全ての待機スレッドは解放さ
れて "BrokenBarrierError" 例外が発生します:

   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)

この例ではバリアは、もっと手堅いルールを強制しています。選挙サイトのど
れかが午前零時前に終了しなかった場合バリアがタイムアウトし、投票用紙は
密封されて後の処理のためのキューに預けられます。

See Barrier Synchronization Patterns 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,
*section 3.6*.

(Contributed by Kristján Valur Jónsson with an API review by Jeffrey
Yasskin in bpo-8777.)


datetime と time
----------------

* "datetime" モジュールに新たに "timezone" 型が追加されています。これ
  は "tzinfo" インターフェイスを実装し、UTC からの固定オフセットとタイ
  ムゾーン名を返します。これによりタイムゾーンを意識する (timezone-
  aware な) datetime オブジェクトを容易に作れます:

     >>> 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)

* さらに、 "timedelta" オブジェクトは今では "float" を掛けることができ
  、 "float" や "int" で割ることができます。また、 "timedelta" オブジ
  ェクトは別の "timedelta" で割ることが出来ます。

* "datetime.date.strftime()" メソッドにはもう 1900 年以降という制限は
  ありません。新しくサポートされる年の範囲は 1000 以上 9999 以下です。

* 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 "DeprecationWarning".  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 "time.asctime()" function will
  accept any year that fits in a C int, while the "time.mktime()" and
  "time.strftime()" functions will accept the full range supported by
  the corresponding operating system functions.

(Contributed by Alexander Belopolsky and Victor Stinner in
bpo-1289118, bpo-5094, bpo-6641, bpo-2706, bpo-1777412, bpo-8013, and
bpo-10827.)


math
----

"math" モジュールがアップデートされ、C99 標準に触発されて6つの新たな関
数が追加されました。

"isfinite()" 関数は特殊値を確実に、かつ高速に検出する方法を提供します
。通常の数には "True" を、*Nan* や *Infinity* には "False" を返します:

>>> from math import isfinite
>>> [isfinite(x) for x in (123, 4.56, float('Nan'), float('Inf'))]
[True, True, False, False]

"expm1()" 関数は小さな *x* に対しても精度を失うこと (訳注: 桁落ち) な
く "e**x-1" を計算します。 通常、桁落ちは非常に近い数の引き算で起こり
ます。

>>> from math import expm1
>>> expm1(0.013671875)   # more accurate way to compute e**x-1 for a small x
0.013765762467652909

"erf()" 関数は確率積分、すなわち 誤差関数 を返します。 相補誤差関数
"erfc()" は "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

"gamma()" 関数は階乗を連続的に拡張したものです。詳細は ガンマ関数  を
参照してください。ガンマ関数は階乗に関係しているため、小さな  *x* でも
大きくなります。そのため、ガンマ関数の自然対数を計算する "lgamma()" 関
数があります。

>>> from math import gamma, lgamma
>>> gamma(7.0)           # six factorial
720.0
>>> lgamma(801.0)        # log(800 factorial)
4551.950730698041

(Contributed by Mark Dickinson.)


abc
---

"abc" モジュールは "abstractclassmethod()" と "abstractstaticmethod()"
をサポートしました。

これらのツールによって特定の "classmethod()" や "staticmethod()" を実
装する必要のある *抽象基底クラス* を定義することが出来ます。

   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.)


io
--

"io.BytesIO" に "memoryview()" と同様の機能を提供する新たなメソッド
"getbuffer()" が追加されました。 このメソッドはコピーせずにデータの編
集可能なビューを作成します。 バッファのランダムアクセスとスライス記法
のサポートはインプレースな編集に適しています:

   >>> 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.)


reprlib
-------

When writing a "__repr__()" 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 "list" and "set"
handle self-reference by displaying "..." in the recursive part of the
representation string.

To help write such "__repr__()" methods, the "reprlib" module has a
new decorator, "recursive_repr()", for detecting recursive calls to
"__repr__()" 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 and bpo-9840.)


logging
-------

上記の辞書ベースの設定に加え、"logging" パッケージには他にも多くの改良
があります。

ロギングのドキュメントは 基本チュートリアル  、 上級チュートリアル  、
クックブック により増強されてきました。ロギングについて学習するにはこ
れらのドキュメントが最速の方法です。

セットアップ関数 "logging.basicConfig()" に *style* 引数が追加され、こ
れにより異なる 3 つのタイプの文字列書式化がサポートされます。デフォル
トは伝統的な % 書式化を使う "%" で、 "str.format()" スタイルの "{" 、
"string.Template" で提供されるシェルスタイルの "$" をセット出来ます。
以下の 3 つの設定は等価です:

   >>> 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 "StreamHandler" directed to
"sys.stderr" 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
"logging.lastResort".

フィルタの利用は単純になりました。 "Filter" オブジェクトを作る代わりに
、 "True" か "False" を返す任意の Python 呼び出し可能オブジェクトを述
語 (predicate) として利用出来ます。

ほかにも柔軟性を高め、設定を単純化する数多くの改善がありました。Python
3.2 での全ての変更についてはモジュールのドキュメントを参照してください
。


csv
---

"csv" モジュールは新たな方言 "unix_dialect" をサポートしました。 この
方言は全フィールドをクォートし、行末を伝統的な Unix 形式の "'\n'" にし
ます。 登録された方言名は "unix" です。

"csv.DictWriter" に新たなメソッド "writeheader()" が追加されました。
最初の列をドキュメントのフィールド名に書き出すのに使用します:

   >>> 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, and the new method
suggested by Ed Abraham in bpo-1537721.)


contextlib
----------

新しく追加され、そして少しばかりハートを揺さぶるツール
"ContextDecorator" は、関数デコレータとしての任務も果たす *context
manager* を作るのに役立つものです。

利便性のためにこの新機能は "contextmanager()" で使われているので、両方
の役割をサポートするのにことさら労力は要しません。

基本的なアイディアは、コンテキストマネージャも関数デコレータも、ともに
事前アクション・事後アクションラッパーとして使うことが出来るというもの
です。コンテキストマネージャは "with" 文を使ったステートメント (文) の
グループを包みますし、関数デコレータは関数に取り囲まれたステートメント
のグループを包みます。そうです、どちらかの役割で使える事前アクションあ
るいは事後アクションラッパーを書く必要性は、しばしば起こることです。

たとえば関数やステートメントのグループをロガーで包んで、開始時刻と終了
時刻を追跡できるようにするとしばしば便利です。このタスクをこなすために
関数デコレータとコンテキストマネージャの両方を書く代わりに、
"contextmanager()" が単一の定義に両方の能力をもたらしてくれます:

   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)

ContextDecorator が使われる前の 3.1 までの "contextmanager()" では、こ
れはコンテキストマネージャとして使えるだけでした:

   with track_entry_and_exit('widget loader'):
       print('Some time consuming activity goes here')
       load_widget()

今ではこれはデコレータとしても使えます:

   @track_entry_and_exit('widget loader')
   def activity():
       print('Some time consuming activity goes here')
       load_widget()

一つの場所に二重の責務を持たせることには普通ある種の制約を持ち込みます
。コンテキストマネージャには普通は "with" 文で使える値を返せる柔軟性を
持ちますが、関数デコレータにはそれに匹敵するものはありません。

上の例の場合は、*track_entry_and_exit* コンテキストマネージャが、取り
囲まれたステートメント本体で使えるロギングのインスタンスを返す綺麗な方
法はありません。

(Contributed by Michael Foord in bpo-9110.)


decimal と fractions
--------------------

Mark Dickinson は、異なる数値データの型が実際の値が等しい場合は常に同
じハッシュ値を持つことを保証する、エレガントで効率的なスキームを作り上
げました (bpo-8188):

   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,
"sys.hash_info", 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 interoperability 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.

* "decimal.Decimal" コンストラクタに "float" オブジェクトを直接渡せる
  ようになりました。これにより "from_float()" メソッドを使う必要はなく
  なりました (bpo-8257)。

* 型違いの比較はいまでは完全にサポートされたので、 "Decimal" オブジェ
  クトを直接 "float" や "fractions.Fraction" と比較出来ます (bpo-2531
  and bpo-8188)。

Similar changes were made to "fractions.Fraction" so that the
"from_float()" and "from_decimal()" methods are no longer needed
(bpo-8294):

>>> 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 "decimal" 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).

(Contributed by Mark Dickinson and Raymond Hettinger.)


ftp
---

"ftplib.FTP" クラスがコンテキスト管理プロトコルをサポートするようにな
りました。with ブロックを抜ける際に無条件に FTP 接続をクローズします (
クローズ処理で発生する "socket.error" は無視されます)。:

   >>> 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

ちなみに "mmap.mmap" や "fileinput.input()"  のようなほかのファイルラ
イクなオブジェクトも自動クローズするコンテキスト管理プロトコルをサポー
トするようになっていたりします:

   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, and by
Georg Brandl in bpo-8046 and bpo-1286.)

"FTP_TLS" クラスが *context* パラメータを受け取れるようになりました。
これには "ssl.SSLContext" オブジェクトを渡します。これは SSL コンフィ
グレーションオプション、証明書、秘密鍵を単一の (たぶん長生きの) 構造に
まとめあげてくれるものです。

(Contributed by Giampaolo Rodolà; bpo-8806.)


popen
-----

"os.popen()" ならびに "subprocess.Popen()" 関数が自動的にファイル記述
子を閉じる "with" 文をサポートしました。

(Contributed by Antoine Pitrou and Brian Curtin in bpo-7461 and
bpo-10554.)


select
------

The "select" module now exposes a new, constant attribute, "PIPE_BUF",
which gives the minimum number of bytes which are guaranteed not to
block when "select.select()" 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)


gzip と zipfile
---------------

"gzip.GzipFile" が "io.BufferedIOBase" *抽象基底クラス* を継承しました
("truncate()" をのぞく)。 "peek()" メソッドが追加され、シーク不能なフ
ァイルオブジェクトならびにゼロ埋めされたファイルオブジェクトをサポート
しました。

また、"gzip" モジュールにより簡便でインメモリな圧縮および解凍を行う
"compress()" ならびに "decompress()" 関数が追加されました。 圧縮や解凍
の前に "bytes" としてエンコードしなければならない点に注意してください:

>>> 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; and by Antoine Pitrou,
Nir Aides and Brian Curtin in bpo-9962, bpo-1675951, bpo-7471 and
bpo-2846.)

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 "io.BufferedReader"
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.)


tarfile
-------

"TarFile" クラスは今ではコンテキストマネージャとして使えます。加えて、
その "add()" メソッドに新しいオプション *filter* が追加されました。こ
れはファイルをアーカイブに追加するかどうかを制御し、また、ファイルのメ
タデータを編集するのにも使えます。

*filter* オプションは古くて柔軟性に欠ける *exclude* パラメータを置き換
えるものであり、 *exclude* は非推奨となりました。 *filter* を指定する
場合は *keyword argument* として渡さなければなりません。ユーザ指定の
filter 関数は "TarInfo" オブジェクトを受け取り、更新した "TarInfo" オ
ブジェクトを返すか、あるいはアーカイブから除外したければ "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.)


hashlib
-------

"hashlib" モジュールに、2 つの定数属性が追加されました。全ての実装で利
用可能であることが保障されているハッシュアルゴリズムのリストと、現在の
実装で利用可能なそれらリストの 2 つです:

   >>> 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.)


ast
---

"ast" モジュールには、Python リテラル構文を使った式文字列を安全に評価
できる、汎用目的の素晴らしい道具が含まれています。その
"ast.literal_eval()" 関数は、悪用されやすい組み込み関数 "eval()" の安
全な代替品として働いてくれます。Python 3.2 はこれのサポート型に
"bytes" と "set" を加えました。結果サポートする型は全部でこれらになり
ました: 文字列型、バイト列文字列型、数値型、タプル型、リスト型、辞書型
、集合型、真偽値型、"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.)


os
--

ファイル名と環境変数に使われるエンコーディングは OS ごとに様々です。
"os" モジュールは新たに 2 つの関数 "fsencode()" と "fsdecode()" を追加
しました。それぞれファイル名のエンコード、デコードを行います:

>>> 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 "os.supports_bytes_environ" constant will be
true.

エンコードされた環境変数への直接アクセス (可能な場合) には、新規関数
"os.getenvb()" または "os.environb" を使ってください。これは
"os.environ" のバイト列バージョンです。

(Contributed by Victor Stinner.)


shutil
------

"shutil.copytree()" 関数に新たに 2 つのオプションが追加されました:

* *ignore_dangling_symlinks*: "symlinks=False" とするとこの関数は、フ
  ァイルがシンボリックリンクだった場合にリンクそのものではなくそれが指
  しているファイルをコピーします。 *ignore_dangling_symlinks* を真にす
  ると、そのファイルが存在しない場合のエラーを無視します。

* *copy_function*: これはファイルをコピーするのに使われる呼び出し可能
  オブジェクトです。デフォルトでは "shutil.copy2()" が使われます。

(Contributed by Tarek Ziadé.)

これらに加え、 "shutil" モジュールは ZIP ファイル、未圧縮の tar ファイ
ル、gzip 圧縮の tar、 bzip 圧縮の tar のための archiving operations を
新たにサポートします。また、追加のアーカイブファイルフォーマットを登録
する関数もあります (例えば xz 圧縮の tar やカスタムフォーマット)。

そのメインとなるのが "make_archive()" と "unpack_archive()" です。デフ
ォルトではカレントディレクトリ ("os.chdir()" で変えれます) とそのサブ
ディレクトリの両方を処理します。アーカイブファイル名はフルパス名で指定
しなければなりません(訳注: これの真偽不明。リファレンスに対応する記述
なし)。アーカイブするステップは非破壊的です (つまりオリジナルのファイ
ルは変更されずにそのままにされます)。

   >>> 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é.)


sqlite3
-------

"sqlite3" モジュールが pysqlite バージョン 2.6.0 に更新されました。こ
れには 2 つの新機能があります。

* The "sqlite3.Connection.in_transit" attribute is true if there is an
  active transaction for uncommitted changes.

* "sqlite3.Connection.enable_load_extension()" メソッドと
  "sqlite3.Connection.load_extension()" メソッドにより ".so" ファイル
  から SQLite 拡張をロードできます。良く知られた拡張の一つとして、
  SQLite と一緒に配布されている全文検索拡張があります。

(Contributed by R. David Murray and Shashwat Anand; bpo-8845.)


html
----

ただ一つの関数 "escape()" が提供される、新しい "html" モジュールが導入
されました。これは HTML マークアップから予約されている文字をエスケープ
するのに使います:

>>> import html
>>> html.escape('x > 2 && x < 7')
'x &gt; 2 &amp;&amp; x &lt; 7'


socket
------

"socket" モジュールに新たに二つの改善がありました。

* Socket objects now have a "detach()" 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.)

* "socket.create_connection()" がコンテキスト管理プロトコルをサポート
  するようになりました。with ブロックを抜ける際に無条件にソケットをク
  ローズします (クローズ処理で発生する "socket.error" は無視されます)
  。 (Contributed by Giampaolo Rodolà; bpo-9794.) (---訳注: リファレン
  スもそうなのですが、「 "socket.create_connection()" が with をサポー
  ト」というのは実態とは違うんじゃないかと思います(この関数のユーザか
  らは同じにみえたとしても)。実際には "socket.socket" がコンテキストマ
  ネージャをサポートしています。 socket.py 参照。--)


ssl
---

"ssl" モジュールは、セキュアな (暗号化され、認証された) インターネット
接続のために共通の要求を満足するための数多くの機能を追加しています:

* 新規クラス "SSLContext" は、プロトコル設定や証明書、秘密鍵やそのた色
  々なオプションのような、永続的 SSL データの容器として働きます。その
  中のメソッド "wrap_socket()" を使うと、 SSL コンテキストから SSL ソ
  ケットを作れます。

* A new function, "ssl.match_hostname()", supports server identity
  verification for higher-level protocols by implementing the rules of
  HTTPS (from **RFC 2818**) which are also suitable for other
  protocols.

* The "ssl.wrap_socket()" constructor function now takes a *ciphers*
  argument.  The *ciphers* string lists the allowed encryption
  algorithms using the format described in the OpenSSL documentation.

* 最新版の OpenSSL にリンクしていれば、 "ssl" モジュールは TLS プロト
  コルに対する Server Name Indication 拡張 (訳注: SNI, **RFC 4366**)
  をサポートします。これは単一の IP ポートで異なった証明書を使った複数
  「バーチャルホスト」をもたらすものです。この拡張はクライアントモード
  でのみサポートされ、 "ssl.SSLContext.wrap_socket()" の
  *server_hostname* を渡すことで活性化されます。

* "ssl" モジュールに色々オプションが追加されました。例えば
  "OP_NO_SSLv2" は安全でなく時代遅れの SSLv2 を無効にします。

* 拡張はOpenSSL の全ての暗号とメッセージダイジェストアルゴリズムをロー
  ドします。SSL 証明書の中に検証できないものがある場合、"unknown
  algorithm" エラーを報告します。

* The version of OpenSSL being used is now accessible using the module
  attributes "ssl.OPENSSL_VERSION" (a string),
  "ssl.OPENSSL_VERSION_INFO" (a 5-tuple), and
  "ssl.OPENSSL_VERSION_NUMBER" (an integer).

(Contributed by Antoine Pitrou in bpo-8850, bpo-1589, bpo-8322,
bpo-5639, bpo-4870, bpo-8484, and bpo-8321.)


nntp
----

The "nntplib" 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
"nntplib.NNTP_SSL") and explicit (using "nntplib.NNTP.starttls()") TLS
has also been added.

(Contributed by Antoine Pitrou in bpo-9360 and Andrew Vant in
bpo-1926.)


certificates
------------

"http.client.HTTPSConnection", "urllib.request.HTTPSHandler"
"urllib.request.urlopen()" はサーバ証明書を証明書認証局 (Certification
Authority (CA)) セットと照合するためのオプショナル引数を取るようになり
ました。これは HTTPS の公共利用で推奨されるものです。(---訳注: これは
*check_hostname* パラメータのことを指していますが、3.4.3 でこれの扱い
が変わっているのでリファレンスを注意深く読んで下さい。--)

(Added by Antoine Pitrou, bpo-9003.)


imaplib
-------

標準 IMAP4 接続での明示的な TLS 使用を新規メソッド
"imaplib.IMAP4.starttls" で出来るようになりました。

(Contributed by Lorenzo M. Catucci and Antoine Pitrou, bpo-4471.)


http.client
-----------

"http.client" モジュールに数多くの小さな API 改善がなされています。旧
式 HTTP 0.9 の単純な応答 (HTTP 0.9 simple responses) はもうサポートさ
れず、また、 *strict* パラメータは全てのクラスで非推奨となりました。

"HTTPConnection" クラスと "HTTPSConnection" クラスが *source_address*
パラメータを取るようになりました。これには HTTP(S) 接続の接続元を表す
(host, port) のタプルを渡します。

"HTTPSConnection" に、証明書検証と HTTPS バーチャルホストのサポートが
追加されました。

接続オブジェクトの "request()" メソッドのオプショナルな *body* 引数に
は、リクエストのコンテンツを与えるために *file object* を渡すことが出
来ました。便利のためにこれに *iterable* も渡すことが出来るようになりま
した。ただしこの場合は明示的に "Content-Length" ヘッダを加えなければな
りません。この拡張されたインターフェイスは以前よりも遥かに柔軟です。

プロキシサーバ経由で HTTPS 接続を確立するために、新規メソッド
"set_tunnel()" が追加されています。これにより HTTP 接続トンネリングの
ためのホストとポートをセットします。

"http.server" の振る舞いと合わせ、HTTP クライアントライブラリもヘッダ
を ISO-8859-1 (Latin-1) エンコーディングでエンコードするようになりまし
た。これは既に incoming (外から来る方) ヘッダに対して行われていますが
、今ではこの振る舞いは incoming、outgoing (外へ向かう方) トラフィック
の両方で一貫しています (bpo-10980 内の Armin Ronacher による仕事を参照
)。


unittest
--------

unittest モジュールに数多くの改善がありました。パッケージに対する test
discovery のサポートが追加され、対話プロンプトから実験しやすくなり、新
しいメソッドが追加され、テスト失敗時の診断メッセージが改善され、そして
メソッド名がより良いものになりました。

* The command-line call "python -m unittest" can now accept file paths
  instead of module names for running specific tests (bpo-10620).  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.TestCase" class can now be instantiated without arguments:

  >>> from unittest import TestCase
  >>> TestCase().assertEqual(pow(2, 3), 8)

  (Contributed by Michael Foord.)

* "unittest" モジュールは 2 つの新メソッド "assertWarns()" と
  "assertWarnsRegex()" を持ちます。これらはテストのもとでコードによっ
  て所与の警告が発行されるかどうかを検証します:

     with self.assertWarns(DeprecationWarning):
         legacy_function('XYZ')

  (Contributed by Antoine Pitrou, bpo-9754.)

  もう一つの新メソッド "assertCountEqual()" は 2 つのイテラブルを比較
  して、要素の出現回数が同じか (順序に関わらず同じ要素が同じ数だけ出現
  するかどうか) をテストします:

     def test_anagram(self):
         self.assertCountEqual('algorithm', 'logarithm')

  (Contributed by Raymond Hettinger.)

* unittest モジュールの大事な特長は、テスト失敗時の有意義な診断を生成
  するよう腐心してくれることです。可能ならば失敗は期待値と実際の出力と
  の差分と一緒に記録されます。これは失敗したテスト実行の解析に特に役に
  立ちますが、差分は時々とてつもなく大きくもなりますので、新たに追加し
  た属性 "maxDiff" に差分の最大長をセットすることで表示を制御できるよ
  うになりました。

* さらにモジュールのメソッド名に数多くのクリーンアップを行いました。

  For example, "assertRegex()" is the new name for
  "assertRegexpMatches()" which was misnamed because the test uses
  "re.search()", not "re.match()".  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 "re" module, and
  it has unambiguous camel-casing.

  (Contributed by Raymond Hettinger and implemented by Ezio Melotti.)

* 一貫性を向上させるために、長年使用されたメソッドエイリアスが推奨名に
  対し非推奨になりました:

     +---------------------------------+--------------------------------+
     | 以前の名前                      | 推奨名                         |
     |=================================|================================|
     | "assert_()"                     | "assertTrue()"                 |
     +---------------------------------+--------------------------------+
     | "assertEquals()"                | "assertEqual()"                |
     +---------------------------------+--------------------------------+
     | "assertNotEquals()"             | "assertNotEqual()"             |
     +---------------------------------+--------------------------------+
     | "assertAlmostEquals()"          | "assertAlmostEqual()"          |
     +---------------------------------+--------------------------------+
     | "assertNotAlmostEquals()"       | "assertNotAlmostEqual()"       |
     +---------------------------------+--------------------------------+

  Likewise, the "TestCase.fail*" methods deprecated in Python 3.1 are
  expected to be removed in Python 3.3.

  (Contributed by Ezio Melotti; bpo-9424.)

* 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.)


random
------

"random" モジュールの整数メソッドの一様分布の生成が改善されました。 以
前は "int(n*random())" を計算していました。 この方法では *n* が2の冪乗
でない限り僅かにバイアスがあります。 現在の方法では次の2の冪乗の範囲ま
で複数の選択肢を作成し、"0 <= x < n" の範囲に入ったもののみを保持しま
す。 影響を受ける関数やメソッドは "randrange()"、"randint()"、
"choice()"、"shuffle()" ならびに "sample()" です。

(Contributed by Raymond Hettinger; bpo-9025.)


poplib
------

"POP3_SSL" クラスが *context* パラメータを受け取れるようになりました。
これには "ssl.SSLContext" オブジェクトを渡します。これは SSL コンフィ
グレーションオプション、証明書、秘密鍵を単一の (たぶん長生きの) 構造に
まとめあげてくれるものです。

(Contributed by Giampaolo Rodolà; bpo-8807.)


asyncore
--------

"asyncore.dispatcher" now provides a "handle_accepted()" 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 "handle_accept()" and avoids the
user  to call "accept()" directly.

(Contributed by Giampaolo Rodolà; bpo-6706.)


tempfile
--------

"tempfile" モジュールに新たなコンテキストマネージャ
"TemporaryDirectory" が追加されました。一時ディレクトリの確実な後片付
けを容易に行えます:

   with tempfile.TemporaryDirectory() as tmpdirname:
       print('created temporary dir:', tmpdirname)

(Contributed by Neil Schemenauer and Nick Coghlan; bpo-5178.)


inspect
-------

* "inspect" モジュールに新たな関数 "getgeneratorstate()" が追加されま
  した。これで簡単にジェネレータイテレータの現在状態を識別出来ます
  (---訳注: generator-iterator という用語を使っている箇所は Python ド
  キュメント全体通して多くはないのですが 2.x の yield 文 説明中に登場
  します。ジェネレータと同じ意味です。なお getgeneratorstate はジェネ
  レータでないイテレータには使えません。---):

     >>> 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.)

* 動的な属性を活性化することなく検索するのをサポートするために、
  "inspect" モジュールに "getattr_static()" 関数が追加されました。
  "hasattr()" とは違ってこれは本当に読み出しのみの検索であり、検索中に
  状態変更しないことが保障されています:

     >>> class A:
     ...     @property
     ...     def f(self):
     ...         print('Running')
     ...         return 10
     ...
     >>> a = A()
     >>> getattr(a, 'f')
     Running
     10
     >>> inspect.getattr_static(a, 'f')
     <property object at 0x1022bd788>

   (Contributed by Michael Foord.)


pydoc
-----

The "pydoc" 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.)


dis
---

"dis" モジュールに、コードを調べるための 2 つの新しい関数
"code_info()" と "show_code()" が追加されました。ともに、与えられた関
数、メソッド、ソースコード文字列、コードオブジェクトについての詳細なコ
ードオブジェクト情報を提供します。前者はそれを文字列で返し、後者は印字
します:

   >>> 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

また、 "dis()" 関数が文字列引数を取れるようになり、これによって、よく
あるイディオム "dis(compile(s, '', 'eval'))" は "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

要はこれらによって、CPython がどう実装されているか探索してみたり、言語
構文が裏方で何をするのか見てみたりすることがより簡単になったということ
です。

(Contributed by Nick Coghlan in bpo-9147.)


dbm
---

All database modules now support the "get()" and "setdefault()"
methods.

(Suggested by Ray Allen in bpo-9523.)


ctypes
------

C "ssize_t" データ型を表現する新たな型 "ctypes.c_ssize_t" が追加されま
した。


site
----

"site" モジュールに、Python インストレーションの詳細を報告するのに有益
な3つの新たな関数が追加されました。

* "getsitepackages()" は全てのグローバルな site-packages ディレクトリ
  を一覧します。

* "getuserbase()" は、ユーザのデータを格納できるベースディレクトリを報
  告します。

* "getusersitepackages()" はユーザ固有の site-packages ディレクトリの
  パスを明らかにします。

   >>> 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'

便宜のために、いくつかの site の機能はコマンドラインから直接アクセス可
能です:

   $ 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.)


sysconfig
---------

新しい "sysconfig" モジュールは、プラットフォームやインストレーション
ごとに変わるインストールパスや環境設定変数への、直接的なアクセスを提供
します。

このモジュールはプラットフォームとバージョンの情報へのシンプルなアクセ
ス関数を持っています:

* "get_platform()" は *linux-i586* や *macosx-10.6-ppc* といった値を返
  します。

* "get_python_version()" は Python バージョンを文字列で "3.2" のように
  返します。

It also provides access to the paths and variables corresponding to
one of seven named schemes used by "distutils".  Those include
*posix_prefix*, *posix_home*, *posix_user*, *nt*, *nt_user*, *os2*,
*os2_home*:

* "get_paths()" は現在のインストールスキームに関するインストールパスを
  含むディレクトリ名を作ります。

* "get_config_vars()" はプラットフォーム依存変数の辞書を返します。

便利なコマンドラインインターフェースもあります:

   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é.)


pdb
---

"pdb" デバッガモジュールに数多くのユーザビリティの改善がなされました:

* "pdb.py" は "-c" オプションで指定した ".pdbrc" スクリプトファイルを
  実行できるようになっています。

* ".pdbrc" スクリプトファイルにはデバッグを継続する "continue" コマン
  ドと "next" コマンドを含むことができます。

* The "Pdb" class constructor now accepts a *nosigint* argument.

* 新コマンド: "l(list)", "ll(long list)", "source" はソースコードを出
  力します。

* 新コマンド:  "display", "undisplay" は、変更があった場合の式の値の表
  示/非表示を切り替えます。

* 新コマンド: "interact" は現在のスコープで見つかったグローバルおよび
  ローカル名を含む対話型インタプリタを開始します。

* ブレイクポイントをブレイクポイント番号指定でクリア出来ます。

(Contributed by Georg Brandl, Antonio Cuni and Ilya Sandler.)


configparser
------------

The "configparser" 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 "ConfigParser".
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 はマッピングプロトコルにもとづく新しい API を獲得しまし
た:

   >>> 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)'

新しい API は旧式 API の上に実装されているので、カスタムなパーサーサブ
クラスは変更なしで使えるはずです。

config parser によって受け付けられる INI ファイルの構造は今ではカスタ
マイズ出来ます。ユーザは、オプションと値を区切る代替のデリミタやコメン
トのプレフィクスを指定でき、 *DEFAULT* セクションの名前を変えることが
でき、あるいは補間の構文を切り替えられます。

抜き挿し可能 (pluggable) な補間サポートがあって、これには追加の補間ハ
ンドラ "ExtendedInterpolation" が含まれています:

   >>> 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'

小さな機能もたくさん導入されています。例えば読み込み操作内でのエンコー
ディング指定のサポート、取得関数でのフォールバック値の指定、あるいは辞
書や文字列から直接的に読むことなどです。

(All changes contributed by Łukasz Langa.)


urllib.parse
------------

数多くのユーザビリティーの改善が "urllib.parse" モジュールになされまし
た。

"urlparse()" 関数が **RFC 2732** にて記述されている IPv6 アドレスをサ
ポートするようになりました:

>>> 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='')

"urldefrag()" 関数は、今や *named tuple* を返します:

   >>> 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'

また、  "urlencode()" 関数はさらに柔軟になり、 *query* 引数としては文
字列とバイト列のどちらも受け付けます。文字列で渡した場合、パラメータ
*safe*, *encoding*, *error* がエンコードのために "quote_plus()" に送ら
れます:

   >>> urllib.parse.urlencode([
   ...      ('type', 'telenovela'),
   ...      ('name', '¿Dónde Está Elisa?')],
   ...      encoding='latin-1')
   'type=telenovela&name=%BFD%F3nde+Est%E1+Elisa%3F'

ASCII エンコードバイト列の解析 に詳細に書きましたが、全ての
"urllib.parse" 関数は今では ASCII エンコードされたバイト文字列を (通常
の文字列と混ぜない限り) 入力として受け付けます。ASCII エンコードされた
バイト文字列がパラメータとして与えられると、戻り値の型も ASCII エンコ
ードされたバイト文字列になります:

>>> 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,
bpo-5468, and bpo-9873.)


mailbox
-------

R. David Murray がコミュニティの調整に尽力したおかげで、 "mailbox" が
Python 3.2 のために修正されました。闘いを挑んだ相手はもともとの
mailbox 設計のテキストインターフェイスでした。ですが email メッセージ
は個々の異なるパートが異なるエンコーディングを持ち得ますから、 "bytes"
が最良の表現です。

解決のためには任意の email メッセージをパースするために、 "email" パッ
ケージのバイナリサポートが利用されました (---訳注: "BytesGenerator" や
"message_from_bytes()" など)。また、API の変更も必要となりました。

"mailbox.Mailbox" オブジェクトの "add()" メソッドは、今や期待通りにバ
イナリ入力を受け付けます。

"StringIO" とテキストファイルでの入力は非推奨となりました。また、文字
列での入力は、非 ASCII 文字が使用されていると早々に失敗します。以前は
後のほうのステップで email が処理される際にようやく失敗していました。

バイナリ出力のサポートもあります。 "get_file()" メソッドは今ではバイナ
リモードのファイルを返します (これまでは不適切にテキストモードが使われ
ていました)。また、新しい "get_bytes()" メソッドは、指定した *key* に
対応するメッセージの "bytes" 表現を返します。

今でも古い API の "get_string()" を使って非バイナリ出力は可能ですが、
そのアプローチはあまり役に立ちません。そうではなく "Message" オブジェ
クトからメッセージを抽出するか、それらをバイナリ入力からロードするのが
最善です。

(Contributed by R. David Murray, with efforts from Steffen Daode
Nurpmeso and an initial patch by Victor Stinner in bpo-9124.)


turtledemo
----------

The demonstration code for the "turtle" module was moved from the
*Demo* directory to main library.  It includes over a dozen sample
scripts with lively displays.  Being on "sys.path", it can now be run
directly from the command-line:

   $ python -m turtledemo

(Moved from the Demo directory by Alexander Belopolsky in bpo-10199.)


マルチスレッディング
====================

* The mechanism for serializing execution of concurrently running
  Python threads (generally known as the *GIL* 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 "sys.setswitchinterval()". It currently
  defaults to 5 milliseconds.

  この実装に関するさらなる詳細は python-dev mailing-list message で読
  むことが出来ます (ただしこのメッセージで言っている "priority
  requests" は含められませんでした)。

  (Contributed by Antoine Pitrou.)

* 通常のロックと再帰的なロックが、"acquire()" メソッドでオプションで
  *timeout* 引数を受け取るようになりました。 (Contributed by Antoine
  Pitrou; bpo-7316.)

* 同様に、"threading.Semaphore.acquire()" にも *timeout* 引数が追加さ
  れました。 (Contributed by Torsten Landschoff; bpo-850728.)

* 通常のロックと再帰的なロックの獲得に、 Pthreads を使用するプラットフ
  ォームでシグナルが割り込めるようになりました。これでロック獲得時にデ
  ッドロックしていた Python プログラムは、プロセスに SIGINT (ほとんど
  のシェルでは "Ctrl+C" で) を繰り返し送ることで殺せます。(Contributed
  by Reid Kleckner; bpo-8844.)


最適化
======

小さな性能向上が多く追加されました:

* 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 "set" as a "frozenset" 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).

* "pickle" モジュールを用いたデータのシリアライズ・デシリアライズが数
  倍速くなりました。

  (Contributed by Alexandre Vassalotti, Antoine Pitrou and the Unladen
  Swallow team in bpo-9410 and bpo-3873.)

* The Timsort algorithm used in "list.sort()" and "sorted()" now runs
  faster and uses less memory when called with a *key function*.
  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.)

* 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 and by Raymond Hettinger
  and Antoine Pitrou in bpo-10314.)

* 再帰的ロック ("threading.RLock()" API により生成) は C で実装された
  ことにより、以前の純粋な Python 実装より10から15倍速くなり、通常のロ
  ックと同じくらいになりました。

  (Contributed by Antoine Pitrou; bpo-3001.)

* The fast-search algorithm in stringlib is now used by the "split()",
  "rsplit()", "splitlines()" and "replace()" methods on "bytes",
  "bytearray" and "str" objects. Likewise, the algorithm is also used
  by "rfind()", "rindex()", "rsplit()" and "rpartition()".

  (Patch by Florent Xicluna in bpo-7622 and bpo-7462.)

* Integer to string conversions now work two "digits" at a time,
  reducing the number of division and modulo operations.

  (bpo-6713 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).  The "array.repeat()" method has a
faster implementation (bpo-1569291 by Alexander Belopolsky). The
"BaseHTTPRequestHandler" has more efficient buffering (bpo-3709 by
Andrew Schaaf).  The "operator.attrgetter()" function has been sped-up
(bpo-10160 by Christos Georgiou).  And "ConfigParser" loads multi-line
arguments a bit faster (bpo-7113 by Łukasz Langa).


Unicode 文字列型
================

Python has been updated to Unicode 6.0.0.  The update to the standard
adds over 2,000 new characters including emoji 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.


Codecs
======

*cp720* アラビア DOS エンコードがサポートされました (bpo-1616979)。

MBCS エンコーディングはもうエラーハンドラ引数を無視することはありませ
ん。デフォルトの strict モードでは、デコード出来ないバイト列に対して
"UnicodeDecodeError" を、エンコードできない文字に対して
"UnicodeEncodeError" を投げます。

MBCS コーデックはエラーハンドラとして、デコードでの "'strict'" と
"'ignore'" 、エンコードでの "'strict'" と "'replace'" をサポートします
。

Python 3.1 での MBCS エンコーディングを模擬したければデコードでは
"'ignore'" ハンドラを、エンコードでは "'replace'" ハンドラを選んでくだ
さい。

Mac OS X では Python は、コマンドライン引数をロケールのエンコーディン
グではなく "'utf-8'" でデコードします。

"tarfile" はデフォルトで、Windows では ("'mbcs'" ではなく) "'utf-8'"
エンコーディングを使い、また、全ての OS でエラーハンドラ
"'surrogateescape'" を使います。


ドキュメント
============

ドキュメントの改善が引き続き行われています。

* 組み込み関数 のような長大なセクションには先頭にクイックリンクの表が
  追加されました。 "itertools" のようなケースでは cheatsheet スタイル
  の表からリンクが張られていて、これで要約がわかりますし、ドキュメント
  全体を読む必要なく記憶を呼び覚ますことが出来ます。

* ある種のケースではピュア Python のソースコードが文書化の助手として便
  利に働いてくれますので、今では多くのモジュールがソースコードの最新バ
  ージョンへのクイックリンクを客演させています。例えば "functools" モ
  ジュールのドキュメントは先頭にこのようにクイックリンクを置いています
  :

     **ソースコード** Lib/functools.py.

  (Contributed by Raymond Hettinger; see rationale.)

* ドキュメントには今ではもっと実例とレシピが含まれています。特に "re"
  モジュールのものには大掛かりなセクション 正規表現の例 があります。同
  じように "itertools" では新しい Itertools レシピ で更新され続けます
  。

* "datetime" モジュールは今ではピュア Python による補助実装を持ってい
  ます。機能に違いはありません。これは読みやすい代替の実装を提供するだ
  けのものです。

  (Contributed by Alexander Belopolsky in bpo-9528.)

* 保守されていなかった "Demo" ディレクトリは削除されました。いくつかの
  デモはドキュメントに統合され、いくつかは "Tools/demo" ディレクトリに
  移動し、ほかのものは一緒に削除されました。

  (Contributed by Georg Brandl in bpo-7962.)


IDLE
====

* IDLE のフォーマットメニューに、ソースコードから末尾の空白を取り除く
  ことが出来るオプションが追加されました。

  (Contributed by Raymond Hettinger; bpo-5150.)

* Mac OS X 上の IDLE が Carbon AquaTk と Cocoa AquaTk の両方で動くよう
  になりました。

  (Contributed by Kevin Walzer, Ned Deily, and Ronald Oussoren;
  bpo-6075.)


コードリポジトリ
================

In addition to the existing Subversion code repository at
https://svn.python.org there is now a Mercurial repository at
https://hg.python.org/.

3.2 のリリース後に、主要なリポジトリを Mercurial に移行する計画があり
ます。 この分散バージョン管理システムによってコミュニティメンバが容易
に外部 changeset を作成したり共有したりすることが出来ます。 詳細は
**PEP 385** を参照してください。

To learn to use the new version control system, see the Quick Start or
the Guide to Mercurial Workflows.


ビルドならびに C API の変更
===========================

Python のビルド過程と C API の変更は以下の通りです:

* スクリプト *idle*, *pydoc*, *2to3* が "make altinstall" 時にバージョ
  ン固有のサフィックスを付けてインストールされるようになりました
  (bpo-10679)。

* Unicode データベースにアクセスする関数 (Py_UNICODE_TOLOWER,
  Py_UNICODE_ISDECIMAL, ほか) が、たとえ narrow Unicode ビルドであって
  も full Unicode 範囲を受け付け、返すようになりました。Python から目
  に見える違いとしては、大きなコードポイントについて
  "unicodedata.numeric()" が正しい値を返し、 "repr()" はより多くの文字
  を表示可能と考えるようになりました。

  (Reported by Bupjoe Lee and fixed by Amaury Forgeot D'Arc;
  bpo-5127.)

* 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.)

* オプション "--with-wctype-functions" は削除されました。今では組み込
  みの Unicode データベースが全ての関数のために使われます。

  (Contributed by Amaury Forgeot D'Arc; bpo-9210.)

* ハッシュ値として新しい型 "Py_hash_t" を使うようになりました。これは
  ポインタと同じサイズになるように定義されています。以前は long 型を使
  っていましたが、これはいくつかの 64 ビット OS でも 32 ビット長のまま
  でした。この修正の結果、64 ビットポインタでビルドすれば "set" と
  "dict" が "2**32" 以上のエントリを持てるようになりました (以前もその
  サイズを入れることは出来ましたが、悲劇的なパフォーマンス低下を起こし
  ていました)。

  (Suggested by Raymond Hettinger and implemented by Benjamin
  Peterson; bpo-9778.)

* 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).

* A new C API function "PySys_SetArgvEx()" allows an embedded
  interpreter to set "sys.argv" without also modifying "sys.path"
  (bpo-5753).

* "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).

* "PyLong_AsLongAndOverflow()" の姉妹品の
  "PyLong_AsLongLongAndOverflow()" 関数が追加されました。両方とも
  Python "int" を C ネイティブの固定幅型に変換しますが、変換が幅に合わ
  ないことを検出します (bpo-7767)。

* "PyUnicode_CompareWithASCIIString()" 関数が、Python 文字列が *NUL*
  終端である場合に「 *等しくない* 」を返すようになりました。

* 新しい関数 "PyErr_NewExceptionWithDoc()" は "PyErr_NewException()"
  と似ていますが、ドキュメンテーション文字列を受け付けます。これは C
  拡張に、対応するピュア Python と同じ自己文書化 (self-documenting) 能
  力を与えます (bpo-7033)。

* "--with-valgrind" オプションとともにコンパイル時、 Valgrind のもとで
  の実行時に自動的に pymalloc アロケータが無効になるようになりました。
  ほかの状況では pymalloc は有利ですが、Valgrind 配下での実行時のメモ
  リリーク検出では無効化したほうが良い結果が得られます (bpo-2422)。

* *PyArg_Parse* 関数から書式文字列 "O?" が削除されました。これはもう使
  われていませんし、今まで一度も文書化されたことはありません
  (bpo-8837)。

There were a number of other small changes to the C-API.  See the
Misc/NEWS file for a complete list.

Also, there were a number of updates to the Mac OS X build, see
Mac/BuildScript/README.txt 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. See
https://www.python.org/download/mac/tcltk/ for additional details.


Python 3.2 への移植
===================

このセクションでは前述の変更とバグフィックスにより必要となるかもしれな
いコードの変更を列挙します:

* The "configparser" 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:

  * 補間 (interpolation) の構文は今では "get()" と "set()" の操作で検
    証されます。デフォルトの補間スキームでは、パーセント記号を使ったた
    だ二つのトークンのみが正当です: "%(name)s" と "%%" 、後者はエスケ
    ープされたパーセント記号自身。

  * "set()" メソッドと "add_section()" メソッドは今ではその値が実際に
    文字列かどうかを検証します。以前はサポートされない型が意図せず持ち
    込まれる可能性がありました。

  * 単一ソースからのセクションまたはオプションの複製 (重複) に対し、今
    では "DuplicateSectionError" もしくは "DuplicateOptionError" を投
    げます。以前は重複は黙って以前のエントリを上書きしていました。

  * インラインコメントは今ではデフォルトで無効なので、 **;** 文字を安
    全に値のなかで使えます。

  * コメントを字下げ出来るようになっています。このことにより、値が継続
    行となる場合の後続行の字下げ開始位置に **;** または **#** を置くに
    は、補間を使う必要があります。こうすればコメントのプレフィクス文字
    が値内に現れて誤ってコメントと解釈されるのを防げます。

  * """" は今では合法な値なので、もはや自動的に空文字列に変換されるこ
    とはありません。空文字列のためには行内で ""option ="" と書いてくだ
    さい。

* The "nntplib" module was reworked extensively, meaning that its APIs
  are often incompatible with the 3.1 APIs.

* "bytearray" オブジェクトはもうファイル名には使えません。代わりに
  "bytes" に変換しなければいけません。

* 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.)

* "PyArg_Parse*()" 系関数:

  * 書式文字列 "t#" の削除: 代わりに "s#" もしくは "s*" を使ってくださ
    い。

  * 書式文字列 "w" および "w#" の削除: 代わりに "w*" を使ってください
    。

* The "PyCObject" type, deprecated in 3.1, has been removed.  To wrap
  opaque C pointers in Python objects, the "PyCapsule" 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.

* "random.seed()" 関数とメソッドが、salt に文字列やバイト列を与えた場
  合に内部で sha512 ハッシュ関数を使うようになりました。Python 3.1 シ
  ーケンスを再現するのに以前のバージョンの *seed* が必要であれば、
  *version* 引数を *1* にしてください。 "random.seed(s, version=1)" の
  ように。

* The previously deprecated "string.maketrans()" function has been
  removed in favor of the static methods "bytes.maketrans()" and
  "bytearray.maketrans()".  This change solves the confusion around
  which types were supported by the "string" module.  Now, "str",
  "bytes", and "bytearray" each have their own **maketrans** and
  **translate** methods with intermediate translation tables of the
  appropriate type.

  (Contributed by Georg Brandl; bpo-5675.)

* The previously deprecated "contextlib.nested()" function has been
  removed in favor of a plain "with" 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 '<critical>' in line:
                 outfile.write(line)

  (Georg Brandl と Mattias Brändström の貢献; appspot issue 53094.)

* "struct.pack()" は、文字列パックコード "s" で bytes のみを許容するよ
  うになりました。以前はテキスト引数を受け取って暗黙に UTF-8 でエンコ
  ードして bytes に変換していました。特定のエンコーディングを前提にす
  るのは間違っていますし、可変長のエンコーディングは構造体の固定長セグ
  メントへの書き込みで失敗しうるので、これは問題でした。

  例えば "struct.pack('<6sHHBBB', 'GIF87a', x, y)" としているならば、
  テキストではなく bytes を使うように "struct.pack('<6sHHBBB',
  b'GIF87a', x, y)" と書き換えなければなりません。

  (Discovered by David Beazley and fixed by Victor Stinner;
  bpo-10783.)

* パース失敗時に "xml.etree.ElementTree" クラスが
  "xml.etree.ElementTree.ParseError" を投げるようになりました。以前は
  "xml.parsers.expat.ExpatError" を投げていました。

* 浮動小数点数と複素数に対する "str()" が以前より長い表現をするように
  なったことで、古いフォーマットに依存する doctest は壊れるかもしれま
  せん。

* "subprocess.Popen" の *close_fds* のデフォルトが、Unix では "True"
  になりました。Windows では 3 つの標準ストリームに "None" がセットさ
  れている場合には "True" 、以外の場合は "False" になりました。以前は
  *close_fds* のデフォルトはいつでも "False" でした。これは開いている
  ファイル記述子が子プロセスに漏れた場合に解決が難しいバグや競合状態を
  生み出していました。

* レガシーな HTTP 0.9 のサポートが "urllib.request" と "http.client"
  から削除されました。このサポートはサーバサイドではまだ健在です
  ("http.server" 内)。

  (Contributed by Antoine Pitrou, bpo-10711.)

* タイムアウトモード時の SSL ソケットでタイムアウトした場合に、汎用の
  "SSLError" ではなく "socket.timeout" を投げるようになりました。

  (Contributed by Antoine Pitrou, bpo-10272.)

* The misleading functions "PyEval_AcquireLock()" and
  "PyEval_ReleaseLock()" have been officially deprecated.  The thread-
  state aware APIs (such as "PyEval_SaveThread()" and
  "PyEval_RestoreThread()") 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.)

* Due to the new *GIL* implementation, "PyEval_InitThreads()" cannot
  be called before "Py_Initialize()" anymore.
