파이썬 3.4의 새로운 기능¶
- 저자
R. David Murray <rdmurray@bitdance.com> (편집자)
이 기사에서는 3.3과 비교하여 파이썬 3.4의 새로운 기능에 관해 설명합니다. 파이썬 3.4는 2014년 3월 16일에 출시되었습니다. 자세한 내용은, changelog를 참조하십시오.
더 보기
PEP 429 -- 파이썬 3.4 출시 일정
요약 -- 배포 주요 사항¶
새로운 문법 기능:
파이썬 3.4에는 새로운 문법 기능이 추가되지 않았습니다.
기타 새로운 기능:
텍스트 인코딩이 아닌 코덱 처리 개선 (여러 이슈).
임포트 시스템을 위한 ModuleSpec 형 (PEP 451). (임포터 저자에게 영향을 미칩니다.)
새로운 라이브러리 모듈:
selectors: 고 수준의 효율적인 I/O 멀티플렉싱,select모듈 프리미티브를 기반으로합니다 (PEP 3156의 일부).statistics: 기초적인 수치적으로 안정적인 통계 라이브러리 (PEP 450).
크게 개선된 라이브러리 모듈:
multiprocessing에는 이제 유닉스에서 os.fork 사용을 피하는 옵션이 있습니다 (bpo-8713).email에는 새로운 하위 모듈인contentmanager와 MIME 처리를 단순화하는 새로운Message서브 클래스(EmailMessage)가 있습니다 (bpo-18891).inspect와pydoc모듈은 이제 훨씬 더 다양한 콜러블 객체를 올바르게 인트로스펙트할 수 있어서 파이썬help()시스템의 출력을 향상시킵니다.ipaddress모듈 API가 안정적이라고 선언되었습니다.
보안 향상:
파일 기술자가 자식 프로세스로 누수되는 것을 방지하기위해 새로 만들어지는 파일 기술자를 상속 불가능하게 만듭니다 (PEP 446).
multiprocessing에는 이제 유닉스에서 os.fork 사용을 피하는 옵션이 있습니다. spawn과 forkserver는 자식 프로세스와 데이터를 공유하지 않기 때문에 더 안전합니다.윈도우의
multiprocessing자식 프로세스는 더는 부모의 상속 가능한 핸들을 모두 상속하지 않고 필요한 것만 상속합니다.새로운
hashlib.pbkdf2_hmac()함수는 PKCS#5 암호 기반 키 파생 함수 2를 제공합니다.ssl.SSLContext클래스에는 많은 개선이 있습니다.SSL을 지원하는 표준 라이브러리의 모든 모듈은 이제 호스트 이름 일치(
ssl.match_hostname())와 CRL(인증서 해지 목록,ssl.SSLContext.load_verify_locations()를 참조하십시오)을 포함하여 서버 인증서 확인을 지원합니다.
CPython 구현 개선:
PEP 442를 활용하여, 대부분의 경우 모듈 전역이 더는 파이널리제이션 중에 None으로 설정되지 않습니다 (bpo-18214).
다른 많은 소규모 개선 사항, CPython 최적화, 폐지 및 잠재적 이식 문제를 포함하여 사용자가 직면하는 변경 사항의 포괄적인 목록을 계속 읽으십시오.
새로운 기능¶
PEP 453: 파이썬 설치 시 PIP의 명시적 부트 스트래핑¶
기본적으로 pip 부트 스트랩하기¶
새로운 (PEP 453에 정의된) ensurepip 모듈은 pip 설치 프로그램을 파이썬 설치와 가상 환경으로 부트 스트랩하는 표준 교차 플랫폼 메커니즘을 제공합니다. 파이썬 3.4.0에 포함된 pip 버전은 pip 1.5.4이며, 향후 3.4.x 유지 보수 릴리스는 포함된 버전을 릴리스 후보를 만드는 시점에 사용 가능한 최신 버전의 pip로 갱신할 것입니다.
기본적으로, pipX와 pipX.Y 명령은 pip 파이썬 패키지와 해당 종속성과 함께 모든 플랫폼에 설치됩니다 (여기서 X.Y는 파이썬 설치 버전을 나타냅니다). 윈도우와 모든 플랫폼의 가상 환경에서 버전이 지정되지 않은 pip 명령도 설치됩니다. 다른 플랫폼에서는, 시스템 전체에 적용되는 버전이 지정되지 않은 pip 명령은 일반적으로 별도로 설치된 파이썬 2 버전을 가리킵니다.
pyvenv 명령 줄 유틸리티와 venv 모듈은 ensurepip 모듈을 사용하여 pip를 가상 환경에서 쉽게 사용할 수 있도록 합니다. 명령 줄 유틸리티를 사용할 때, pip가 기본적으로 설치되지만, venv 모듈을 사용할 때는 pip의 API 설치를 명시적으로 요청해야 합니다.
CPython의 POSIX 시스템에서의 소스 빌드의 경우, make install과 make altinstall 명령은 기본적으로 pip를 부트 스트랩합니다. 이 동작은 구성 옵션을 통해 제어할 수 있으며, Makefile 옵션을 통해 재정의 할 수 있습니다.
윈도우와 Mac OS X에서, CPython 설치 프로그램은 이제 CPython 자체와 함께 pip를 기본적으로 설치합니다 (사용자는 설치 프로세스 중에 설치를 거부할 수 있습니다). 윈도우 사용자는 기본적으로 명령 줄에서 pip를 사용할 수 있도록 자동 PATH 수정을 선택해야합니다, 그렇지 않으면 윈도우 용 파이썬 런처를 통해 py -m pip로 계속 액세스 할 수 있습니다.
PEP 에서 논의되었듯이, 이 명령을 호출할 때 (일반적으로 시스템 패키지 관리자를 사용해서) 해당 플랫폼에 설치하는 방법에 대한 명확하고 간단한 지침을 제공하는 한, 플랫폼 패키저는 기본적으로 이러한 명령을 설치하지 않도록 선택할 수 있습니다.
참고
파이썬 2와 파이썬 3의 병행 설치 간의 충돌을 방지하기 위해, ensurepip가 직접 호출될 때 기본적으로 버전이 지정된 pip3와 pip3.4 명령만 부트 스트랩됩니다 - 버전이 지정되지 않은 pip 명령도 요청하려면 --default-pip 옵션이 필요합니다. pyvenv와 윈도우 설치 프로그램은 해당 환경에서 버전이 지정되지 않은 pip 명령을 사용할 수 있도록 보장하며, 여러 파이썬 설치가 있는 시스템에서 모호함을 피하기 위해 pip는 직접 호출되는 대신 항상 -m 스위치를 통해 호출될 수 있습니다.
문서 변경¶
이 변경의 일환으로, 문서의 파이썬 모듈 설치하기와 파이썬 모듈 배포하기 섹션이 짧은 시작 및 FAQ 문서로 완전히 재 설계되었습니다. 대부분의 패키징 문서는 이제 파이썬 패키징 사용자 지침서와 개별 프로젝트의 설명서를 유지하는 파이썬 패키징 위원회(Python Packaging Authority)로 옮겨졌습니다.
그러나, 이 마이그레이션은 현재 아직 불완전하므로, 해당 지침서의 레거시 버전은 파이썬 모듈 설치 (레거시 버전)와 파이썬 모듈 배포 (레거시 버전)에서 계속 제공됩니다.
더 보기
- PEP 453 -- 파이썬 설치시 pip의 명시적 부트 스트랩
Donald Stufft와 Nick Coghlan이 작성한 PEP, Donald Stufft, Nick Coghlan, Martin von Löwis 및 Ned Deily가 구현했습니다.
PEP 446: 새로 만든어진 파일 기술자는 상속할 수 없습니다¶
PEP 446은 새로 만들어진 파일 기술자를 상속 불가능으로 만듭니다. 일반적으로, 이것은 응용 프로그램이 원하는 동작입니다: 새 프로세스를 시작할 때, 현재 열려있는 파일이 새 프로세스에서도 열리면 찾기 어려운 모든 종류의 버그로 이어지고, 잠재적으로 보안 문제가 발생할 수 있습니다.
그러나, 상속이 필요한 경우가 있습니다. 이러한 경우를 지원하기 위해, 다음과 같은 새로운 함수와 메서드를 제공합니다:
더 보기
- PEP 446 -- 새로 만들어진 파일 기술자를 상속 불가능하게 만듭니다
Victor Stinner가 작성하고 구현 한 PEP.
코덱 처리 개선¶
처음 도입 된 이후, codecs 모듈은 항상 형 중립 동적 인코딩과 디코딩 시스템으로 작동하도록 설계되었습니다. 그러나, 파이썬 텍스트 모델, 특히 내장 str, bytes 및 bytearray 형에 대한 형 제한 편의 메서드와의 밀접한 결합은, 역사적으로 그 사실을 모호하게 만들었습니다.
상황을 명확히 하기 위한 핵심 단계로, codecs.encode()와 codecs.decode() 편의 함수가 이제 파이썬 2.7, 3.3 및 3.4에서 올바르게 설명되었습니다. 이 함수들은 파이썬 2.4부터 codecs 모듈에 존재했지만 (그리고 회귀 테스트 스위트에서 다루었습니다), 이전에는 실행시간 인트로스펙션을 통해서만 발견할 수 있었습니다.
str, bytes 및 bytearray의 편의 메서드와 달리, codecs 편의 함수는 유니코드 텍스트 인코딩(파이썬 3에서)이나 basestring <-> basestring 변환(파이썬 2에서)으로 제한되지 않고, 파이썬 2와 파이썬 3 모두에서 임의의 코덱을 지원합니다.
파이썬 3.4에서, 인터프리터는 표준 라이브러리에서 제공되는 알려진 비 텍스트 인코딩을 식별하고 적절한 경우 다음과 같은 범용 편의 함수로 사용자를 안내할 수 있습니다:
>>> b"abcdef".decode("hex")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs
>>> "hello".encode("rot13")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
LookupError: 'hex' is not a text encoding; use codecs.open() to handle arbitrary codecs
관련 변경에서, 이전 버전과의 호환성을 깨지 않고 실행 가능할 때마다, 인코딩과 디코딩 연산 중에 발생한 예외는 에러를 생성하는 코덱의 이름을 언급하는 같은 형의 연결된 예외(chained exception)로 래핑됩니다:
>>> 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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
TypeError: encoding with 'bz2' codec failed (TypeError: 'str' does not support the buffer interface)
마지막으로, 위의 예에서 볼 수 있듯이, 이러한 개선으로 인해 파이썬 3.2에서 자체적으로 복원된 비 유니코드 코덱의 편의 별칭을 복원할 수 있었습니다. 즉, 바이너리 데이터를 16진수 표현 으로부터/으로 인코딩하는 것은 (예를들어) 이제 다음과 같이 작성할 수 있습니다:
>>> from codecs import encode, decode
>>> encode(b"hello", "hex")
b'68656c6c6f'
>>> decode(b"68656c6c6f", "hex")
b'hello'
표준 라이브러리에서 제공되는 바이너리와 텍스트 변환은 바이너리 변환과 텍스트 변환에 자세히 설명되어 있습니다.
(Contributed by Nick Coghlan in bpo-7475, bpo-17827, bpo-17828 and bpo-19619.)
PEP 451: 임포트 시스템을 위한 ModuleSpec 형¶
PEP 451은 임포트 절차가 모듈을 로드하는 데 사용할 모듈 (즉, 모듈 명세)에 대한 정보의 캡슐화를 제공합니다. 이는 임포트 구현과 여러 임포트 관련 API를 단순화하는 데 도움이됩니다. 변경은 여러 미래의 임포트 관련 구현의 디딤돌이기도 합니다.
PEP의 공개 변경 사항은 완전히 이전 버전과 호환됩니다. 또한 임포터 저자를 제외한 모든 사람에게 투명해야합니다. 키 파인더와 로더 메서드는 폐지되었지만, 계속 작동합니다. 새로운 임포터는 PEP에 설명된 새로운 메서드를 사용해야합니다. 새 메서드를 구현하려면 기존 임포터를 업데이트해야 합니다. 교체해야 하는 메서드와 이들이 교체될 메서드 목록은 폐지 섹션을 참조하십시오.
기타 언어 변경¶
핵심 파이썬 언어에 대한 몇 가지 작은 변경 사항은 다음과 같습니다:
유니코드 데이터베이스가 UCD 버전 6.3으로 갱신되었습니다.
min()과max()는 이제 평가중인 이터러블에 요소가 없는 경우 반환하는 값을 지정하는 데 사용할 수 있는 default 키워드 전용 인자를 받아들입니다. (Contributed by Julian Berman in bpo-18111.)모듈 객체는 이제
weakref가능합니다.모듈
__file__어트리뷰트(및 관련 값)는 이제 스크립트가 상대 경로를 사용하여 직접 실행될 때의__main__.__file__을 제외하고, 기본적으로 항상 절대 경로를 포함해야합니다. (Contributed by Brett Cannon in bpo-18416.)모든 UTF-* 코덱(UTF-7 제외)은 이제
surrogatepass에러 처리기를 사용하지 않는 한 인코딩과 디코딩 중에 서로게이트를 거부합니다. 단, UTF-16 디코더(이것은 유효한 서로게이트 쌍을 허용합니다)와 UTF-16 인코더(비 BMP 문자를 인코딩하는 동안 생성합니다)는 예외입니다. (Contributed by Victor Stinner, Kang-Hao (Kenny) Lu and Serhiy Storchaka in bpo-12892.)새 독일어 EBCDIC 코덱
cp273. (Contributed by Michael Bierenfeld and Andrew Kuchling in bpo-1097797.)새 우크라이나어 코덱
cp1125. (Contributed by Serhiy Storchaka in bpo-19668.)bytes.join()과bytearray.join()은 이제 임의의 버퍼 객체를 인자로 받아들입니다. (Contributed by Antoine Pitrou in bpo-15958.)int생성자는 이제__index__메서드가 있는 모든 객체를 base 인자로 받아들입니다. (Contributed by Mark Dickinson in bpo-16772.)이제 프레임 객체에는 프레임에서 지역 변수에 대한 모든 참조를 지우는
clear()메서드가 있습니다. (Contributed by Antoine Pitrou in bpo-17934.)memoryview는 이제Sequence로 등록되었으며,reversed()내장을 지원합니다. (Contributed by Nick Coghlan and Claudiu Popa in bpo-18690 and bpo-19078.)인자 클리닉의 도입과
inspect와pydoc모듈에 대한 다른 변경의 결과로help()가 보고하는 서명이 여러 경우에서 수정되고 개선되었습니다.__length_hint__()는 이제 공식 언어 명세의 일부입니다 (PEP 424를 참조하십시오). (Contributed by Armin Ronacher in bpo-16148.)
새로운 모듈¶
asyncio¶
새로운 (PEP 3156에 정의된) asyncio 모듈은 파이썬 용 표준 플러그 형 이벤트 루프 모델을 제공하여, 표준 라이브러리에서 견고한 비동기 IO 지원을 제공하고, 다른 이벤트 루프 구현이 표준 라이브러리와 서로 상호 운용 할 수 있도록합니다.
파이썬 3.4의 경우, 이 모듈은 잠정 API로 간주됩니다.
더 보기
- PEP 3156 -- 비동기 IO 지원 재부팅: "asyncio" 모듈
Guido van Rossum이 주도하여 작성하고 구현한 PEP.
ensurepip¶
새로운 ensurepip 모듈은 PEP 453 구현을위한 기본 인프라입니다. 정상적인 이벤트 과정에서 최종 사용자는 이 모듈과 상호 작용할 필요가 없지만, 설치나 가상 환경으로의 자동화된 부트스트래핑이 거부 된 경우 pip를 수동으로 부트스트랩하는 데 사용할 수 있습니다.
ensurepip에는 함께 제공되는 CPython 릴리스의 첫 번째 릴리스 후보를 기준으로 최신 pip 번들 사본이 포함되어 있습니다 (유지 보수 릴리스와 기능 릴리스 모두에 적용됩니다). ensurepip는 인터넷에 접속하지 않습니다. 설치시 인터넷 액세스가 가능하면 ensurepip가 실행된 후 번들로 제공되는 pip를 사용하여 pip를 번들된 버전보다 최신 릴리스로 업그레이드 할 수 있습니다. (이러한 업그레이드 된 pip 버전은 별도로 설치된 패키지로 간주되며 파이썬을 제거해도 제거되지 않음에 유의하십시오.)
pip가 이미 설치되어있을 때 호출하면 아무 작업도 수행하지 않기 때문에 모듈 이름은 ensurepip입니다. 또한 기존에 설치된 pip 버전이 번들 사본보다 오래된 경우 pip 번들 사본을 설치하도록하는 --upgrade 옵션도 있습니다.
enum¶
새로운 (PEP 435에 정의된) enum 모듈은 열거 형의 표준 구현을 제공하여, 다른 모듈(가령 socket)이 더 많은 정보를 제공하는 에러 메시지를 제공하고 불투명 한 정수 상수를 이전 버전과 호환되는 열거 값으로 대체함으로써 더 나은 디버깅 지원을 제공하도록 합니다.
더 보기
- PEP 435 -- 파이썬 표준 라이브러리에 Enum 형 추가
Barry Warsaw, Eli Bendersky 및 Ethan Furman이 작성하고, Ethan Furman이 구현한 PEP.
pathlib¶
새로운 pathlib 모듈은 다양한 운영 체제에 적합한 의미 체계를 가진 파일 시스템 경로를 나타내는 클래스를 제공합니다. 경로 클래스는 I/O 없이 순전히 계산 연산을 제공하는 순수 경로와 순수 경로에서 상속되지만 I/O 연산도 제공하는 구상 경로로 나뉩니다.
파이썬 3.4의 경우, 이 모듈은 잠정 API로 간주됩니다.
더 보기
- PEP 428 -- pathlib 모듈 -- 객체 지향 파일 시스템 경로
Antoine Pitrou가 작성하고 구현한 PEP.
selectors¶
새로운 (PEP 3156 구현의 일부로 만들어진) selectors 모듈은 select 모듈 프리미티브를 기반으로하는, 고수준의 효율적인 I/O 멀티플렉싱을 허용합니다.
statistics¶
새로운 (PEP 450에서 정의된) statistics 모듈은 표준 라이브러리에서 직접 몇 가지 핵심 통계 기능을 제공합니다. 이 모듈은 데이터 계열의 평균, 중앙값, 모드, 분산 및 표준 편차 계산을 지원합니다.
더 보기
- PEP 450 -- 표준 라이브러리에 통계 모듈 추가
Steven D'Aprano가 작성하고 구현한 PEP
tracemalloc¶
새로운 (PEP 454에 정의된) tracemalloc 모듈은 파이썬에서 할당한 메모리 블록을 추적하는 디버그 도구입니다. 다음 정보를 제공합니다:
객체가 할당된 위치 추적
파일명과 줄 번호 당 할당 된 메모리 블록에 대한 통계: 할당 된 메모리 블록의 총 크기, 수 및 평균 크기
두 스냅 샷 간의 차이를 계산하여 메모리 누수 감지
더 보기
- PEP 454 -- 파이썬 메모리 할당을 추적하기 위해 새 tracemalloc 모듈 추가
Victor Stinner가 작성하고 구현한 PEP
개선된 모듈¶
abc¶
새 함수 abc.get_cache_token()을 사용하여 객체 그래프의 변경에 영향을 받는 캐시를 무효화 할 시기를 알 수 있습니다. (Contributed by Łukasz Langa in bpo-16832.)
새 클래스 ABC는 메타 클래스로 ABCMeta를 갖습니다. ABC를 베이스 클래스로 사용하는 것은 본질적으로 metaclass=abc.ABCMeta를 지정하는 것과 같은 효과가 있지만, 입력하기가 더 간단하고 읽기 쉽습니다. (Contributed by Bruno Dupuis in bpo-16049.)
aifc¶
getparams() 메서드는 이제 일반 튜플이 아닌 네임드 튜플을 반환합니다. (Contributed by Claudiu Popa in bpo-17818.)
aifc.open()은 이제 컨텍스트 관리 프로토콜을 지원합니다: with 블록에서 사용하면, 반환된 객체의 close() 메서드가 블록 끝에서 자동으로 호출됩니다. (Contributed by Serhiy Storchacha in bpo-16486.)
writeframesraw()와 writeframes() 메서드는 이제 모든 바이트열류 객체를 받아들입니다. (Contributed by Serhiy Storchaka in bpo-8311.)
argparse¶
FileType 클래스는 이제 open()으로 전달되는 encoding과 errors 인자를 받아들입니다. (Contributed by Lucas Maystre in bpo-11175.)
audioop¶
audioop는 이제 24-비트 샘플을 지원합니다. (Contributed by Serhiy Storchaka in bpo-12866.)
새로운 byteswap() 함수는 빅 엔디안 샘플을 리틀 엔디안으로 또는 그 반대로 변환합니다. (Contributed by Serhiy Storchaka in bpo-19641.)
모든 audioop 함수는 이제 모든 바이트열류 객체를 받아들입니다. 문자열은 허용되지 않습니다: 이전에는 작동하지 않았으나, 이제 즉시 에러가 발생합니다. (Contributed by Serhiy Storchaka in bpo-16685.)
base64¶
base64의 인코딩과 디코딩 함수는 이전에 bytes나 bytearray 인스턴스를 요구했던 곳에서 모든 바이트열류 객체를 받아들입니다. (Contributed by Nick Coghlan in bpo-17839.)
새로운 함수 a85encode(), a85decode(), b85encode() 및 b85decode()는 각각 Ascii85와 git/mercurial Base85 형식으로 바이너리 데이터를 인코딩하고 디코딩하는 기능을 제공합니다. a85 함수에는 Adobe 변형을 포함하여 Ascii85 인코딩 변형과 호환되도록 하는 데 사용할 수 있는 옵션이 있습니다. (Contributed by Martin Morrison, the Mercurial project, Serhiy Storchaka, and Antoine Pitrou in bpo-17618.)
collections¶
ChainMap.new_child() 메서드는 이제 체인에 추가할 자식 맵을 지정하는 m 인자를 받아들입니다. 이를 통해 기존 매핑 및/또는 사용자 정의 매핑형을 자식으로 사용할 수 있습니다. (Contributed by Vinay Sajip in bpo-16613.)
colorsys¶
RGB --- YIQ 변환에 대한 계수의 자릿수가 FCC NTSC 버전과 일치하도록 확장되었습니다. 결과의 변화는 1% 미만이어야 하며 다른 곳에서 찾은 결과와 더 잘 일치 할 수 있습니다. (Contributed by Brian Landers and Serhiy Storchaka in bpo-14323.)
contextlib¶
새로운 contextlib.suppress 컨텍스트 관리자는 단일 문에서 예외를 의도적으로 억제하는 코드의 의도를 명확히 하는 데 도움이 됩니다. (Contributed by Raymond Hettinger in bpo-15806 and Zero Piraeus in bpo-19266.)
새로운 contextlib.redirect_stdout() 컨텍스트 관리자를 사용하면 유틸리티 스크립트가 sys.stdout에 출력을 기록하고 리디렉션 옵션을 제공하지 않는 유연하지 않은 API를 더 쉽게 처리 할 수 있습니다. 컨텍스트 관리자를 사용하여, sys.stdout 출력을 다른 스트림으로 리디렉션 하거나, io.StringIO와 함께 사용하여 문자열로 리디렉션 할 수 있습니다. 후자는, 예를 들어, 명령 줄 인터페이스를 구현하기 위해 작성된 함수에서 출력을 캡처하는 데 특히 유용할 수 있습니다. sys.stdout의 전역 상태에 영향을 미치기 때문에 유틸리티 스크립트에만 권장됩니다. (Contributed by Raymond Hettinger in bpo-15805.)
contextlib 설명서는 또한 일회용, 재사용 가능 및 재진입 가능 컨텍스트 관리자 간의 차이점에 대한 논의를 포함하도록 갱신되었습니다.
dbm¶
dbm.open() 객체는 이제 컨텍스트 관리 프로토콜을 지원합니다. with 문에서 사용하면, 데이터베이스 객체의 close 메서드가 블록 끝에서 자동으로 호출됩니다. (Contributed by Claudiu Popa and Nick Coghlan in bpo-19282.)
dis¶
show_code(), dis(), distb() 및 disassemble() 함수는 이제 출력을 기록할 곳을 제어하는 키워드 전용 file 인자를 받아들입니다.
The dis module is now built around an Instruction class
that provides object oriented access to the details of each individual bytecode
operation.
A new method, get_instructions(), 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 dis 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 dis module have been rewritten to use
these new components.
In addition, a new application-friendly class Bytecode provides
an object-oriented API for inspecting bytecode in both in human-readable form
and for iterating over instructions. The Bytecode constructor
takes the same arguments that get_instruction() does (plus an
optional current_offset), and the resulting object can be iterated to produce
Instruction objects. But it also has a dis
method, equivalent to calling dis 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']
Bytecode also has a class method,
from_traceback(), 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 and Claudiu Popa in bpo-17916.)
New function stack_effect() 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.)
doctest¶
A new option flag, FAIL_FAST, halts
test running as soon as the first failure is detected. (Contributed by R.
David Murray and Daniel Urban in bpo-16522.)
The doctest command line interface now uses argparse, and has two
new options, -o and -f. -o allows doctest options to be specified on the command line, and -f is a
shorthand for -o FAIL_FAST (to parallel the similar option supported by the
unittest CLI). (Contributed by R. David Murray in bpo-11390.)
doctest will now find doctests in extension module __doc__ strings.
(Contributed by Zachary Ware in bpo-3158.)
email¶
as_string() 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 generator in
order to pass formatting parameters to its flatten method. (Contributed by
R. David Murray in bpo-18600.)
New method as_bytes() 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
Message __bytes__() 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.)
The Message.set_param() 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.)
A pair of new subclasses of Message have been added
(EmailMessage and MIMEPart), along with a new sub-module,
contentmanager and a new policy attribute
content_manager. All documentation is
currently in the new module, which is being added as part of email's new
provisional API. 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 contentmanager documentation and
the email: 예제. 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.)
filecmp¶
A new clear_cache() function provides the ability to clear the
filecmp comparison cache, which uses os.stat() 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.)
New module attribute DEFAULT_IGNORES provides the list of
directories that are used as the default value for the ignore parameter of
the dircmp() function. (Contributed by Eli Bendersky in
bpo-15442.)
functools¶
The new partialmethod() descriptor brings partial argument
application to descriptors, just as partial() provides
for normal callables. The new descriptor also makes it easier to get
arbitrary callables (including partial() instances)
to behave like normal instance methods when included in a class definition.
(Contributed by Alon Horev and Nick Coghlan in bpo-4331.)
The new singledispatch() 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.
더 보기
- PEP 443 -- Single-dispatch generic functions
Łukasz Langa가 작성하고 구현한 PEP.
total_ordering() now supports a return value of
NotImplemented from the underlying comparison function. (Contributed
by Katie Miller in bpo-10042.)
A pure-python version of the partial() 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.)
gc¶
New function get_stats() returns a list of three per-generation
dictionaries containing the collections statistics since interpreter startup.
(Contributed by Antoine Pitrou in bpo-16351.)
glob¶
A new function escape() 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.)
hashlib¶
A new hashlib.pbkdf2_hmac() function provides
the PKCS#5 password-based key derivation function 2. (Contributed by Christian
Heimes in bpo-18582.)
The name attribute of hashlib hash objects is now
a formally supported interface. It has always existed in CPython's
hashlib (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.)
hmac¶
hmac now accepts bytearray as well as bytes for the key
argument to the new() function, and the msg parameter to both the
new() function and the update() method now
accepts any type supported by the hashlib module. (Contributed
by Jonas Borgström in bpo-18240.)
The digestmod argument to the hmac.new() function may now be any hash
digest name recognized by hashlib. 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.)
With the addition of block_size and name
attributes (and the formal documentation of the digest_size
attribute), the hmac module now conforms fully to the PEP 247 API.
(Contributed by Christian Heimes in bpo-18775.)
html¶
New function unescape() function converts HTML5 character references to
the corresponding Unicode characters. (Contributed by Ezio Melotti in
bpo-2927.)
HTMLParser 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.)
The strict argument of HTMLParser is now deprecated.
(Contributed by Ezio Melotti in bpo-15114.)
http¶
send_error() 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.)
The http.server command line interface now has
a -b/--bind option that causes the server to listen on a specific address.
(Contributed by Malte Swart in bpo-17764.)
idlelib와 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 dialog.
importlib¶
The InspectLoader ABC defines a new method,
source_to_code() 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.)
InspectLoader also now has a default implementation
for the get_code() method. However,
it will normally be desirable to override the default implementation
for performance reasons. (Contributed by Brett Cannon in bpo-18072.)
The reload() function has been moved from imp to
importlib as part of the imp module deprecation. (Contributed by
Berker Peksag in bpo-18193.)
importlib.util now has a MAGIC_NUMBER attribute
providing access to the bytecode version number. This replaces the
get_magic() function in the deprecated imp module.
(Contributed by Brett Cannon in bpo-18192.)
New importlib.util functions cache_from_source()
and source_from_cache() replace the same-named functions
in the deprecated imp module. (Contributed by Brett Cannon in
bpo-18194.)
The importlib bootstrap NamespaceLoader now conforms to
the InspectLoader ABC, which means that runpy and
python -m can now be used with namespace packages. (Contributed
by Brett Cannon in bpo-18058.)
importlib.util has a new function decode_source()
that decodes source from bytes using universal newline processing. This is
useful for implementing InspectLoader.get_source() methods.
importlib.machinery.ExtensionFileLoader now has a
get_filename() method. This was
inadvertently omitted in the original implementation. (Contributed by Eric
Snow in bpo-19152.)
inspect¶
The inspect module now offers a basic command line interface to quickly display source code and other
information for modules, classes and functions. (Contributed by Claudiu Popa
and Nick Coghlan in bpo-18626.)
unwrap() makes it easy to unravel wrapper function chains
created by functools.wraps() (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.)
As part of the implementation of the new enum module, the
inspect module now has substantially better support for custom
__dir__ methods and dynamic class attributes provided through
metaclasses. (Contributed by Ethan Furman in bpo-18929 and
bpo-19030.)
getfullargspec() and getargspec()
now use the signature() API. This allows them to
support a much broader range of callables, including those with
__signature__ attributes, those with metadata provided by argument
clinic, functools.partial() objects and more. Note that, unlike
signature(), 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
signature() directly if those features are desired.
(Contributed by Yury Selivanov in bpo-17481.)
signature() 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.)
ipaddress¶
ipaddress was added to the standard library in Python 3.3 as a
provisional API. With the release of Python 3.4, this qualification
has been removed: ipaddress is now considered a stable API, covered
by the normal standard library requirements to maintain backwards
compatibility.
A new is_global property is True if
an address is globally routeable. (Contributed by Peter Moody in
bpo-17400.)
logging¶
The TimedRotatingFileHandler 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.)
SocketHandler and
DatagramHandler now support Unix domain sockets (by
setting port to None). (Contributed by Vinay Sajip in commit
ce46195b56a9.)
fileConfig() now accepts a
configparser.RawConfigParser 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
fileConfig(). (Contributed by Vinay Sajip in
bpo-16110.)
Logging configuration data received from a socket via the
logging.config.listen() 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.)
marshal¶
The default marshal 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,
with additional speedups by Antoine Pitrou in bpo-19219.)
multiprocessing¶
On Unix two new start methods,
spawn and forkserver, have been added for starting processes using
multiprocessing. 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
get_all_start_methods() reports all start methods
available on the platform, get_start_method() reports
the current start method, and set_start_method() sets
the start method. (Contributed by Richard Oudkerk in bpo-8713.)
multiprocessing also now has the concept of a context, which
determines how child processes are created. New function
get_context() returns a context that uses a specified
start method. It has the same API as the multiprocessing module itself,
so you can use it to create Pools 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.)
Except when using the old fork start method, child processes no longer inherit unneeded handles/file descriptors from their parents (part of bpo-8713).
multiprocessing now relies on runpy (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.)
operator¶
New function length_hint() provides an implementation of the
specification for how the __length_hint__() special method should
be used, as part of the PEP 424 formal specification of this language
feature. (Contributed by Armin Ronacher in bpo-16148.)
There is now a pure-python version of the operator module available for
reference and for use by alternate implementations of Python. (Contributed by
Zachary Ware in bpo-16694.)
os¶
There are new functions to get and set the inheritable flag of a file descriptor (os.get_inheritable(),
os.set_inheritable()) or a Windows handle
(os.get_handle_inheritable(), os.set_handle_inheritable()).
New function cpu_count() reports the number of CPUs available on the
platform on which Python is running (or None if the count can't be
determined). The multiprocessing.cpu_count() 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.)
os.path.samestat() is now available on the Windows platform (and the
os.path.samefile() implementation is now shared between Unix and
Windows). (Contributed by Brian Curtin in bpo-11939.)
os.path.ismount() now recognizes volumes mounted below a drive
root on Windows. (Contributed by Tim Golden in bpo-9035.)
os.open() supports two new flags on platforms that provide them,
O_PATH (un-opened file descriptor), and O_TMPFILE
(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 and Benjamin Peterson, respectively.)
pdb¶
pdb has been enhanced to handle generators, yield, and
yield from in a more useful fashion. This is especially helpful when
debugging asyncio based programs. (Contributed by Andrew Svetlov and
Xavier de Gaye in bpo-16596.)
The print command has been removed from pdb, restoring access to the
Python print() 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 p 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.)
pickle¶
pickle 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 __new__() method takes
keyword-only arguments. It also provides some efficiency improvements.
더 보기
- PEP 3154 -- Pickle protocol 4
Antoine Pitrou가 작성하고 Alexandre Vassalotti가 구현한 PEP.
plistlib¶
plistlib now has an API that is similar to the standard pattern for
stdlib serialization protocols, with new load(),
dump(), loads(), and dumps()
functions. (The older API is now deprecated.) In addition to the already
supported XML plist format (FMT_XML), it also now supports
the binary plist format (FMT_BINARY). (Contributed by Ronald
Oussoren and others in bpo-14455.)
poplib¶
Two new methods have been added to poplib: capa(),
which returns the list of capabilities advertised by the POP server, and
stls(), 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.)
pprint¶
The pprint module's PrettyPrinter class and its
pformat(), and pprint() 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.)
Long strings are now wrapped using Python's normal line continuation syntax. (Contributed by Antoine Pitrou in bpo-17150.)
pty¶
pty.spawn() now returns the status value from os.waitpid() on
the child process, instead of None. (Contributed by Gregory P. Smith.)
pydoc¶
The pydoc module is now based directly on the inspect.signature()
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.)
The pydoc 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.)
In addition to the changes that have been made to pydoc directly,
its handling of custom __dir__ methods and various descriptor
behaviours has also been improved substantially by the underlying changes in
the inspect module.
As the help() builtin is based on pydoc, the above changes also
affect the behaviour of help().
re¶
New fullmatch() 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.)
The repr of regex objects now includes the pattern and the flags; the repr of match objects now includes the start, end, and the part of the string that matched. (Contributed by Hugo Lopes Tavares and Serhiy Storchaka in bpo-13592 and bpo-17087.)
resource¶
New prlimit() 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.)
On Linux kernel version 2.6.36 or later, there are also some new
Linux specific constants: RLIMIT_MSGQUEUE,
RLIMIT_NICE, RLIMIT_RTPRIO,
RLIMIT_RTTIME, and RLIMIT_SIGPENDING.
(Contributed by Christian Heimes in bpo-19324.)
On FreeBSD version 9 and later, there some new FreeBSD specific constants:
RLIMIT_SBSIZE, RLIMIT_SWAP, and
RLIMIT_NPTS. (Contributed by Claudiu Popa in
bpo-19343.)
select¶
epoll objects now support the context management protocol.
When used in a with statement, the close()
method will be called automatically at the end of the block. (Contributed
by Serhiy Storchaka in bpo-16488.)
devpoll objects now have fileno() and
close() methods, as well as a new attribute
closed. (Contributed by Victor Stinner in
bpo-18794.)
shelve¶
Shelf instances may now be used in with statements,
and will be automatically closed at the end of the with block.
(Contributed by Filip Gruszczyński in bpo-13896.)
shutil¶
copyfile() now raises a specific Error subclass,
SameFileError, 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.)
smtpd¶
The SMTPServer and SMTPChannel classes now
accept a map keyword argument which, if specified, is passed in to
asynchat.async_chat as its map argument. This allows an application
to avoid affecting the global socket map. (Contributed by Vinay Sajip in
bpo-11959.)
smtplib¶
SMTPException is now a subclass of OSError, 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.)
socket¶
The socket module now supports the CAN_BCM protocol on
platforms that support it. (Contributed by Brian Thorne in bpo-15359.)
Socket objects have new methods to get or set their inheritable flag, get_inheritable() and
set_inheritable().
The socket.AF_* and socket.SOCK_* constants are now enumeration values
using the new enum module. This allows meaningful names to be printed
during debugging, instead of integer "magic numbers".
The AF_LINK constant is now available on BSD and OSX.
inet_pton() and inet_ntop() are now supported
on Windows. (Contributed by Atsuo Ishimoto in bpo-7171.)
sqlite3¶
A new boolean parameter to the connect() function, uri, can be
used to indicate that the database parameter is a uri (see the SQLite
URI documentation). (Contributed by poq in
bpo-13773.)
ssl¶
PROTOCOL_TLSv1_1 and PROTOCOL_TLSv1_2 (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.)
New function create_default_context() provides a standard way to
obtain an SSLContext whose settings are intended to be a
reasonable balance between compatibility and security. These settings are
more stringent than the defaults provided by the SSLContext
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
create_default_context() to obtain an SSLContext
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.)
SSLContext method load_verify_locations()
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.)
New function get_default_verify_paths() returns
a named tuple of the paths and environment variables that the
set_default_verify_paths() 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.)
SSLContext has a new method,
cert_store_stats(), that reports the number of loaded
X.509 certs, X.509 CA certs, and certificate revocation lists
(crls), as well as a get_ca_certs() method that
returns a list of the loaded CA certificates. (Contributed by Christian
Heimes in bpo-18147.)
If OpenSSL 0.9.8 or later is available, SSLContext has a new
attribute verify_flags that can be used to control the
certificate verification process by setting it to some combination of the new
constants VERIFY_DEFAULT, VERIFY_CRL_CHECK_LEAF,
VERIFY_CRL_CHECK_CHAIN, or VERIFY_X509_STRICT.
OpenSSL does not do any CRL verification by default. (Contributed by
Christien Heimes in bpo-8813.)
New SSLContext method load_default_certs()
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=SERVER_AUTH) for a client to use to verify a
server, and certificates for a server to use in verifying client certificates
(purpose=CLIENT_AUTH). (Contributed by Christian
Heimes in bpo-19292.)
Two new windows-only functions, enum_certificates() and
enum_crls() provide the ability to retrieve certificates,
certificate information, and CRLs from the Windows cert store. (Contributed
by Christian Heimes in bpo-17134.)
Support for server-side SNI (Server Name Indication) using the new
ssl.SSLContext.set_servername_callback() method.
(Contributed by Daniel Black in bpo-8109.)
The dictionary returned by SSLSocket.getpeercert() contains additional
X509v3 extension items: crlDistributionPoints, calIssuers, and
OCSP URIs. (Contributed by Christian Heimes in bpo-18379.)
stat¶
The stat 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.)
The module supports new ST_MODE flags, S_IFDOOR,
S_IFPORT, and S_IFWHT. (Contributed by
Christian Hiemes in bpo-11016.)
struct¶
New function iter_unpack and a new
struct.Struct.iter_unpack() 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.)
subprocess¶
check_output() 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.)
getstatus() and getstatusoutput() now
work on Windows. This change was actually inadvertently made in 3.3.4.
(Contributed by Tim Golden in bpo-10197.)
sunau¶
The getparams() method now returns a namedtuple rather than a
plain tuple. (Contributed by Claudiu Popa in bpo-18901.)
sunau.open() now supports the context management protocol: when used in a
with 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.)
AU_write.setsampwidth() now supports 24 bit samples, thus adding
support for writing 24 sample using the module. (Contributed by
Serhiy Storchaka in bpo-19261.)
The writeframesraw() and
writeframes() methods now accept any bytes-like
object. (Contributed by Serhiy Storchaka in bpo-8311.)
sys¶
New function sys.getallocatedblocks() returns the current number of
blocks allocated by the interpreter. (In CPython with the default
--with-pymalloc setting, this is allocations made through the
PyObject_Malloc() API.) This can be useful for tracking memory leaks,
especially if automated via a test suite. (Contributed by Antoine Pitrou
in bpo-13390.)
When the Python interpreter starts in interactive mode, it checks for an __interactivehook__ attribute
on the sys module. If the attribute exists, its value is called with no
arguments just before interactive mode is started. The check is made after the
PYTHONSTARTUP file is read, so it can be set there. The site
module sets it to a function that enables tab
completion and history saving (in ~/.python-history) if the platform
supports readline. If you do not want this (new) behavior, you can
override it in PYTHONSTARTUP, sitecustomize, or
usercustomize by deleting this attribute from sys (or setting it
to some other callable). (Contributed by Éric Araujo and Antoine Pitrou in
bpo-5845.)
tarfile¶
The tarfile module now supports a simple 명령 줄 인터페이스 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.)
textwrap¶
The TextWrapper class has two new attributes/constructor
arguments: max_lines, which limits the number of
lines in the output, and placeholder, 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 shorten() 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 and bpo-18725.)
threading¶
The Thread object representing the main thread can be
obtained from the new main_thread() function. In normal
conditions this will be the thread from which the Python interpreter was
started. (Contributed by Andrew Svetlov in bpo-18882.)
traceback¶
A new traceback.clear_frames() 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.)
types¶
A new DynamicClassAttribute() 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.)
urllib¶
urllib.request now supports data: URLs via the
DataHandler class. (Contributed by Mathias Panzenböck
in bpo-16423.)
The http method that will be used by a Request class
can now be specified by setting a method
class attribute on the subclass. (Contributed by Jason R Coombs in
bpo-18978.)
Request objects are now reusable: if the
full_url or data
attributes are modified, all relevant internal properties are updated. This
means, for example, that it is now possible to use the same
Request object in more than one
OpenerDirector.open() call with different data arguments, or to
modify a Request's url rather than recomputing it
from scratch. There is also a new
remove_header() method that can be used to remove
headers from a Request. (Contributed by Alexey
Kachayev in bpo-16464, Daniel Wozniak in bpo-17485, and Damien Brecht
and Senthil Kumaran in bpo-17272.)
HTTPError objects now have a
headers attribute that provides access to the
HTTP response headers associated with the error. (Contributed by
Berker Peksag in bpo-15701.)
unittest¶
The TestCase class has a new method,
subTest(), that produces a context manager whose
with 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 부분 테스트(subtest)를 사용하여 테스트 반복 구별 짓기 for the full
version of this example. (Contributed by Antoine Pitrou in bpo-16997.)
unittest.main() 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.)
If SkipTest 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.)
discover() now sorts the discovered files to provide
consistent test ordering. (Contributed by Martin Melin and Jeff Ramnani in
bpo-16709.)
TestSuite 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 TestSuite subclass that defines a
custom _removeTestAtIndex method. (Contributed by Tom Wardill, Matt
McClure, and Andrew Svetlov in bpo-11798.)
A new test assertion context-manager, assertLogs(),
will ensure that a given block of code emits a log message using the
logging 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 LogRecords and/or
formatted messages that were logged. (Contributed by Antoine Pitrou in
bpo-18937.)
Test discovery now works with namespace packages (Contributed by Claudiu Popa in bpo-17457.)
unittest.mock 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.)
mock_open() objects now have readline and readlines
methods. (Contributed by Toshio Kuratomi in bpo-17467.)
venv¶
venv now includes activation scripts for the csh and fish
shells. (Contributed by Andrew Svetlov in bpo-15417.)
EnvBuilder and the create() convenience function
take a new keyword argument with_pip, which defaults to False, that
controls whether or not EnvBuilder ensures that pip is
installed in the virtual environment. (Contributed by Nick Coghlan in
bpo-19552 as part of the PEP 453 implementation.)
wave¶
The getparams() method now returns a namedtuple rather than a
plain tuple. (Contributed by Claudiu Popa in bpo-17487.)
wave.open() now supports the context management protocol. (Contributed
by Claudiu Popa in bpo-17616.)
wave can now write output to unseekable files. (Contributed by David Jones, Guilherme Polo, and Serhiy
Storchaka in bpo-5202.)
The writeframesraw() and
writeframes() methods now accept any bytes-like
object. (Contributed by Serhiy Storchaka in bpo-8311.)
weakref¶
New WeakMethod class simulates weak references to bound
methods. (Contributed by Antoine Pitrou in bpo-14631.)
New finalize 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.)
The callback, if any, associated with a ref is now
exposed via the __callback__ attribute. (Contributed
by Mark Dickinson in bpo-17643.)
xml.etree¶
A new parser, XMLPullParser, allows a
non-blocking applications to parse XML documents. An example can be
seen at 비 블로킹 구문 분석을 위한 풀(pull) API. (Contributed by Antoine
Pitrou in bpo-17741.)
The xml.etree.ElementTree tostring() and
tostringlist() functions, and the
ElementTree
write() method, now have a
short_empty_elements keyword-only parameter
providing control over whether elements with no content are written in
abbreviated (<tag />) or expanded (<tag></tag>) form. (Contributed by
Ariel Poliak and Serhiy Storchaka in bpo-14377.)
zipfile¶
The writepy() method of the
PyZipFile 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.)
The allowZip64 parameter to ZipFile and
PyZipfile is now True by default. (Contributed by
William Mallard in bpo-17201.)
CPython Implementation Changes¶
PEP 445: Customization of CPython Memory Allocators¶
PEP 445 adds new C level interfaces to customize memory allocation in the CPython interpreter.
더 보기
- PEP 445 -- Add new APIs to customize Python memory allocators
Victor Stinner가 작성하고 구현 한 PEP.
PEP 442: Safe Object Finalization¶
PEP 442 removes the current limitations and quirks of object finalization
in CPython. With it, objects with __del__() methods, as well as
generators with finally 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
None 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.
더 보기
- PEP 442 -- Safe object finalization
Antoine Pitrou가 작성하고 구현한 PEP.
PEP 456: Secure and Interchangeable Hash Algorithm¶
PEP 456 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 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 sys.hash_info named tuple to
describe the hash algorithm in use by the currently executing binary. Otherwise,
the PEP does not alter any existing CPython APIs.
PEP 436: Argument Clinic¶
"Argument Clinic" (PEP 436) 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 pydoc and inspect 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.
참고
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.
더 보기
- PEP 436 -- The Argument Clinic DSL
Larry Hastings가 작성하고 구현한 PEP.
Other Build and C API Changes¶
The new
PyType_GetSlot()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.)The new
Py_SetStandardStreamEncoding()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.)Most Python C APIs that don't mutate string arguments are now correctly marked as accepting
const char *rather thanchar *. (Contributed by Serhiy Storchaka in bpo-1772673.)A new shell version of
python-configcan be used even when a python interpreter is not available (for example, in cross compilation scenarios).PyUnicode_FromFormat()now supports width and precision specifications for%s,%A,%U,%V,%S, and%R. (Contributed by Ysj Ray and Victor Stinner in bpo-7330.)New function
PyStructSequence_InitType2()supplements the existingPyStructSequence_InitType()function. The difference is that it returns0on success and-1on 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.)
The Windows build now uses Address Space Layout Randomization and Data Execution Prevention. (Contributed by Christian Heimes in bpo-16632.)
New function
PyObject_LengthHint()is the C API equivalent ofoperator.length_hint(). (Contributed by Armin Ronacher in bpo-16148.)
Other Improvements¶
The python command has a new option,
-I, which causes it to run in "isolated mode", which means thatsys.pathcontains neither the script's directory nor the user'ssite-packagesdirectory, and allPYTHON*environment variables are ignored (it implies both-sand-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.)Tab-completion is now enabled by default in the interactive interpreter on systems that support
readline. 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.)Invoking the Python interpreter with
--versionnow outputs the version to standard output instead of standard error (bpo-18338). Similar changes were made toargparse(bpo-18920) and other modules that have script-like invocation capabilities (bpo-18922).The CPython Windows installer now adds
.pyto thePATHEXTvariable when extensions are registered, allowing users to run a python script at the windows command prompt by just typing its name without the.pyextension. (Contributed by Paul Moore in bpo-18569.)A new
maketarget coverage-report will build python, run the test suite, and generate an HTML coverage report for the C codebase usinggcovand lcov.The
-Roption to the python regression test suite now also checks for memory allocation leaks, usingsys.getallocatedblocks(). (Contributed by Antoine Pitrou in bpo-13390.)python -mnow works with namespace packages.The
statmodule 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.)A new opcode,
LOAD_CLASSDEREF, has been added to fix a bug in the loading of free variables in class bodies that could be triggered by certain uses of __prepare__. (Contributed by Benjamin Peterson in bpo-17853.)A number of MemoryError-related crashes were identified and fixed by Victor Stinner using his PEP 445-based
pyfailmalloctool (bpo-18408, bpo-18520).The
pyvenvcommand now accepts a--copiesoption to use copies rather than symlinks even on systems where symlinks are the default. (Contributed by Vinay Sajip in bpo-18807.)The
pyvenvcommand also accepts a--without-pipoption to suppress the otherwise-automatic bootstrapping of pip into the virtual environment. (Contributed by Nick Coghlan in bpo-19552 as part of the PEP 453 implementation.)The encoding name is now optional in the value set for the
PYTHONIOENCODINGenvironment variable. This makes it possible to set just the error handler, without changing the default encoding. (Contributed by Serhiy Storchaka in bpo-18818.)The
bz2,lzma, andgzipmoduleopenfunctions now supportx(exclusive creation) mode. (Contributed by Tim Heaney and Vajrasky Kok in bpo-19201, bpo-19222, and bpo-19223.)
Significant Optimizations¶
The UTF-32 decoder is now 3x to 4x faster. (Contributed by Serhiy Storchaka in bpo-14625.)
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.)
The interpreter starts about 30% faster. A couple of measures lead to the speedup. The interpreter loads fewer modules on startup, e.g. the
re,collectionsandlocalemodules 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, bpo-19218, bpo-19209, bpo-19205 and bpo-9548.)bz2.BZ2Fileis now as fast or faster than the Python2 version for most cases.lzma.LZMAFilehas also been optimized. (Contributed by Serhiy Storchaka and Nadeem Vawda in bpo-16034.)random.getrandbits()is 20%-40% faster for small integers (the most common use case). (Contributed by Serhiy Storchaka in bpo-16674.)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.)
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 throughsubprocess. (Contributed by Richard Oudkerk in bpo-15758.)html.escape()is now 10x faster. (Contributed by Matt Bryant in bpo-18020.)On Windows, the native
VirtualAllocis now used instead of the CRTmallocinobmalloc. Artificial benchmarks show about a 3% memory savings.os.urandom()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.)
폐지¶
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 DeprecationWarning
when the interpreter is run with deprecation warnings enabled (for example, by
using -Wd).
Deprecations in the Python API¶
As mentioned in PEP 451: 임포트 시스템을 위한 ModuleSpec 형, a number of
importlibmethods and functions are deprecated:importlib.find_loader()is replaced byimportlib.util.find_spec();importlib.machinery.PathFinder.find_module()is replaced byimportlib.machinery.PathFinder.find_spec();importlib.abc.MetaPathFinder.find_module()is replaced byimportlib.abc.MetaPathFinder.find_spec();importlib.abc.PathEntryFinder.find_loader()andfind_module()are replaced byimportlib.abc.PathEntryFinder.find_spec(); all of thexxxLoaderABCload_modulemethods (importlib.abc.Loader.load_module(),importlib.abc.InspectLoader.load_module(),importlib.abc.FileLoader.load_module(),importlib.abc.SourceLoader.load_module()) should no longer be implemented, instead loaders should implement anexec_modulemethod (importlib.abc.Loader.exec_module(),importlib.abc.InspectLoader.exec_module()importlib.abc.SourceLoader.exec_module()) and let the import system take care of the rest; andimportlib.abc.Loader.module_repr(),importlib.util.module_for_loader(),importlib.util.set_loader(), andimportlib.util.set_package()are no longer needed because their functions are now handled automatically by the import system.The
impmodule is pending deprecation. To keep compatibility with Python 2/3 code bases, the module's removal is currently not scheduled.The
formattermodule is pending deprecation and is slated for removal in Python 3.6.MD5as the default digestmod for thehmac.new()function is deprecated. Python 3.6 will require an explicit digest name or constructor as digestmod argument.The internal
Netrcclass in theftplibmodule has been documented as deprecated in its docstring for quite some time. It now emits aDeprecationWarningand will be removed completely in Python 3.5.The undocumented endtime argument to
subprocess.Popen.wait()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
HTMLParseris deprecated.The
plistlibreadPlist(),writePlist(),readPlistFromBytes(), andwritePlistToBytes()functions are deprecated in favor of the corresponding new functionsload(),dump(),loads(), anddumps().Data()is deprecated in favor of just using thebytesconstructor.The
sysconfigkeySOis deprecated, it has been replaced byEXT_SUFFIX.The
Umode accepted by variousopenfunctions is deprecated. In Python3 it does not do anything useful, and should be replaced by appropriate uses ofio.TextIOWrapper(if needed) and its newline argument.The parser argument of
xml.etree.ElementTree.iterparse()has been deprecated, as has the html argument ofXMLParser(). To prepare for the removal of the latter, all arguments toXMLParsershould be passed by keyword.
제거¶
Operating Systems No Longer Supported¶
Support for the following operating systems has been removed from the source and build tools:
API and Feature Removals¶
The following obsolete and previously deprecated APIs and features have been removed:
The unmaintained
Misc/TextMateandMisc/vimdirectories have been removed (see the devguide for suggestions on what to use instead).The
SOmakefile macro is removed (it was replaced by theSHLIB_SUFFIXandEXT_SUFFIXmacros) (bpo-16754).The
PyThreadState.tick_counterfield has been removed; its value has been meaningless since Python 3.2, when the "new GIL" was introduced (bpo-19199).PyLoaderandPyPycLoaderhave been removed fromimportlib. (Contributed by Taras Lyapun in bpo-15641.)The strict argument to
HTTPConnectionandHTTPSConnectionhas been removed. HTTP 0.9-style "Simple Responses" are no longer supported.The deprecated
urllib.request.Requestgetter and setter methodsadd_data,has_data,get_data,get_type,get_host,get_selector,set_proxy,get_origin_req_host, andis_unverifiablehave been removed (use direct attribute access instead).Support for loading the deprecated
TYPE_INT64has been removed frommarshal. (Contributed by Dan Riti in bpo-15480.)inspect.Signature: positional-only parameters are now required to have a valid name.object.__format__()no longer accepts non-empty format strings, it now raises aTypeErrorinstead. 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 aTypeErrorif you are using an's'format code with objects that do not have a __format__ method that handles it. See bpo-7994 for background.difflib.SequenceMatcher.isbjunk()anddifflib.SequenceMatcher.isbpopular()were deprecated in 3.2, and have now been removed: usex in sm.bjunkandx in sm.bpopular, where sm is aSequenceMatcherobject (bpo-13248).
Code Cleanups¶
The unused and undocumented internal
Scannerclass has been removed from thepydocmodule.The private and effectively unused
_gestaltmodule has been removed, along with the privateplatformfunctions_mac_ver_lookup,_mac_ver_gstalt, and_bcd2str, which would only have ever been called on badly broken OSX systems (see bpo-18393).The hardcoded copies of certain
statconstants that were included in thetarfilemodule namespace have been removed.
파이썬 3.4로 이식하기¶
This section lists previously described changes and other bugfixes that may require changes to your code.
Changes in 'python' Command Behavior¶
In a posix shell, setting the
PATHenvironment variable to an empty value is equivalent to not setting it at all. However, settingPYTHONPATHto an empty value was not equivalent to not setting it at all: settingPYTHONPATHto an empty value was equivalent to setting it to., which leads to confusion when reasoning by analogy to howPATHworks. The behavior now conforms to the posix convention forPATH.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 showrefcountoption. (Contributed by Ezio Melotti in bpo-17323.)The python command and most stdlib scripts (as well as
argparse) now output--versioninformation tostdoutinstead ofstderr(for issue list see Other Improvements above).
파이썬 API의 변경¶
The ABCs defined in
importlib.abcnow either raise the appropriate exception or return a default value instead of raisingNotImplementedErrorblindly. This will only affect code callingsuper()and falling through all the way to the ABCs. For compatibility, catch bothNotImplementedErroror the appropriate exception as needed.The module type now initializes the
__package__and__loader__attributes toNoneby 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.)importlib.util.module_for_loader()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 useimportlib.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).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 usehasattr(module, '__path__')(bpo-18065).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__.locationis set to'frozen', check if the loader is a subclass ofimportlib.machinery.FrozenImporter, or if Python 2 compatibility is necessary you can useimp.is_frozen().py_compile.compile()now raisesFileExistsErrorif 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.importlib.abc.SourceLoader.get_source()no longer raisesImportErrorwhen the source code being loaded triggers aSyntaxErrororUnicodeDecodeError. AsImportErroris 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.functools.update_wrapper()andfunctools.wraps()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 useinspect.unwrap()to access the first function in the chain that has no__wrapped__attribute.inspect.getfullargspec()has been reimplemented on top ofinspect.signature()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 thatinspect.getfullargspec()will fail on non-Python callables may need to be adjusted accordingly.importlib.machinery.PathFindernow passes on the current working directory to objects insys.path_hooksfor the empty string. This results insys.path_importer_cachenever containing'', thus iterating throughsys.path_importer_cachebased onsys.pathwill 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-mwith the interpreter (except for__main__.__file__when a script has been executed directly using a relative path) (Contributed by Brett Cannon in bpo-18416). is specified on the command-line) (bpo-18416).The removal of the strict argument to
HTTPConnectionandHTTPSConnectionchanges 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 aSyntaxError. 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).ssl.SSLSocket.getpeercert()andssl.SSLSocket.do_handshake()now raise anOSErrorwithENOTCONNwhen theSSLSocketis not connected, instead of the previous behavior of raising anAttributeError. In addition,getpeercert()will raise aValueErrorif the handshake has not yet been done.base64.b32decode()now raises abinascii.Errorwhen the input string contains non-b32-alphabet characters, instead of aTypeError. This particularTypeErrorwas missed when the otherTypeErrors were converted. (Contributed by Serhiy Storchaka in bpo-18011.) Note: this change was also inadvertently applied in Python 3.3.3.The
fileattribute is now automatically closed when the creatingcgi.FieldStorageinstance is garbage collected. If you were pulling the file object out separately from thecgi.FieldStorageinstance and not keeping the instance alive, then you should either store the entirecgi.FieldStorageinstance or read the contents of the file before thecgi.FieldStorageinstance is garbage collected.Calling
readorwriteon a closed SSL socket now raises an informativeValueErrorrather than the previous more mysteriousAttributeError(bpo-9177).slice.indices()no longer produces anOverflowErrorfor huge values. As a consequence of this fix,slice.indices()now raises aValueErrorif given a negative length; previously it returned nonsense values (bpo-14794).The
complexconstructor, unlike thecmathfunctions, was incorrectly acceptingfloatvalues if an object's__complex__special method returned one. This now raises aTypeError. (bpo-16290.)The
intconstructor in 3.2 and 3.3 erroneously acceptsfloatvalues for the base parameter. It is unlikely anyone was doing this, but if so, it will now raise aTypeError(bpo-16772).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).
Stale thread states are now cleared after
fork(). 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.)Parameter names in
__annotations__dicts are now mangled properly, similarly to__kwdefaults__. (Contributed by Yury Selivanov in bpo-20625.)hashlib.hash.namenow 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).Because
unittest.TestSuitenow drops references to tests after they are run, test harnesses that re-use aTestSuiteto 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 thatunittestis designed to provide. However, if the lack of isolation is considered acceptable, the old behavior can be restored by creating aTestSuitesubclass that defines a_removeTestAtIndexmethod that does nothing (seeTestSuite.__iter__()) (bpo-11798).unittestnow usesargparsefor 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
re.split(),re.findall(), andre.sub()functions, and thegroup()andgroups()methods ofmatchobjects now always return a bytes object when the string to be matched is a bytes-like object. Previously the return type matched the input type, so if your code was depending on the return value being, say, abytearray, you will need to change your code.audioopfunctions now raise an error immediately if passed string input, instead of failing randomly later on (bpo-16685).The new convert_charrefs argument to
HTMLParsercurrently defaults toFalsefor backward compatibility, but will eventually be changed to default toTrue. It is recommended that you add this keyword, with the appropriate value, to anyHTMLParsercalls in your code (bpo-13633).Since the digestmod argument to the
hmac.new()function will in the future have no default, all calls tohmac.new()should be changed to explicitly specify a digestmod (bpo-17276).Calling
sysconfig.get_config_var()with theSOkey, or lookingSOup in the results of a call tosysconfig.get_config_vars()is deprecated. This key should be replaced byEXT_SUFFIXorSHLIB_SUFFIX, depending on the context (bpo-19555).Any calls to
openfunctions that specifyUshould be modified.Uis 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 inTextIOWrapperto use its newline argument (bpo-15204).If you use
pyvenvin a script and desire that pip not be installed, you must add--without-pipto your command invocation.The default behavior of
json.dump()andjson.dumps()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).doctestnow 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).The
collections.abcmodule has been slightly refactored as part of the Python startup improvements. As a consequence of this, it is no longer the case that importingcollectionsautomatically importscollections.abc. If your program depended on the (undocumented) implicit import, you will need to add an explicitimport collections.abc(bpo-20784).
C API의 변경¶
PyEval_EvalFrameEx(),PyObject_Repr(), andPyObject_Str(), 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 withPyErr_Fetch()or is being deliberately replaced with a different exception), an explicitPyErr_Clear()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.PyErr_SetImportError()now setsTypeErrorwhen its msg argument is not set. Previously onlyNULLwas returned with no exception set.The result of the
PyOS_ReadlineFunctionPointercallback must now be a string allocated byPyMem_RawMalloc()orPyMem_RawRealloc(), orNULLif an error occurred, instead of a string allocated byPyMem_Malloc()orPyMem_Realloc()(bpo-16742)PyThread_set_key_value()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-NULLpointer).The
f_tstate(thread state) field of thePyFrameObjectstructure has been removed to fix a bug: see bpo-14432 for the rationale.
Changed in 3.4.3¶
PEP 476: Enabling certificate verification by default for stdlib http clients¶
http.client and modules which use it, such as urllib.request and
xmlrpc.client, 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)
