Commit Graph

242 Commits

Author SHA1 Message Date
Richard W.M. Jones
8dc3b9fb23 New mailing list email address
(cherry picked from commit 0b8b1e4b2d)
2024-07-09 14:42:43 +01:00
Richard W.M. Jones
ef18637581 python: Fix exception name in example
Fixes: https://github.com/libguestfs/libguestfs/issues/138
(cherry picked from commit 1d760b0d8f)
(cherry picked from commit b56b5ca46c)
2024-07-09 14:08:41 +01:00
Richard W.M. Jones
2cfb5420fe python: Avoid leaking py_array and py_args in event callbacks
See also:

https://listman.redhat.com/archives/libguestfs/2023-February/030730.html
https://listman.redhat.com/archives/libguestfs/2023-February/030745.html
https://listman.redhat.com/archives/libguestfs/2023-February/030746.html

Fixes: commit 6ef5837e2d
Thanks: Laszlo Ersek, Eric Blake
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
(cherry picked from commit afa4d64fca)
2023-02-21 20:42:20 +00:00
Richard W.M. Jones
1b2e354359 Revert "python: fix call of Python handlers of events"
This reverts one part of commit 85235aec83 related to reference
counts.  Py_BuildValue always increments the reference count (when it
succeeds) and I could not reproduce the Python 3 problem that was
described there.

Reviewed-by: Laszlo Ersek <lersek@redhat.com>
(cherry picked from commit e07304de86)
2023-02-21 20:42:14 +00:00
Richard W.M. Jones
8578c62dc9 python: Use bytes instead of str for event callback buffer
The event callback gets a buffer parameter which is usually something
like a log message.  However as it comes from C it is not necessarily
well-formed (eg) UTF-8 but could contain any old sequence of bytes.

In the test case provided by the reporter, we failed to encode the
buffer as 'str' with this error:

  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 137: unexpected end of data

Use 'bytes' instead.  Strictly speaking this changes the type
signature of the callbacks, but our existing Python tests which just
print the buffer using '%s' don't fail and in any case we don't
guarantee the stability of non-C APIs.

Reported-by: Yonatan Shtarkman
See: https://listman.redhat.com/archives/libguestfs/2023-February/030653.html
(cherry picked from commit bbf396fc55)
2023-02-21 20:39:59 +00:00
Richard W.M. Jones
86df8c9a1c python: Avoid crash if callback parameters cannot be built
In the case that building the parameters to the Python event callback
fails, args was returned as NULL.  We immediately tried to call
Py_INCREF on this which crashed.  Returning NULL means the Python
function threw an exception, so print the exception and return (there
is no way to return an error here - the event is lost).

Reported-by: Yonatan Shtarkman
See: https://listman.redhat.com/archives/libguestfs/2023-February/030653.html
(cherry picked from commit 6ef5837e2d)
2023-02-21 20:39:53 +00:00
Richard W.M. Jones
e2c7bddf10 Update copyright dates for 2023
Run this command across the source:

  perl -pi.bak -e 's/(20[012][0-9])-20[12][012]/$1-2023/g' `git ls-files`

and remove changes to po{,-docs}/*.po{,t} (these will be regenerated
later when we run 'make dist').
2023-02-07 10:50:48 +00:00
Richard W.M. Jones
fa305b709a python: Fix code style ("missing whitespace after keyword") 2022-11-28 10:32:21 +00:00
Richard W.M. Jones
ceb034c92c python, java: Avoid bogus -fanalyzer warnings
This is essentially the same as the previous OCaml commit.  It does
not fix a real bug.
2021-09-07 16:50:38 +01:00
Richard W.M. Jones
2883fc2acc python: Relicense setup.py to LGPLv2+ (originally GPLv2+)
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1951560#c3
2021-04-23 10:49:55 +01:00
Richard W.M. Jones
245d1a3d7c python: Ignore code style error E128. 2021-03-22 13:19:19 +00:00
Richard W.M. Jones
75abec1f70 include: Move lib/guestfs.h to include/guestfs.h
This brings libguestfs into line with other projects which have a
separate include/ directory for the public header.

It's also the case that <guestfs.h> has never particularly belonged in
the lib/ subdirectory.  Some tools add -Ilib/ but they only need
<guestfs.h> and not any other headers from that directory, and
separating out the public header allows us to clean those up.  This is
certainly the case for examples, and some language bindings and some
tests.

In future I'm hopeful we can use this as the basis to tease out other
dependencies, as a prelude to separating them out from the repo.
2020-09-21 18:38:28 +01:00
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
cad3ea9e74 python: add a pycodestyle test
Look for pycodestyle, and use it to check all the Python sources (tests
and auxiliary scripts included) of the Python bindings.
2020-01-14 16:21:22 +01:00
Pino Toscano
da4c7f9112 python: skip line length check in license line
Properly mark the long line with the license classifier in setup.py:
the classifier is long on its own, and changing the code to stay within
79 characters would result in worse code.
2020-01-14 16:21:22 +01:00
Pino Toscano
92759efbf5 python: improve errors in inspect_vm example
When validating user input, print an error message and exit, instead of
either asserting or raising a non-existing exception.
2020-01-14 16:21:22 +01:00
Pino Toscano
7d0f8e1b22 python: tests: remove unused imports
No functional changes.
2020-01-14 16:21:22 +01:00
Pino Toscano
4719cca7f5 python: tests: improve variable naming
Use a slightly more descriptive name, as also pointed out by
pycodestyle.

No functional changes.
2020-01-14 16:21:22 +01:00
Pino Toscano
6d0e9531c8 python: tests: catch specific exception
When trying to import libvirt, catch the specific exception that is
raised when an importing fails.
2020-01-14 16:21:22 +01:00
Pino Toscano
0703ec9c8c python: PEP 8: adapt whitespaces in lines
Fix continuation indentation, and whitespaces around operators.

This is just code reformatting, with no behaviour changes; no content
changed beside whitespaces, so "git diff -w" gives an empty diff.
2020-01-14 16:21:22 +01:00
Pino Toscano
df3e693b1b python: PEP 8: adapt empty lines
Add or remove empty lines to match the needed ones around
blocks/functions/etc.

Adapt the generation of guestfs.py to emit the separating empty line
before adding a new function/alias, rather than after it.

This is just formatting, no behaviour changes.
2020-01-14 16:21:22 +01: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
Pino Toscano
cb0edd4354 build: use split stringlist functions from common/utils
The OCaml and Python bindings directly use the utils.c source in
common/utils, mostly for guestfs_int_free_string_list.  That source
contained also functions using gnulib functions, however without
linking to gnulib.  When building with default build flags (e.g. without
as-needed mode), the gnulib symbols cannot be resolved, leading to
unusable OCaml and Python libraries.

As solution, update the common submodule to get the split of the split
of the stringlist functions in common/utils, and adapt the OCaml and
Python bindings:
- both now use stringlists-utils.c instead of utils.c
- fix the Python distutils setup to include only the sources really
  needed
2020-01-09 14:57:38 +01:00
Pino Toscano
e789a070de python: remove also __pycache__ directories on clean 2019-12-19 15:34:37 +01:00
Pino Toscano
323909d92e build: stop shipping files generated by configure
They will be generated by configure.
2019-12-12 18:01:32 +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
9f39e9be24 python: silence usage of add_cdrom in test
One test explicitly tests add_cdrom, so silence the deprecation warning
only for that function.
2019-04-24 12:49:48 +02:00
Pino Toscano
b6a7157751 python: modernize inspect_vm example
Since we already assume Python >= 2.7, modernize this example to make it
work also on Python 3:
- use print() as function
- sort the mount points using a key for sorted(), instead of a
  comparison function
- remove extra newline escape
- reident two lines according to the PEP 8 style
2019-04-24 11:59:36 +02:00
Hiroyuki Katsura
7772cfc771 Add missing python bindings tests
Signed-off-by: Hiroyuki Katsura <hiroyuki.katsura.0513@gmail.com>
2019-04-03 17:28:58 +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
d0e5a819e8 python: Fix missing & additional backslashes which broke python sdist.
Fixes commit e6c89f9631.
2018-02-09 16:00:00 +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
Richard W.M. Jones
f3330e144b python: tests: Allow some tests to be skipped.
Currently test820RHBZ912499.py fails with libvirt because libvirt
still doesn't work around qemu locking properly.  Allow this test to
be skipped on a temporary basis using SKIP_TEST820RHBZ912499_PY=1
2017-10-18 18:30:05 +01:00
Richard W.M. Jones
f30b2065a2 gobject: Add outline guestfs-gobject(3) manual page.
Since we removed gtk doc, we might as well replace it with a
manual page explaining the basics of how to run gjs.
2017-07-10 17:03:19 +01:00
Richard W.M. Jones
e6c89f9631 utils: Rename ‘guestfs-internal-frontend.h’ to ‘guestfs-utils.h’.
The reason it's not just ‘utils.h’ is because Pino is worried that we
might pick up /usr/include/utils.h from a rogue library.
2017-07-10 17:01:59 +01:00
Richard W.M. Jones
b2469a6d96 common/utils: Refactor stdlib, gnulib and libxml2 cleanup functions.
This refactoring change just moves the cleanup functions around in the
common/utils directory.

libxml2 cleanups are moved to a separate object file, so that we can
still link to libutils even if the main program is not using libxml2
anywhere.  Similarly gnulib cleanups.

cleanup.c is renamed to cleanups.c.

A new header file cleanups.h is introduced which will replace
guestfs-internal-frontend-cleanups.h (fully replaced in a later commit).
2017-07-10 17:01:59 +01:00
Pino Toscano
713243b3cb python: PEP 8: remove extra spaces in example lines
Remove (before opening round bracket) whitespaces in the documentation
of the Python binding, according to the PEP 8 specification.

This is just code reformatting, with no behaviour changes; no content
changed beside whitespaces, so "git diff -w" gives an empty diff.
2017-06-21 13:46:58 +02:00
Pino Toscano
00bf11cba8 python: PEP 8: add empty line
Small update for commit 45e046dac9.

This is just formatting, no behaviour changes.
2017-05-15 14:31:56 +02: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
Richard W.M. Jones
c4106d34cb python: Fix python sdist.
Fixes commit d45986d21b.
2017-02-28 20:56:38 +00:00