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

989 Commits

Author SHA1 Message Date
Philipp Hahn
89c453db00 generator: Use dict.item() to walk keys and values
which saves a lookup of each key.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
659d0b4dfd generator: Check contained in hash
directly instead of explicitly requesting only the keys as a list and
converting that list to another list.
Checking directly for an element to be contained in a hash is much more
efficient as this is done using hashing O(1) instead of walking the list
in half on average O(n).

Signed-off-by: Philipp Hahn <hahn@univention.de>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-09-01 13:26:01 +00:00
Philipp Hahn
a0798b9bbc generator: Initialize function_classes directly
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
94b30b7fb7 generator: Remove global declarations
for variables with are not assigned.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
f4aefad212 generator: Use string concatenation
:%s,\(\w\+\)\s*=\s*\1\s*+,\1 +=,

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
9e325358de generator: Use increment assignment
:%s,\(\w\+\)\s*=\s*\1\s*+,\1 +=,

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
03b558b32e generator: Use enumerate()
instead of manual iteration counting.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
4b798e3405 generator: Simplify string concatentaion
by using ''.join() instead of concatenating string fragments in a loop,
which is slower as it required re-hashing the string multiple times.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
879dae7483 generator: Use more string formatting
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
928f5a22e6 generator: Merge now identical if-elif-else cases
Commit ca394b9f "generator: Fix parent type" fixed the case for creating
`virStorage*` instances, which require a reference to `virConnect`, so
the special handling for `._conn` is no longer needed.

Commit ee5c856a "Remove legacy libvirtError arguments" removed the
different arguments, so all cases are the same now.

Signed-off-by: Philipp Hahn <hahn@univention.de>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-09-01 13:26:01 +00:00
Philipp Hahn
2679b06d9c generator: Fix return type on failure
to return a negative value instead of None for consistency.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
241e89b713 generator: Remove useless sort key
tuples are sorted by first component anyway.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
0132c6d2a9 generator: Remove skipped_modules
Unused.

Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
Philipp Hahn
3b85ec54c9 generator: Remove dead variable assignments
Signed-off-by: Philipp Hahn <hahn@univention.de>
2020-09-01 13:26:01 +00:00
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