Commit Graph

460 Commits

Author SHA1 Message Date
Miss Islington (bot) 9979635612
[3.11] gh-107461 ctypes: Add a testcase for nested `_as_parameter_` lookup (GH-107462) (GH-114859)
(cherry picked from commit 0bf42dae7e)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
2024-02-01 12:08:52 +00:00
Miss Islington (bot) f5d9980217
[3.11] gh-114257: Ignore the FileNotFound error in ctypes.util._is_elf() (GH-114394) (GH-114445)
(cherry picked from commit 7fc51c3f6b)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
2024-01-22 18:10:44 +00:00
Serhiy Storchaka 2b03259022
[3.11] gh-105102: Fix nested unions in structures when the system byteorder is the opposite (GH-105106) (GH-114205)
(cherry picked from commit 0b541f64c4)

Co-authored-by: Sheidan <37596668+Sh3idan@users.noreply.github.com>
2024-01-17 19:58:28 +00:00
Diego Russo 5da53d3024
[3.11] gh-110190: Fix ctypes structs with array on PPCLE64 (GH-112959) (#113167)
Fix the same issue of PR #112604 on PPC64LE platform
Refactor tests to make easier to add more platfroms if needed.

(cherry picked from commit 6644ca45cd)
Change-Id: I1ada30808c0d593a43eca3fa7a628c26bc276310
2024-01-05 16:38:20 +01:00
Serhiy Storchaka 36153494f1
[3.11] gh-62260: Fix ctypes.Structure subclassing with multiple layers (GH-13374) (GH-113624)
The length field of StgDictObject for Structure class contains now
the total number of items in ffi_type_pointer.elements (excluding
the trailing null).

The old behavior of using the number of elements in the parent class can
cause the array to be truncated when it is copied, especially when there
are multiple layers of subclassing.

(cherry picked from commit 5f3cc90a12)

Co-authored-by: Jeffrey Kintscher <49998481+websurfer5@users.noreply.github.com>
2024-01-01 16:46:07 +00:00
Łukasz Langa 1e6d36da1e
[3.11] gh-110190: Temporarily skip new test introduced in gh-112604 on PPC64LE (GH-112818) (#112830)
(cherry picked from commit 9f67042f28)
2023-12-08 12:47:32 +01:00
Diego Russo b49c963e85
[3.11] gh-110190: Fix ctypes structs with array on Arm (#112604) (#112766)
Set MAX_STRUCT_SIZE to 32 in stgdict.c when on Arm platforms.
This because on Arm platforms structs with at most 4 elements of any
floating point type values can be passed through registers. If the type
is double the maximum size of the struct is 32 bytes.
On x86-64 Linux, it's maximum 16 bytes hence we need to differentiate.

(cherry picked from commit bc68f4a4ab)
2023-12-06 16:57:42 +01:00
Alex Waygood 5f7aba938c
[3.11] Enable ruff on several more files in `Lib/test` (#110929) (#110935)
(cherry-picked from commit 02d26c4bef)
2023-10-16 23:17:59 +03:00
Łukasz Langa c3d129f07e
[3.11] Revert "gh-46376: Return existing pointer when possible in ctypes (GH-107131) (GH-107488)" (#108412)
This reverts commit 57f27e4441.

The fix caused gh-107940. Until we have a bulletproof fix for that, the 3.11 backport needs to be reverted to make way for 3.11.5.
2023-08-24 13:26:19 +02:00
Łukasz Langa 1b40431189
[3.11] gh-105751: Remove obsolete `object` base class in some ctypes tests (GH-107460) (#107502)
(cherry picked from commit 520efecfc3)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
2023-07-31 16:40:47 +02:00
Łukasz Langa 57f27e4441
[3.11] gh-46376: Return existing pointer when possible in ctypes (GH-107131) (#107488)
(cherry picked from commit 08447b5deb)

Co-authored-by: Konstantin <kpp.live+github@gmail.com>
2023-07-31 11:16:59 +02:00
Łukasz Langa aa5f2b1f3c
[3.11] gh-105751, test_ctypes: Remove disabled tests (GH-105826) (#107484)
* The following tests were disabled since the initial ctypes commit
  in 2006, commit babddfca758abe34ff12023f63b18d745fae7ca9:

  * Callbacks.test_char_p()
  * DeletePointerTestCase.test_X()
  * NumberTestCase.test_perf()
  * StructureTestCase.test_subclass_creation()
  * Tests.test_X() of test_byteswap

* NumberTestCase.test_bool_from_address() was disabled in 2007 by
  commit 5dc4fe09b7.
* Remove check_perf() and run_test() of test_numbers.
(cherry picked from commit 8f10140e74)

Co-authored-by: Victor Stinner <vstinner@python.org>
2023-07-31 10:28:45 +02:00
Gregory P. Smith fa7c37af49
[3.11] gh-99952: fix refcount issues in ctypes.Structure from_param() result (#101339)
[3.11] gh-99952: [ctypes] fix refcount issues in from_param() result. (GH-100169)

Fixes a reference counting issue with `ctypes.Structure` when a `from_param()` method call is used and the structure size is larger than a C pointer `sizeof(void*)`.

This problem existed for a very long time, but became more apparent in 3.8+ by change likely due to garbage collection cleanup timing changes..
(cherry picked from commit dfad678d70)

Co-authored-by: Yukihiro Nakadaira <yukihiro.nakadaira@gmail.com>
2023-01-26 01:02:37 -08:00
Miss Islington (bot) 2d1128e9eb
GH-81061: Fix refcount issue when returning `None` from a `ctypes.py_object` callback (GH-13364)
(cherry picked from commit 837ba05267)

Co-authored-by: dgelessus <dgelessus@users.noreply.github.com>
2023-01-09 08:06:17 -08:00
Miss Islington (bot) f6fe4bb75c
[3.11] gh-76963: PEP3118 itemsize of an empty ctypes array should not be 0 (GH-5576) (GH-100452)
gh-76963: PEP3118 itemsize of an empty ctypes array should not be 0 (GH-5576)

The itemsize returned in a memoryview of a ctypes array is now computed from the item type, instead of dividing the total size by the length and assuming that the length is not zero.
(cherry picked from commit 84bc6a4f25)

Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
2022-12-23 08:56:20 +00:00
Miss Islington (bot) 3e715e0cc8
gh-78878: Fix crash when creating an instance of `_ctypes.CField` (GH-14837)
(cherry picked from commit d713c54ac8)

Co-authored-by: Hai Shi <shihai1992@gmail.com>
2022-12-21 10:02:05 -08:00
Miss Islington (bot) bf76d9bd4d
gh-99275: Fix `SystemError` in `ctypes` during `__initsubclass__` (GH-99283)
(cherry picked from commit 343eb0f94b)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-11-13 11:51:26 -08:00
Miss Islington (bot) 2d00190591
gh-92119: ctypes: Print exception class name instead of its representation (GH-98302)
(cherry picked from commit b9dedfe61d)

Co-authored-by: Kamil Turek <kamil.turek@hotmail.com>
2022-11-07 21:20:06 -08:00
Miss Islington (bot) 4d2c972ff7
gh-84461: Fix ctypes and test_ctypes on Emscripten (GH-94142)
- c_longlong and c_longdouble need experimental WASM bigint.
- Skip tests that need threading
- Define ``CTYPES_MAX_ARGCOUNT`` for Emscripten. libffi-emscripten 2022-06-23 supports up to 1000 args.
(cherry picked from commit 8625802d85)

Co-authored-by: Christian Heimes <christian@python.org>
2022-06-24 04:17:21 -07:00
Oleg Iarygin 804ea2da97
gh-91265: Make old ctypes.macholib tests runned by `python -m test` (gh-32094)
* Move tests from ctypes.macholib.dy* to ctypes.test
2022-04-18 09:27:09 +02:00
Christian Heimes d1b1c885d8
bpo-47208: Allow vendors to override CTYPES_MAX_ARGCOUNT (GH-32297) 2022-04-04 19:13:42 +02:00
Dave Goncalves dc2d8404a3
bpo-33178: Add BigEndianUnion, LittleEndianUnion classes to ctypes (GH-25480)
* bpo-33178: Add BigEndianUnion, LittleEndianUnion classes to ctypes
* GH-25480: remove trailing whitespace in ctypes doc
* GH-25480: add news entry blurb
* GH-25480: corrected formatting error in news blurb
* GH-25480: simplified, corrected formatting in news blurb
* GH-25480: remove trailing whitespace in news blurb
* GH-25480: fixed class markup in news blurb
* GH-25480: fixed unsupported type tests and naming per review comments
* GH-25480: fixed whitepace errors
* condensed base class selection for unsupported byte order tests
* added versionadded tags for new EndianUnion classes
2022-03-29 14:26:27 -07:00
Victor Stinner ad1b04451d
bpo-46913: Skip test_ctypes.test_shorts() on UBSan (GH-31674)
If Python is built with UBSan, test_ctypes now skips test_shorts().
This change allows to run test_ctypes to check for new UBSan regression,
but the known test_shorts() undefined behavior must be fixed.
2022-03-04 00:41:57 +01:00
Kumar Aditya e182c660b6
bpo-46748: Fix ctypes test_frozentable() (GH-31600) 2022-02-27 00:25:46 +01:00
Victor Stinner 4cce1352bb
bpo-46323: _ctypes.CFuncPtr fails if _argtypes_ is too long (GH-31188)
ctypes.CFUNCTYPE() and ctypes.WINFUNCTYPE() now fail to create the
type if its "_argtypes_" member contains too many arguments.
Previously, the error was only raised when calling a function.

Change also how CFUNCTYPE() and WINFUNCTYPE() handle KeyError to
prevent creating a chain of exceptions if ctypes.CFuncPtr raises an
error.
2022-02-07 14:53:15 +01:00
Kumar Aditya bf95ff91f2
bpo-46608: exclude marshalled-frozen data if deep-freezing to save 300 KB space (GH-31074)
This reduces the size of the data segment by **300 KB** of the executable because if the modules are deep-frozen then the marshalled frozen data just wastes space. This was inspired by comment by @gvanrossum in https://github.com/python/cpython/pull/29118#issuecomment-958521863. Note: There is a new option `--deepfreeze-only` in `freeze_modules.py` to change this behavior, it is on be default to save disk space.
```console 
# du -s ./python before
27892   ./python
# du -s ./python after
27524   ./python
```

Automerge-Triggered-By: GH:ericsnowcurrently
2022-02-04 09:57:03 -08:00
Nikita Sobolev 913e340a32
bpo-46584: remove check for `py2.3` from `ctypes/test_python_api` (GH-31024) 2022-02-01 05:56:25 -08:00
Nikita Sobolev b48ac6fe38
bpo-45878: convert `try/except` to `self.assertRaises` in `Lib/ctypes/test/test_functions.py` (GH-29721) 2021-11-23 13:12:13 +02:00
Guido van Rossum 1cbaa505d0
bpo-45696: Deep-freeze selected modules (GH-29118)
This gains 10% or more in startup time for `python -c pass` on UNIX-ish systems.

The Makefile.pre.in generating code builds on Eric's work for bpo-45020, but the .c file generator is new.

Windows version TBD.
2021-11-10 18:01:53 -08:00
Eric Snow 074fa57506
bpo-45395: Make custom frozen modules additions instead of replacements. (gh-28778)
Currently custom modules (the array set on PyImport_FrozenModules) replace all the frozen stdlib modules. That can be problematic and is unlikely to be what the user wants. This change treats the custom frozen modules as additions instead. They take precedence over all other frozen modules except for those needed to bootstrap the import system. If the "code" field of an entry in the custom array is NULL then that frozen module is treated as disabled, which allows a custom entry to disable a frozen stdlib module.

This change allows us to get rid of is_essential_frozen_module() and simplifies the logic for which frozen modules should be ignored.

https://bugs.python.org/issue45395
2021-10-28 15:04:33 -06:00
Christian Clauss 745c9d9dfc
Fix typos in the Lib directory (GH-28775)
Fix typos in the Lib directory as identified by codespell.

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
2021-10-06 16:13:48 -07:00
Eric Snow 3814e2036d
bpo-45019: Clean up the frozen __hello__ module. (gh-28374)
Here's one more small cleanup that should have been in PR gh-28319. We eliminate stdout side-effects from importing the frozen __hello__ module, and update tests accordingly. We also move the module's source file into Lib/ from Toos/freeze/flag.py.

https://bugs.python.org/issue45019
2021-09-15 14:15:32 -06:00
Eric Snow 4b30aaa0c9
bpo-45019: Silence a warning in test_ctypes. (gh-28362)
This was missed in PR gh-28319.

https://bugs.python.org/issue45019
2021-09-15 13:33:05 -06:00
Eric Snow a65c86889e
bpo-45020: Add -X frozen_modules=[on|off] to explicitly control use of frozen modules. (gh-28320)
Currently we freeze several modules into the runtime. For each of these modules it is essential to bootstrapping the runtime that they be frozen. Any other stdlib module that we later freeze into the runtime is not essential. We can just as well import from the .py file.  This PR lets users explicitly choose which should be used, with the new "-X frozen_modules=[on|off]" CLI flag. The default is "off" for now.

https://bugs.python.org/issue45020
2021-09-14 17:31:45 -06:00
Eric Snow a2d8c4b81b
bpo-45019: Do some cleanup related to frozen modules. (gh-28319)
There are a few things I missed in gh-27980. This is a follow-up that will make subsequent PRs cleaner. It includes fixes to tests and tools that reference the frozen modules.

https://bugs.python.org/issue45019
2021-09-13 16:18:37 -06:00
andrei kulakov a561005761
bpo-19113: Remove unused test_errors from ctypes tests (GH-28008)
This test was forever shadowed by another test method named `test_errors`.
2021-09-08 17:28:32 +02:00
Victor Stinner a1e15a7a60
bpo-45082: Cleanup ctypes.c_buffer alias (GH-28129)
* Remove commented deprecation of ctypes.c_buffer.
* Remove references to ctypes.c_string which doesn't exist.
* Remove StringTestCase: it only had skipped test methods.
2021-09-02 19:02:03 +02:00
Serhiy Storchaka 3c65457156
bpo-45060: Get rid of few uses of the equality operators with None (GH-28087) 2021-08-31 16:59:52 +03:00
Noah Kantrowitz be42c06bb0
Update URLs in comments and metadata to use HTTPS (GH-27458) 2021-07-30 15:54:46 +02:00
Filipe Laíns e14d5ae544
bpo-29753: revert 0d7ad9f (GH-19850) (GH-27085)
This reverts commit 0d7ad9fb38 as it has a regression.

See https://github.com/python/cpython/pull/19850#issuecomment-869410686
2021-07-11 17:43:50 +01:00
Gabriele N. Tornetta 2f180ce2cb
bpo-44530: Add co_qualname field to PyCodeObject (GH-26941) 2021-07-07 12:21:51 +01:00
Pablo Galindo 98eee94421
bpo-43950: Add code.co_positions (PEP 657) (GH-26955)
This PR is part of PEP 657 and augments the compiler to emit ending
line numbers as well as starting and ending columns from the AST
into compiled code objects. This allows bytecodes to be correlated
to the exact source code ranges that generated them.

This information is made available through the following public APIs:

* The `co_positions` method on code objects.
* The C API function `PyCode_Addr2Location`.

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
Co-authored-by: Ammar Askar <ammar@ammaraskar.com>
2021-07-02 15:10:11 +01:00
Guido van Rossum 355f5dd36a
bpo-43693: Turn localspluskinds into an object (GH-26749)
Managing it as a bare pointer to malloc'ed bytes is just too awkward in a few places.
2021-06-21 13:53:04 -07:00
Eric Snow 2ab27c4af4
bpo-43693: Un-revert commits 2c1e258 and b2bf2bc. (gh-26577)
These were reverted in gh-26530 (commit 17c4edc) due to refleaks.

* 2c1e258 - Compute deref offsets in compiler (gh-25152)
* b2bf2bc - Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388)

This change fixes the refleaks.

https://bugs.python.org/issue43693
2021-06-07 12:22:26 -06:00
Pablo Galindo 17c4edc4e0
bpo-43693: Revert commits 2c1e2583fd and b2bf2bc1ec (GH-26530)
* Revert "bpo-43693: Compute deref offsets in compiler (gh-25152)"

This reverts commit b2bf2bc1ec.

* Revert "bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388)"

This reverts commit 2c1e2583fd.

These two commits are breaking the refleak buildbots.
2021-06-04 17:51:05 +01:00
Eric Snow 2c1e2583fd
bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388)
A number of places in the code base (notably ceval.c and frameobject.c) rely on mapping variable names to indices in the frame "locals plus" array (AKA fast locals), and thus opargs.  Currently the compiler indirectly encodes that information on the code object as the tuples co_varnames, co_cellvars, and co_freevars.  At runtime the dependent code must calculate the proper mapping from those, which isn't ideal and impacts performance-sensitive sections.  This is something we can easily address in the compiler instead.

This change addresses the situation by replacing internal use of co_varnames, etc. with a single combined tuple of names in locals-plus order, along with a minimal array mapping each to its kind (local vs. cell vs. free).  These two new PyCodeObject fields, co_fastlocalnames and co_fastllocalkinds, are not exposed to Python code for now, but co_varnames, etc. are still available with the same values as before (though computed lazily).

Aside from the (mild) performance impact, there are a number of other benefits:

* there's now a clear, direct relationship between locals-plus and variables
* code that relies on the locals-plus-to-name mapping is simpler
* marshaled code objects are smaller and serialize/de-serialize faster

Also note that we can take this approach further by expanding the possible values in co_fastlocalkinds to include specific argument types (e.g. positional-only, kwargs).  Doing so would allow further speed-ups in _PyEval_MakeFrameVector(), which is where args get unpacked into the locals-plus array.  It would also allow us to shrink marshaled code objects even further.

https://bugs.python.org/issue43693
2021-06-03 10:28:27 -06:00
Eric Snow 9f494d4929
bpo-43693: Add _PyCode_New(). (gh-26375)
This is an internal-only API that helps us manage the many values used to create a code object.

https://bugs.python.org/issue43693
2021-05-27 09:54:34 -06:00
Mark Shannon adcd220556
bpo-40222: "Zero cost" exception handling (GH-25729)
"Zero cost" exception handling.

* Uses a lookup table to determine how to handle exceptions.
* Removes SETUP_FINALLY and POP_TOP block instructions, eliminating (most of) the runtime overhead of try statements.
* Reduces the size of the frame object by about 60%.
2021-05-07 15:19:19 +01:00
Zackery Spytz 73766b0341
bpo-32745: Fix a regression in the handling of ctypes' c_wchar_p type (#8721)
Embedded nulls would cause a ValueError to be raised. Thanks go to
Eryk Sun for their analysis.

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-05-02 12:40:01 +02:00
Mark Shannon c76da79b37
bpo-42739: Don't use sentinels to mark end of line table. (GH-25657)
* Add length parameter to PyLineTable_InitAddressRange and doen't use sentinel values at end of table. Makes the line number table more robust.

* Update PyCodeAddressRange to match PEP 626.
2021-04-29 13:12:51 +01:00