From d7eb7ce0ff497848b4c4750de03001ed6fbd31dc Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Wed, 8 Apr 2020 15:02:36 +0300 Subject: [PATCH] bpo-39481: Generic alias support to ctypes.Array, LibraryLoader... --- Lib/ctypes/__init__.py | 3 +++ Lib/test/test_genericalias.py | 6 +++++- Lib/urllib/parse.py | 3 +++ Modules/_ctypes/_ctypes.c | 8 +++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 8f0991147d72f2..4afa4ebd422493 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -1,6 +1,7 @@ """create and manipulate C data types in Python""" import os as _os, sys as _sys +import types as _types __version__ = "1.1.0" @@ -450,6 +451,8 @@ def __getitem__(self, name): def LoadLibrary(self, name): return self._dlltype(name) + __class_getitem__ = classmethod(_types.GenericAlias) + cdll = LibraryLoader(CDLL) pydll = LibraryLoader(PyDLL) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 9c5b23e5e5b0fc..5f28f08ea91f95 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -7,9 +7,11 @@ ) from collections.abc import * from contextlib import AbstractContextManager, AbstractAsyncContextManager +from ctypes import Array, LibraryLoader from os import DirEntry from re import Pattern, Match from types import GenericAlias, MappingProxyType +from urllib.parse import SplitResult, ParseResult import typing from typing import TypeVar @@ -35,7 +37,9 @@ def test_subscriptable(self): Mapping, MutableMapping, MappingView, KeysView, ItemsView, ValuesView, Sequence, MutableSequence, - MappingProxyType, DirEntry + MappingProxyType, DirEntry, + Array, LibraryLoader, + SplitResult, ParseResult ): tname = t.__name__ with self.subTest(f"Testing {tname}"): diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 779278bac598a1..ea897c3032257b 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -29,6 +29,7 @@ import re import sys +import types import collections import warnings @@ -176,6 +177,8 @@ def port(self): raise ValueError("Port out of range 0-65535") return port + __class_getitem__ = classmethod(types.GenericAlias) + class _NetlocResultMixinStr(_NetlocResultMixinBase, _ResultMixinStr): __slots__ = () diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index dab39396476ba4..ba5ef397cf05bf 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4798,6 +4798,12 @@ Array_length(PyObject *myself) return self->b_length; } +static PyMethodDef Array_methods[] = { + {"__class_getitem__", (PyCFunction)Py_GenericAlias, + METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, + { NULL, NULL } +}; + static PySequenceMethods Array_as_sequence = { Array_length, /* sq_length; */ 0, /* sq_concat; */ @@ -4846,7 +4852,7 @@ PyTypeObject PyCArray_Type = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + Array_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */