Commit Graph

18 Commits

Author SHA1 Message Date
Richard W.M. Jones
75def0ef18 python: Remove deprecated use of PyEval_ThreadsInitialized.
The correct/modern way to do this is:

https://docs.python.org/3/c-api/init.html#releasing-the-gil-from-extension-code

PyEval_ThreadsInitialized was deprecated in 3.9 and will be removed
completely in 3.11, so we shouldn't use that function.
2020-07-06 12:40:33 +01:00
Richard W.M. Jones
403d17a876 python: Drop support for Python 2.
Python 2 reached end of life on 2020-01-01:
https://python3statement.org/
https://pythonclock.org/

The minimum version required is now Python 3.4 (since that is the
version in Debian oldoldstable), but 3.6 is the minimum version that
I actually test.
2020-07-06 12:40:33 +01:00
Richard W.M. Jones
0e17236d7d Update copyright dates to 2020. 2020-03-06 19:32:32 +00:00
Richard W.M. Jones
4e90febbaa RHEL 7: python: Check for _Py_IsFinalizing.
This symbol is not present in Python 2.7 or 3.6.  It's not really
necessary to call this, it just avoids a crash in certain corner cases
when the interpreter is shutting down.  So make the call conditional
on the function existing.

Fixes commit e6f9e0b0f6.
2020-03-06 18:59:52 +00:00
Pino Toscano
a68a0298b5 python: remove compile time check for PyString_AsString
The PyString API is specific to Python < 3, replaced by PyBytes and
PyUnicode in Python 3+; hence, drop the compile time check, and use the
right API depending on the Python version.
2020-01-09 17:45:26 +01:00
Sam Eiderman
e6f9e0b0f6 python: Don't call Python functions if Python interpreter has already gone.
If you've registered a callback in Python and the handle is implicitly
closed when the Python interpreter exits, then it can be that the
following happens:

 - Python interpreter is finalized.
 - guestfs_close is called
 - callback is invoked (eg. close event or to display a debug message)
 - Python code runs from the callback wrapper

This cause a segfault on shutdown.  Catch this case and stop the
callback from running (we lose the event but given the above sequence
of events there's not much we can do about it).

See:
https://bugzilla.redhat.com/show_bug.cgi?id=1773520#c7
2019-11-18 15:35:17 +00:00
Sam Eiderman
7757eb8d0c Python: Fix GIL usage in guestfs_int_py_event_callback_wrapper (RHBZ#1773520)
All Py_* functions should be protected by the GIL.
Otherwise internal python data structures can get corrupted.

Move PyGILState_Ensure to the beginning of the block and
PyGILState_Release to the bottom.

Signed-off-by: Sam Eiderman <sameid@google.com>
2019-11-18 15:30:01 +00:00
Richard W.M. Jones
451e6a7b97 python: Include <Python.h> in one place.
Previously to work around some problems in Python 2 header files we
had to include <Python.h> before any other config file.

For Python 3 which is all we really care about now this is no longer
needed.  We can move the include from three files into the local
"actions.h" file, bringing all the Python definitions and workarounds
into a single place.
2019-09-02 09:00:18 +01:00
Pino Toscano
85235aec83 python: fix call of Python handlers of events
Make sure to reference the arguments, to make sure they are kept alive
during the function call; this is visible when setting an event handler
for the CLOSE event, and testing it with Python 3.

This does not seem to create a memory leak e.g. with Python 2.

Also, switch away from the quasi-internal PyEval_CallObject to the
public PyObject_CallObject, which takes care of doing safety checks.
2019-01-22 12:43:11 +01:00
Richard W.M. Jones
05d4fcb64d Update copyright dates for 2019.
This command run over the source:

perl -pi.bak -e 's/(20[01][0-9])-2018/$1-2019/g' `git ls-files`
2019-01-08 11:58:30 +00:00
Richard W.M. Jones
212762c593 Update copyright dates for 2018.
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2017/$1-2018/g' `git ls-files`
2018-01-04 15:30:10 +00:00
Matteo Cafasso
401c445636 python: check return value of Python object functions (RHBZ#1406906)
Verify the returned values of Python Object constructor functions
are not NULL before adding them to a collection.

This is particularly relevant when constructing Unicode strings in
Python 3 as they will return NULL if non UTF-8 characters are present.

Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
2017-05-15 14:20:31 +02:00
Pino Toscano
3c0b7710c1 python: improve few exceptions thrown on error
Make use of functions and types that fit more, and that do the same job:
- use PyErr_NoMemory() on malloc failure
- use PyErr_SetFromErrno when setting an exception from an errno
- throw TypeError if not getting a list when required
2017-05-11 14:35:42 +02:00
Pino Toscano
f3f99a09ea python: use right func when PyString_FromStringAndSize is not there
Fixes commit 9d25b4e564.

Thanks to: Matteo Cafasso
2017-05-09 17:47:32 +02:00
Pino Toscano
9d25b4e564 python: add simple wrappers for PyObject<->string functions
The current need for #ifdef's based on the presence of
PyString_FromString makes both the OCaml code of the generator, and the
generated C code a mess to read.

Hence, add three simple wrappers to make both the OCaml, and C code more
readable, and easier to tweak in the future.

This should be just refactoring, with no actual behaviour changes.

Thanks to: Matteo Cafasso
2017-05-09 16:45:31 +02:00
Pino Toscano
b890fd2900 python: do not try to malloc 0 elements in get_all_event_callbacks
In case there are no event handlers registered with the handle,
get_all_event_callbacks will count 0 elements, trying to malloc a buffer
of that size.  POSIX says that this can result in either a null pointer,
or an unusable pointer.

Short-circuit get_all_event_callbacks to allocate nothing when there are
no events, making sure to use its results only when there were events.
2017-03-06 09:02:02 +01:00
Pino Toscano
55bf7de97c Update copyright dates for 2017
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2016/$1-2017/g' `git ls-files`

(Thanks Rich for the perl snippet, as used in past years.)
2017-01-03 16:48:21 +01:00
Richard W.M. Jones
63db9ae81f python: Split up large Python extension into smaller C files.
Reduces build time in this directory from 11s to 3s.
2016-09-02 23:14:08 +01:00