Commit Graph

3603 Commits

Author SHA1 Message Date
David Hewitt eafc75ab06
Merge pull request #1777 from indygreg/unicode-apis
ffi: define some cpython/unicodeobject bindings
2021-08-14 23:07:49 +01:00
Gregory Szorc 807c8ee33c string: enable use of PyUnicode_AsUTF8AndSize on all Python versions
This API is available in all supported Python versions and isn't
deprecated.
2021-08-14 11:19:02 -07:00
David Hewitt 974a782895 ffi: PyPy and Python 3.10 attributes for unicodeobject 2021-08-14 10:09:57 -07:00
Gregory Szorc 9690e9ece6 ffi: move limited API unicode symbols to cpython/unicodeobject.rs
All symbols which are canonically defined in cpython/unicodeobject.h
and had been defined in our unicodeobject.rs have been moved to our
corresponding cpython/unicodeobject.rs file.

This module is only present in non-limited build configurations, so
we were able to drop the cfg annotations as part of moving the
definitions.
2021-08-14 08:19:10 -07:00
Gregory Szorc b8738b0430 ffi: define some cpython/unicodeobject bindings
pyo3 doesn't currently define various Unicode bindings that allow the
retrieval of raw data from Python strings. Said bindings are a
prerequisite to possibly exposing this data in the Rust API (#1776).
Even if those high-level APIs never materialize, the FFI bindings are
necessary to enable consumers of the raw C API to utilize them.

This commit partially defines the FFI bindings as defined in
CPython's Include/cpython/unicodeobject.h file.

I used the latest CPython 3.9 Git commit for defining the order
of the symbols and the implementation of various inline preprocessor
macros. I tried to be as faithful as possible to the original
implementation, preserving intermediate `#define`s as inline functions.

Missing symbols have been annotated with `skipped` and symbols currently
defined in `src/ffi/unicodeobject.rs` have been annotated with `move`.

The `state` field of `PyASCIIObject` is a bitfield, which Rust doesn't
support. So we've provided accessor functions for retrieving these
fields' values. No accessor functions are present because you shouldn't
be touching these values from Rust code.

Tests of the bitfield APIs and macro implementations have been added.
2021-08-14 08:19:10 -07:00
David Hewitt 584de688c3
Merge pull request #1779 from indygreg/setter-handle-del
macros: raise AttributeError on property deletion requests
2021-08-14 08:57:33 +01:00
David Hewitt 78f269e946
Merge branch 'main' into setter-handle-del 2021-08-14 08:07:15 +01:00
David Hewitt 739a885f5a
Merge pull request #1791 from indygreg/fix-pystatus
ffi: fix PyStatus._type
2021-08-14 08:05:40 +01:00
David Hewitt 30e6f58080
Update CHANGELOG.md 2021-08-14 07:43:32 +01:00
David Hewitt 150f4adbba
Update CHANGELOG.md 2021-08-14 07:40:52 +01:00
Gregory Szorc 410c9f13c9 macros: raise AttributeError on property deletion requests
The setter function will receive a NULL value on deletion requests.
This wasn't properly handled before, leading to a panic.

The new code raises AttributeError in this scenario instead.

A test for the behavior has been added. Documentation has also
been updated to reflect the behavior.
2021-08-13 21:01:00 -07:00
Gregory Szorc d3762a679f ffi: fix PyStatus._type
The field wasn't defined previously. And the enum wasn't defined as
`[repr(C)]`.

This missing field could result in memory corruption if a Rust-allocated
`PyStatus` was passed to a Python API, which could perform an
out-of-bounds write. In my code, the out-of-bounds write corrupted a
variable on the stack, leading to a segfault due to illegal memory
access. However, this crash only occurred on Rust 1.54! So I initially
mis-attribted it as a compiler bug / regression. It appears that a
low-level Rust change in 1.54.0 changed the LLVM IR in such a way to
cause LLVM optimization passes to produce sufficiently different
assembly code, tickling the crash. See
https://github.com/rust-lang/rust/issues/87947 if you want to see
the wild goose chase I went on in Rust / LLVM land to potentially
pin this on a compiler bug.

Lessen learned: Rust crashes are almost certainly due to use of
`unsafe`.
2021-08-13 20:51:26 -07:00
David Hewitt 254ea53f3f
Merge pull request #1789 from davidhewitt/example-word-count
examples: make `word-count` example comparison fairer
2021-08-13 15:08:16 +01:00
David Hewitt e44920ef9c
Merge pull request #1790 from PyO3/davidhewitt-patch-1
ffi: fix PyPy symbols for `cpython/pystate.rs`
2021-08-13 15:07:29 +01:00
David Hewitt 06ff76fec2
examples: make `word-count` example comparison fairer 2021-08-13 14:19:46 +01:00
David Hewitt c439f97f1a
ffi: fix PyPy symbols for `cpython/pystate.rs` 2021-08-13 14:07:15 +01:00
David Hewitt 388c2552f8
Merge pull request #1786 from mejrs/with_gil2
tests: switch to python_with_gil
2021-08-13 12:39:17 +01:00
David Hewitt 874e7e5cab
Merge pull request #1787 from davidhewitt/ffi-pystate
ffi: cleanup pystate
2021-08-13 12:36:37 +01:00
David Hewitt 0bfd50d314 ffi: cleanup pystate 2021-08-13 08:26:38 +01:00
mejrs 596c3ff4b7 remove now unused imports 2021-08-12 12:08:54 +02:00
mejrs 9fe3e2b2b8
Apply suggestions from code review
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2021-08-12 11:52:56 +02:00
David Hewitt 0b269c4d3d
Merge pull request #1678 from sansyrox/main
Add documentation to call async python from rust
2021-08-12 08:58:46 +01:00
messense 4c734ef72a
Merge pull request #1783 from PyO3/davidhewitt-patch-1
changelog: fix missing PR number
2021-08-12 10:33:10 +08:00
messense 2a7c4f495b
Merge pull request #1784 from davidhewitt/gh-actions-pages-v3
guide: allow using newer gh-actions-pages version
2021-08-12 10:32:35 +08:00
messense d197357478
Merge pull request #1785 from davidhewitt/pin-bitflags
ci: pin bitflags for MSRV job
2021-08-12 10:31:13 +08:00
mejrs f55a9c8ab0 tests: switch to python_with_gil 2021-08-12 02:47:41 +02:00
David Hewitt 4eee9009db ci: pin bitflags for MSRV job 2021-08-12 00:27:49 +01:00
David Hewitt bdad08ce6a
Merge pull request #1717 from messense/pyerr-fetch
Change `PyErr::fetch` to return an `Option<PyErr>`
2021-08-12 00:24:15 +01:00
Sanskar Jethi aac3d1da6e Add documentation to call async python from rust 2021-08-12 00:22:10 +01:00
David Hewitt 72f3f1131b guide: allow using newer gh-actions-pages version 2021-08-11 23:50:58 +01:00
David Hewitt c59b51b85b
changelog: fix missing PR number 2021-08-11 23:49:01 +01:00
messense 3ba24bee14 `PyErr::api_call_failed` should return a `SystemError` when no error is set
17f94e2888/Python/ceval.c (L4330-L4333)
2021-08-11 23:45:09 +01:00
messense 93b25edba1 Use `errror_on_minusone` more often 2021-08-11 23:44:40 +01:00
messense 9d0b92bf32 Change `PyErr::fetch` to return an `Option<PyErr>` 2021-08-11 23:44:40 +01:00
David Hewitt 140c090504
Merge pull request #1780 from davidhewitt/pin-trybuild
ci: downgrade trybuild to 1.0.43
2021-08-11 09:26:41 +01:00
David Hewitt c59f518f76 ci: downgrade trybuild to 1.0.43 2021-08-11 08:39:37 +01:00
David Hewitt 76e79d615c
Merge pull request #1772 from davidhewitt/async-guide-fixes
guide: don't bother doctesting async guide
2021-08-11 00:52:25 +01:00
David Hewitt 290ded4d4e guide: don't bother doctesting async guide 2021-08-10 07:56:36 +01:00
David Hewitt 6a9ef543c1
Merge pull request #1771 from awestlake87/master
Problems with the new Async/Await guide
2021-08-10 07:48:58 +01:00
Andrew J Westlake f4975c6e1d Fixed broken link in async-await.md 2021-08-09 09:07:57 -05:00
David Hewitt ccdc26f971
Merge pull request #1760 from PyO3/release-0.14.2
release: 0.14.2
2021-08-09 09:03:51 +01:00
David Hewitt 1873264b7d release: 0.14.2 2021-08-09 07:58:31 +01:00
David Hewitt 575c448345
Merge pull request #1769 from indygreg/force-acquire-gil
gil: add unsafe variation for obtaining GILGuard without checks
2021-08-09 07:49:06 +01:00
Gregory Szorc 3a6740a459 gil: add unsafe variation for obtaining GIL without checks
GILGuard::acquire() cannot be called during multi-phase Python
interpreter initialization because it calls Py_IsInitialized(),
which doesn't report the interpreter as initialized until all
phases of initialization have completed.

PyOxidizer uses the multi-phase initialization API and needs to
interact with pyo3's high-level APIs (not the FFI bindings) after
partial interpreter initialization, before the interpreter is fully
initialized. Attempts to use GILGuard::acquire() result in a panic
due to the aforementioned Py_IsInitialized() check failing.

This commit refactors the GILGuard logic into a function that
obtains the actual GILGuard and another function to perform
checks before calling the aforementioned functions.

A new unsafe `Python::with_gil_unchecked()` has been defined
to acquire the GIL via the unchecked code path so we may obtain
a `Python` during multi-phase initialization (and possibly other
scenarios).
2021-08-08 15:40:10 -07:00
David Hewitt 91615338ae
Merge pull request #1764 from awestlake87/master
Updated async-await guide for pyo3-asyncio 0.14
2021-08-08 23:14:43 +01:00
David Hewitt edec1f12d6
Merge pull request #1763 from deantvv/ffi-cleanup
clean up ffi from pyframe to pyhash
2021-08-08 23:09:19 +01:00
David Hewitt 473cd5cc4f
Merge pull request #1765 from indygreg/frozen-public
ffi: make _frozen fields public
2021-08-08 23:09:00 +01:00
David Hewitt ac92b79f83
Merge pull request #1768 from indygreg/decodelocale-signature
ffi: fix Py_DecodeLocale() signature
2021-08-08 23:08:45 +01:00
Andrew J Westlake bf23f8bda7 Shortened the async/await guide 2021-08-08 14:58:46 -05:00
David Hewitt 3de5591534
Merge pull request #1767 from indygreg/packagecontext
ffi: define _Py_PackageContext
2021-08-08 19:23:18 +01:00