Commit Graph

1665 Commits

Author SHA1 Message Date
Georg Brandl ead86bd666 PySequence: add missing tests 2021-08-23 16:38:59 +02:00
Georg Brandl 925fef1ece PySequence: fix the in_place methods to
a) not leak a reference
b) correctly return the result, since for immutable types `self` is not actually mutated in place
2021-08-23 15:47:09 +02:00
Georg Brandl b55d62dd3e PySequence: use usize everywhere
See #1667
2021-08-23 15:47:09 +02:00
David Hewitt 591f44ea6d
Merge pull request #1794 from indygreg/pystringdata
string: implement API to access raw string data
2021-08-21 22:54:58 +01:00
David Hewitt 17077fd05e
Merge pull request #1816 from mejrs/dedup
deduplicate pyo3-macros documentation
2021-08-21 09:23:50 +01:00
Benjamin Kay ac4613eb9f
Improve documentation about when we free memory, resolves #311 (#1807)
* Improve API docs regarding when we free memory, resolves #311

* Add chapter to guide about when we free memory, resolves #311

* Fix typos in documentation

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>

* Add links from guide to docs.rs

* Update guide/src/memory.md

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2021-08-21 09:23:10 +01:00
Gregory Szorc 0cf7776a4d string: implement API to access raw string data
With the recent implementation of non-limited unicode APIs, we're
able to query Python's low-level state to access the raw bytes that
Python is using to store string objects.

This commit implements a safe Rust API for obtaining a view into
Python's internals and representing the raw bytes Python is using
to store strings.

Not only do we allow accessing what Python has stored internally,
but we also support coercing this data to a `Cow<str>`.

Closes #1776.
2021-08-19 18:24:17 -07:00
mejrs e753d77e4a deduplicate documentation 2021-08-19 22:55:39 +02:00
Christian Heimes d74c3f6cd0 Use 'crate::' to refer to pyo3
Fixes: #1811
Signed-off-by: Christian Heimes <christian@python.org>
2021-08-18 12:54:27 +02:00
Péter Leéh f72b2c8f09
Py_CompileString decref (#1810)
* update changelog

* fix memory leak in PyModule::from_code

* add PR link to changelog

* Add Py_DECREF also when PyImport_ExecCodeModuleEx fails

* Remove duplicated calls, simplify logic

Co-authored-by: messense <messense@icloud.com>

Co-authored-by: messense <messense@icloud.com>
2021-08-18 10:50:57 +00:00
Peter Schafhalter cf39181efd Fix memory leak when calling Python from Rust 2021-08-17 14:21:56 -07:00
Aviram Hassan c6255e6734 - `PyList`, `PyTuple` and `PySequence`'s `get_item` now accepts only `usize` indices instead of `isize`.
- `PyList` and `PyTuple`'s `get_item` now always uses the safe API. See `get_item_unchecked` for retrieving index without checks.
2021-08-17 15:01:22 +01:00
David Hewitt 7a92e2699b
Merge pull request #1804 from davidhewitt/deprecate_split_from
pytuple: deprecate split_from
2021-08-17 15:00:21 +01:00
David Hewitt 37d39aa83a
Merge pull request #1751 from davidhewitt/pyany-py
pyany: add PyAny::py()
2021-08-17 14:10:16 +01:00
David Hewitt 7fd70d103e pytuple: deprecate split_from 2021-08-17 13:57:27 +01:00
Georg Brandl 79d9741906 Add PyList::slice and fix index types of PyTuple::slice, PyList::insert and PyList::set_item.
NB: the behavior on out-of-range indices hasn't changed;
it was merely wrongly documented before.

See #1667
2021-08-17 09:45:06 +02: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
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 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 0bfd50d314 ffi: cleanup pystate 2021-08-13 08:26:38 +01:00
David Hewitt ebada76ae3 pyany: add PyAny::py() 2021-08-13 08:00:13 +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
mejrs f55a9c8ab0 tests: switch to python_with_gil 2021-08-12 02:47:41 +02: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 290ded4d4e guide: don't bother doctesting async guide 2021-08-10 07:56:36 +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 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
Gregory Szorc 495ac98eb3 ffi: fix Py_DecodeLocale() signature
Closes #1766.
2021-08-08 10:38:00 -07:00
Gregory Szorc 42eb8f26ae ffi: define _Py_PackageContext
This is needed for PyOxidizer.
2021-08-08 10:35:20 -07:00
Gregory Szorc 9a9362909f ffi: make _frozen fields public
PyOxidizer needs to access these fields.
2021-08-08 08:52:08 -07:00
Dean Li 19eb3138f2
clean up ffi from pyframe to pyhash 2021-08-08 19:56:11 +08:00
David Hewitt 7be6bdd97d
Merge pull request #1754 from davidhewitt/sync-lib-readme-examples
docs: sync README and lib.rs examples
2021-08-03 19:23:57 +01:00
David Hewitt b66f539473 docs: sync README and lib.rs examples 2021-08-02 23:03:25 +01:00
David Hewitt 61aaed711d
Merge pull request #1752 from mejrs/with_gil
remove some of python::acquire_gil usage
2021-08-02 19:56:21 +01:00
mejrs 88a5eb643f Fix formatting 2021-08-02 13:09:02 +02:00
mejrs 32f52fca91 remove public python::acquire_gil usage 2021-08-02 01:17:23 +02:00
David Hewitt 3d3dacf2ac guide: rewrite "Building and Distribution" chapter 2021-08-01 16:59:47 +01:00
Georg Brandl 9c33c988ed
Apply suggestions from code review
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2021-08-01 10:11:46 +02:00
Georg Brandl aa48e5be7a some more docstring proofreading 2021-08-01 09:05:30 +02:00
Georg Brandl edd5ca9806 pycell: proofread docstrings 2021-08-01 00:23:59 +02:00