From 4ddd2b763bfee809310e8ddca3279321065527d5 Mon Sep 17 00:00:00 2001 From: Harikrishna Srinivasan <176735075+Harikrishna-Srinivasan-1@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:11:50 +0530 Subject: [PATCH 1/8] Update _endian.py --- Lib/ctypes/_endian.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/Lib/ctypes/_endian.py b/Lib/ctypes/_endian.py index 6382dd22b8acc8..0b71caef421482 100644 --- a/Lib/ctypes/_endian.py +++ b/Lib/ctypes/_endian.py @@ -3,34 +3,41 @@ _array_type = type(Array) +def _get_other_endian_attr(): + """Return the appropriate other endian attribute based on system byte order.""" + return "__ctype_be__" if sys.byteorder == "little" else "__ctype_le__" + def _other_endian(typ): - """Return the type with the 'other' byte order. Simple types like + """Return the type with the 'other' byte order. Simple types like c_int and so on already have __ctype_be__ and __ctype_le__ - attributes which contain the types, for more complicated types + attributes which contain the types; for more complicated types, arrays and structures are supported. """ - # check _OTHER_ENDIAN attribute (present if typ is primitive type) - if hasattr(typ, _OTHER_ENDIAN): - return getattr(typ, _OTHER_ENDIAN) + other_endian_attr = _get_other_endian_attr() + # if typ is array if isinstance(typ, _array_type): return _other_endian(typ._type_) * typ._length_ # if typ is structure or union if issubclass(typ, (Structure, Union)): return typ + # check other endian attribute (present if typ is primitive type) + if hasattr(typ, other_endian_attr): + return getattr(typ, other_endian_attr) + raise TypeError("This type does not support other endian: %s" % typ) class _swapped_meta: def __setattr__(self, attrname, value): if attrname == "_fields_": - fields = [] - for desc in value: - name = desc[0] - typ = desc[1] - rest = desc[2:] - fields.append((name, _other_endian(typ)) + rest) + # Use a generator expression to avoid creating a new list + fields = [ + (desc[0], _other_endian(desc[1])) + desc[2:] + for desc in value + ] value = fields super().__setattr__(attrname, value) + class _swapped_struct_meta(_swapped_meta, type(Structure)): pass class _swapped_union_meta(_swapped_meta, type(Union)): pass @@ -40,9 +47,8 @@ class _swapped_union_meta(_swapped_meta, type(Union)): pass # value!) of a _swappedbytes_ attribute to determine the bit order in # structures containing bit fields. +# Determine the byte order and define types accordingly if sys.byteorder == "little": - _OTHER_ENDIAN = "__ctype_be__" - LittleEndianStructure = Structure class BigEndianStructure(Structure, metaclass=_swapped_struct_meta): @@ -58,8 +64,6 @@ class BigEndianUnion(Union, metaclass=_swapped_union_meta): _swappedbytes_ = None elif sys.byteorder == "big": - _OTHER_ENDIAN = "__ctype_le__" - BigEndianStructure = Structure class LittleEndianStructure(Structure, metaclass=_swapped_struct_meta): From 48501d7d31a194568715d7c034af6494c9127955 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:05:35 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/C_API/2024-09-23-17-05-34.gh-issue-124351.ba9txm.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-23-17-05-34.gh-issue-124351.ba9txm.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-23-17-05-34.gh-issue-124351.ba9txm.rst b/Misc/NEWS.d/next/C_API/2024-09-23-17-05-34.gh-issue-124351.ba9txm.rst new file mode 100644 index 00000000000000..b5c0e02b911af8 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-23-17-05-34.gh-issue-124351.ba9txm.rst @@ -0,0 +1,3 @@ +```rst +Optimized endianness handling in the ctypes module. The `_other_endian` function has been refactored to reduce redundant type checks, improving performance during frequent calls. Additionally, the `_swapped_meta` class now efficiently handles attribute assignments without unnecessary list creation, enhancing overall efficiency. This update provides better handling of data serialization across different byte orders, ensuring compatibility and performance improvements in cross-platform applications. +``` From eaf92c0557591b77c7f458b07987b3843a061d6e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:25:38 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/C_API/2024-09-23-17-25-37.gh-issue-124351.YJ6wdd.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-23-17-25-37.gh-issue-124351.YJ6wdd.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-23-17-25-37.gh-issue-124351.YJ6wdd.rst b/Misc/NEWS.d/next/C_API/2024-09-23-17-25-37.gh-issue-124351.YJ6wdd.rst new file mode 100644 index 00000000000000..002e7093591829 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-23-17-25-37.gh-issue-124351.YJ6wdd.rst @@ -0,0 +1,2 @@ +.. default-role:: code +Optimized endianness handling in the ctypes module. The `_other_endian` function has been refactored to reduce redundant type checks, improving performance during frequent calls. Additionally, the `_swapped_meta` class now efficiently handles attribute assignments without unnecessary list creation, enhancing overall efficiency. This update provides better handling of data serialization across different byte orders, ensuring compatibility and performance improvements in cross-platform applications. From e1f3176de1fe8405dff3d820e42ca65940320f46 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:28:38 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/C_API/2024-09-23-17-28-37.gh-issue-124351.hszcK-.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-23-17-28-37.gh-issue-124351.hszcK-.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-23-17-28-37.gh-issue-124351.hszcK-.rst b/Misc/NEWS.d/next/C_API/2024-09-23-17-28-37.gh-issue-124351.hszcK-.rst new file mode 100644 index 00000000000000..3673cc2a7019f0 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-23-17-28-37.gh-issue-124351.hszcK-.rst @@ -0,0 +1,3 @@ +.. default-role:: code + +Optimized endianness handling in the ctypes module. The `_other_endian` function has been refactored to reduce redundant type checks, improving performance during frequent calls. Additionally, the `_swapped_meta` class now efficiently handles attribute assignments without unnecessary list creation, enhancing overall efficiency. This update provides better handling of data serialization across different byte orders, ensuring compatibility and performance improvements in cross-platform applications. From d52b6a444c9417971da54ee71b0a3a5f1928979a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 02:50:55 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/C_API/2024-09-24-02-50-54.gh-issue-124351.BuQG-z.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-24-02-50-54.gh-issue-124351.BuQG-z.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-24-02-50-54.gh-issue-124351.BuQG-z.rst b/Misc/NEWS.d/next/C_API/2024-09-24-02-50-54.gh-issue-124351.BuQG-z.rst new file mode 100644 index 00000000000000..a39cc0e8be499d --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-24-02-50-54.gh-issue-124351.BuQG-z.rst @@ -0,0 +1,3 @@ +- Fixed a potential side effect from the global variable `_OTHER_ENDIAN` in the `_other_endian` function by ensuring it’s not declared at the module level. +- Reduced redundant type checks within the `_other_endian` recursion to enhance performance during frequent calls. +- Optimized the `setattr` method in `_swapped_meta` to prevent unnecessary list creation, reducing overhead when no modifications are required. From 5dd3b0bd918e0c2cce3dc73f8bbf4cdeaa0f65f4 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 02:54:52 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C_API/2024-09-24-02-54-51.gh-issue-124351.XtNTlp.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-24-02-54-51.gh-issue-124351.XtNTlp.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-24-02-54-51.gh-issue-124351.XtNTlp.rst b/Misc/NEWS.d/next/C_API/2024-09-24-02-54-51.gh-issue-124351.XtNTlp.rst new file mode 100644 index 00000000000000..ff93cf9eff6f57 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-24-02-54-51.gh-issue-124351.XtNTlp.rst @@ -0,0 +1,6 @@ +Fix potential side effect from the global variable `_OTHER_ENDIAN` in the +`_other_endian` function by ensuring it is not declared at the module level. +Reduce redundant type checks within the `_other_endian` recursion to enhance +performance during frequent calls. Optimize the `setattr` method in +`_swapped_meta` to prevent unnecessary list creation, reducing overhead when +no modifications are required. From e2f63ebf96080746613008e02c8b6d32acda4ed3 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 02:55:00 +0000 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C_API/2024-09-24-02-54-59.gh-issue-124351.XtNTlp.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-24-02-54-59.gh-issue-124351.XtNTlp.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-24-02-54-59.gh-issue-124351.XtNTlp.rst b/Misc/NEWS.d/next/C_API/2024-09-24-02-54-59.gh-issue-124351.XtNTlp.rst new file mode 100644 index 00000000000000..ff93cf9eff6f57 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-24-02-54-59.gh-issue-124351.XtNTlp.rst @@ -0,0 +1,6 @@ +Fix potential side effect from the global variable `_OTHER_ENDIAN` in the +`_other_endian` function by ensuring it is not declared at the module level. +Reduce redundant type checks within the `_other_endian` recursion to enhance +performance during frequent calls. Optimize the `setattr` method in +`_swapped_meta` to prevent unnecessary list creation, reducing overhead when +no modifications are required. From b927068ba75695978c9a9ed56803c06fe02d7ace Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 02:59:24 +0000 Subject: [PATCH 8/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C_API/2024-09-24-02-59-23.gh-issue-124351.XtNTlp.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2024-09-24-02-59-23.gh-issue-124351.XtNTlp.rst diff --git a/Misc/NEWS.d/next/C_API/2024-09-24-02-59-23.gh-issue-124351.XtNTlp.rst b/Misc/NEWS.d/next/C_API/2024-09-24-02-59-23.gh-issue-124351.XtNTlp.rst new file mode 100644 index 00000000000000..ff93cf9eff6f57 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2024-09-24-02-59-23.gh-issue-124351.XtNTlp.rst @@ -0,0 +1,6 @@ +Fix potential side effect from the global variable `_OTHER_ENDIAN` in the +`_other_endian` function by ensuring it is not declared at the module level. +Reduce redundant type checks within the `_other_endian` recursion to enhance +performance during frequent calls. Optimize the `setattr` method in +`_swapped_meta` to prevent unnecessary list creation, reducing overhead when +no modifications are required.