1
0
mirror of https://gitlab.com/libvirt/libvirt-python.git synced 2025-12-13 00:23:49 +03:00
Commit Graph

1075 Commits

Author SHA1 Message Date
Philipp Hahn
8b8b394555 generator: Use raw-string for regular expression
"\(" is not a valid escape sequence for a Python string, but currently
is passed on unmodified. This might breaks in the future when new escape
sequences are introduced.

> generator.py:1001:7: W605 invalid escape sequence '\('
> generator.py:1001:18: W605 invalid escape sequence '\)'

Use raw python string instead.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
bc486beb0d generator: Convert to 'not in' and 'is not'
as recommended by pep8

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
443403e7fe generator: Change type of quiet to bool
Use `bool` instead of `int`.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:00 +00:00
Philipp Hahn
ab1147f524 generator: Simplify exception handling
sys.exc_info() returns a 3-tuple (type, value, traceback), where `value`
is the instance captured by `except type as value`.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:00 +00:00
Philipp Hahn
38a1b70524 generator: Cleanup imports
Move imports to top
Remove unused import string
Remove duplicate import os

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:00 +00:00
Philipp Hahn
e16eab444f generator: Do not use bare except
as it also catches SystemExit, InterruptedError, SyntaxError and such.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:00 +00:00
Jiri Denemark
a1f2c432a8 Post-release version bump to 6.8.0
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
2020-09-01 09:10:48 +02:00
Philipp Hahn
8c800b0adf Revert "libvirtaio: Drop object(*args, **kwargs)"
This reverts commit f4be03b330.

While object.__init__() does not expect any additional arguments, this
construct is required for Pythons multiple inheritance implementation.
The original author Wojtek Porczyk <woju@invisiblethingslab.com>
explained is this way:

> I'm sorry I didn't notice this earlier, but the commit f4be03b3 dated
> 2020-04-20 [0] is wrong. The super().__init__(*args, **kwargs) in
> Callback.__init__ was there on purpose, because of how Python's inheritance in
> new-style classes works.
>
> Let me explain this a bit, because it is not obvious.
>
> Suppose you had diamond inheritance like this:
>
>     class A(object): pass
>     class B(A): pass
>     class C(A): pass
>     class D(B,C): pass
>
> And those classes needed a common function with varying arguments:
>
>     class A(object):
>         def spam(self, a): print(f'A: {a}')
>     class B(A):
>         def spam(self, b): print(f'B: {b}')
>     class C(A):
>         def spam(self, c): print(f'C: {c}')
>     class D(B,C):
>         def spam(self, d): print(f'D: {d}')
>
> The way to call all parent's functions exactly once (as per MRO) and accept
> all arguments and also forbid unknown arguments is to accept **kwargs
> everywhere and pass them to super().spam():
>
>     class A:
>         def spam(self, a):
>             print(f'A: {a}')
>     class B(A):
>         def spam(self, b, **kwargs):
>             print(f'B: {b}')
>             super().spam(**kwargs)
>     class C(A):
>         def spam(self, c, **kwargs):
>             print(f'C: {c}')
>             super().spam(**kwargs)
>     class D(B, C):
>         def spam(self, d, **kwargs):
>             print(f'D: {d}')
>             super().spam(**kwargs)
>
> Let's run this:
>
>     >>> B().spam(a=1, b=2)
>     B: 2
>     A: 1
>     >>> D().spam(a=1, b=2, c=3, d=4)
>     D: 4
>     B: 2
>     C: 3
>     A: 1
>
> You may notice that super() in B.spam refers to two different classes, either
> A or C, depending on inheritance order in yet undefined classes (as of B's
> definition).
>
> That's why the conclusion that super() in Callback.__init__ refers to object
> is wrong. In this example, spam=__init__, A=object, B=Callback and C and D are
> not yet written, but theoretically possible classes that could be written by
> someone else. Why would they be needed, I don't know, but if someone writes
> them, s/he would be out of options to invent new arguments to C.__init__.
>
> Note that super().__init__(*args, **kwargs) when super() refers to object
> isn't harmful, and just ensures that args and kwargs are empty (i.e. no
> unknown arguments were passed). In fact, this is exactly why object.__init__()
> takes no arguments since Python 2.6 [1][2], as you correctly point out in the
> commit message.
>
> I don't think this breaks anything (I very much doubt anyone would need to
> write code that would trigger this), nevertheless, as the commit is both
> pointless and wrong, and as the original author of libvirtaio I'd like to ask
> for this commit to be reverted. If this breaks some static analysis tool,
> could you just suppress it for this particular line?
>
>
> [0] f4be03b330
> [1] https://bugs.python.org/issue1683368
> [2] https://docs.python.org/3/whatsnew/2.6.html#porting-to-python-2-6
>     (fourth point)
>

Signed-off-by: Philipp Hahn <hahn@univention.de>
v6.7.0
2020-08-28 18:42:48 +02:00
Philipp Hahn
d8d55b17e2 connect: Just clear all event handlers
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
bd1a2c9fff domain: Fix None comparison
None should be compared with "is None" instead of "== None", as the
later would invoke a "__cmp__()" method.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
fd7087fb72 stream: Return None from callback
nobody evaluates the return value.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
547965ecd9 stream: Convert type() to isinstance()
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
d144e70957 stream: no type change
static typing forbids re-declaring a variable with different types.
Rename the variable.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
109b257fe5 stream: Simplify boolean condition
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
06222739f7 stream: Fix exception traceback handling
sys.exc_info() returns a 3-tuple (type, value, traceback). Raising just
`value` again looses the traceback information as this creates a new
exception.

Just use `raise` which re-raises the previous exception including the
original traceback.

FYI: There is a subtile difference between Python 2 and Python 3:

> try:
>     raise ValueError()
> except ValueError:
>     try:
>         raise TypeError()
>     except TypeError:
>         pass
>     raise

With Python 3 the exception environment is dropped after the exception
has been handled - as such Python 3 re-raises the outer ValueError.

With Python 2 the last (inner) exception is raised: TypeError

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
9074b50259 stream: Do not use bare except
as it also catches SystemExit, InterruptedError, SyntaxError and such.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
5777008197 override: Convert to list comprehension
:%s/retlist = list()\n\s*\(for \w\+ in ret\):\n\s*retlist.append(\(.*\))\n\n\s*return retlist/return [\2 \1]/

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
d49e850807 override: Catch type error
handler() should either return bytes or -2 or -3.
Explicitly raise ValueError or TypeError to silence mypy.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
051a00c212 override: no type change
static typing forbids re-declaring a variable with different types.
Rename the variable.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
abbd47f4ea override: Add manual PEP 484 type annotations
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Philipp Hahn
67af8b910b override: Simplify exception handling
sys.exc_info() returns a 3-tuple (type, value, traceback), where `value`
is the instance captured by `except type as value`.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-18 09:48:25 +00:00
Vincent Vanlaer
ec76ba3507 Include libvirt-qemu-override.py in sdist
libvirt-qemu-override.py was introduced in e3da8f17 but never added to
MANIFEST.in. It was therefore not contained in the official releases on
libvirt.org.

Signed-off-by: Vincent Vanlaer <vincent.vanlaer@skynet.be>
2020-08-18 00:23:56 +02:00
Philipp Hahn
026a47928a libvirtaio: Add manual PEP 484 type annotations
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-07 18:05:53 +02:00
Philipp Hahn
6b97281730 libvirtaio: assert callback type
self.callbacks contains a mix of FDCallback and TimeoutCallback, while
the update code does not explicitly check for.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
12d631d11a libvirtaio: Fix return type
libvirtaio.py:364: error: "virEventInvokeFreeCallback" does not return a value

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
f4be03b330 libvirtaio: Drop object(*args, **kwargs)
object.__init__() does not expect those parameters.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
429973c836 libvirtaio: Cleanup imports
Move imports to top

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
bd238713ef sanitytest: no type change
static typing forbids re-declaring a variable with different types.
Rename the variable.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
22f2ba37ae sanitytest: Use str.startswith() instead of str[0]
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
b6ad44257e sanitytest: Use set for tracking used functions
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
ddf73e40a9 sanitytest: Use 3-tuple for finalklassmap
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
809b963134 sanitytest: Use 3-tuple for basicklassmap
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
94c24929e4 sanitytest: Add PEP 484 type annotations
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
fd8b7733dc sanitytest: Drop Python 2 compatibility
Python 3 only has int, remove the Python 2 long type

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
ff9575a727 sanitytest: Drop else:pass
useless

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
6279dbefc2 sanitytest: Do not re-declare set
is a built-in python type

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
39ffa7f838 sanitytest: Convert type() to isinstance()
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
8a81fc6f2b sanitytest: Skip type annotations
Teach sanitytest to ignore typing imports

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
b5f7beb931 sanitytest: Remove unused import
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
ee5c856af7 Remove legacy libvirtError arguments
The fields have been deprecated in C with
git:f60dc0bc09f09c6817d6706a9edb1579a3e2b2b8

They are only passed to the libvirtError constructor, but not stored for
later or used anywhere else.

sed -ri '/raise libvirtError/s/, \w+=self(\._dom)?//' *.py

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
0ca8dc6340 Normalize white space
indent by 4 spaces
one spaces around assignments

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
7c93891420 examples/event-test: Fix remove return type
The remove function are supposed to return 0 on success and -1 on failure.
<https://libvirt.org/html/libvirt-libvirt-event.html#virEventRemoveTimeoutFunc>

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-06 08:50:37 +02:00
Philipp Hahn
74a78fa42c examples: Fix white space
indent by 4 spaces
one spaces around assignments

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
b801ff31fa examples: Replace sys.exit() with exit()
No need to import sys.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
f496dc55ee examples: Cleanup imports
Break import into multiple lines as recommended by PEP-8

Move imports to top

Remove unused imports

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
4e22f4de6f examples: Do not use bare except
as it also catches SystemExit, InterruptedError, SyntaxError and such.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
06aba185a8 examples: Convert to ArgumentParser
Replace getopt() and hand-rolled-parser with argparse.ArgumentParser.

Fix wrong header comments copy-pasted from domstart.py

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
9cf539a2a8 examples: Add/fix PEP 484 type annotation
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
5434ed53ff examples: Add missing return values
examples/dhcpleases.py:45: error: Missing return statement

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00
Philipp Hahn
c588ba982e examples/nodestat: Fix None comparison
"is" compares for "points to the same object", which for strings is the
same as comparing the byte sequence itself as Python hashes each strings
to only stores a unique copy of each string.

> examples/nodestats.py:86:43: F632 use ==/!= to compare constant literals (str, bytes, int, float, tuple)
> examples/nodestats.py:91:12: F632 use ==/!= to compare constant literals (str, bytes, int, float, tuple)
> examples/nodestats.py:94:40: F632 use ==/!= to compare constant literals (str, bytes, int, float, tuple)

Use "==" and "!=" for string comparsion.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-08-05 07:43:02 +00:00