1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

235 Commits

Author SHA1 Message Date
Bartosz Golaszewski
445793aabf bindings: python: examples: new test case
Add a test case for incorrect number of default values in line
requests.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-22 20:14:50 +02:00
Bartosz Golaszewski
f8221bc88b bindings: python: don't call gpiod_LineBulk_init() directly
We can instantiate an object of a type by "calling" it's type structure
using PyObject_CallObject(). It will internally call the constructor so
use it to shrink the code.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-22 15:23:59 +02:00
Bartosz Golaszewski
e03e802add bindings: python: add test cases for default values in line requests
Add two new tests verifying the default_vals argument in line requests
for single and multiple lines.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-20 10:35:35 +02:00
Bartosz Golaszewski
58331f66e7 bindings: python: fix the default_vals argument in line requests
We're assigning the wrong local variable in PyArg_ParseTupleAndKeywords()
which results in not honoring the default_vals argument. Fix the
LineBulk.request() implementation.

Fixes: 96c524c495 ("bindings: implement python bindings")
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-20 10:35:35 +02:00
Bartosz Golaszewski
a610c5077e bindings: python: examples: remove unnecessary import
We no longer need sys in gpiod_tests.py. Remove it.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 19:47:37 +02:00
Bartosz Golaszewski
150d2ca90b bindings: python: move gpiod_ChipIsClosed() to the top of gpiodmodule.c
Unlike the other two helpers that are still declared at the top of the
source file - this routine doesn't need to know any gpiod-specific type
so move it up and remove the prototype.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 18:34:50 +02:00
Bartosz Golaszewski
ce2853562e bindings: python: examples: don't use sys.argv[0] as consumer string
Strings with slashes trigger a warning in the kernel in
fs/proc/generic.c. Use a hardcoded consumer string.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 16:01:06 +02:00
Bartosz Golaszewski
048f0dc26e bindings: python: call gpiod_LineBulk_init() directly
Using the .tp_init callback is more complicated and unnecessary. Call
gpiod_LineBulk_init() directly from gpiod_ListToLineBulk().

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 15:58:27 +02:00
Bartosz Golaszewski
f7e284ee6e bindings: python: provide a helper for calling methods with python args
Wrap the calls to PyObject_GetAttrString() and PyObject_Call() in
a single helper that allows to call a python method and pass it the
args and kwds tuples directly as received from the interpreter.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 15:08:45 +02:00
Bartosz Golaszewski
df5d7b4f42 bindings: python: fix a memory corruption
PyList_GetItem() returns a borrowed reference so we need to increase
the reference count on the returned object before DECREF'ing the list
object itself

This fixes random 'free(): invalid pointer' errors.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 11:25:25 +02:00
Bartosz Golaszewski
ead4d6b46a bindings: python: fix a memory leak in error path
We need to decref the iter object in Line.set_values() if the
conversion from python to C unsigned integer fails.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-07-13 10:11:12 +02:00
Bartosz Golaszewski
3c660a3ec5 bindings: python: fix a warning
On some architectures we get a 'comparison between signed and unsigned
integer' warning when building python bindings. Fix it by changing the
type of the variable used for offset to unsigned int.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-06-07 09:27:54 +02:00
Bartosz Golaszewski
e421f0d776 bindings: python: add a test case for line_event's __repr__
Check if an event's representation string is generated correctly.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-06-07 09:25:48 +02:00
Bartosz Golaszewski
6b3450372e bindings: python: avoid calling object method callbacks directly
Where it's impossible to use PyObject_CallMethod() (e.g. when we want
to pass args and kwargs unchanged to the method being called), use
a combination of PyObject_GetAttrString() and PyObject_Call().

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-06-07 09:25:04 +02:00
Bartosz Golaszewski
18d286843b bindings: python: use PyObject_CallMethod() where possible
Avoid directly calling method callbacks. Use the routine provided by
Python API instead where possible.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-06-07 08:56:05 +02:00
Bartosz Golaszewski
2dfb43260a bindings: python: add support for controlled execution
Provide __enter__ and __exit__ callbacks for Chip so that it can be
used with the 'with' statement.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-16 12:29:17 +02:00
Bartosz Golaszewski
e04d4d45c2 bindings: python: provide better resource control
So far we rely on cpython's reference counting for closing the GPIO
chips and freeing the underlying resources. This is however wrong.
Python doesn't guarantee anything regarding the lifetime of an object.

Provide a method for closing the underlying chip handle and add
relevant checks to all methods using the resources associated with
a GPIO chip, so that we raise an error should the user use a chip
after closing it.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-16 12:01:45 +02:00
Bartosz Golaszewski
7cc5304f33 bindings: python: provide gpiod.version_string()
Implement a python wrapper around gpiod_version_string().

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-16 10:39:11 +02:00
Bartosz Golaszewski
3107d63692 bindings: python: test case for line flags
Add a new test case verifying that line flags work as expected.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 18:35:16 +02:00
Bartosz Golaszewski
16e5a3ce56 bindings: python: provide Line.is_requested()
Add a python wrapper around gpiod_line_is_requested().

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 18:34:52 +02:00
Bartosz Golaszewski
345d408836 bindings: python: improve readability in test cases
Mostly just add new lines for improved readability.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 18:13:36 +02:00
Bartosz Golaszewski
0596dfb0b5 bindings: python: new test cases/examples
Add more test cases/code examples for python bindings.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 14:51:00 +02:00
Bartosz Golaszewski
d463052184 bindings: python: provide Chip.get_all_lines()
Add a method for retrieving all GPIO lines exposed by a chip.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 12:05:28 +02:00
Bartosz Golaszewski
2c8127eba1 bindings: python: improve readability
Order the arguments in a reverse christmas tree shape in
gpiod_Chip_find_lines();

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 11:50:16 +02:00
Bartosz Golaszewski
7a64b84885 bindings: python: code shrink
Provide a helper for converting a list of lines into a LineBulk object
in a single call.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 11:49:16 +02:00
Bartosz Golaszewski
e6d1647a8f bindings: python: implement Chip.find_lines()
Provide a method for looking up multiple lines by name at once.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-15 11:41:52 +02:00
Bartosz Golaszewski
2df328e989 bindings: python: extend tests
Add more test cases/examples, implement some helper functions, tweak
the output messages, mention the need for a dummy chip in the system
in the module's pydoc string.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-14 12:01:13 +02:00
Bartosz Golaszewski
1e47c4d6ea bindings: python: change the return value of gpiod_LineBulk_event_wait()
gpiod_Line_event_wait() works differently than its LineBulk
counterpart. While the latter returns either a list of lines or False,
the former returns True if an event occurred on this line or False
otherwise.

False is not a good counterpart for an actual object, so instead return
None if no events occurred on any of the monitored lines.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-14 12:01:13 +02:00
Bartosz Golaszewski
15a623a7d2 bindings: python: fix a memory leak in gpiod_Line_event_wait()
We don't return the object returned from gpiod_LineBulk_event_wait()
back to the interpreter. We need to collect it before returning True.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-14 12:01:13 +02:00
Bartosz Golaszewski
27fec345ca bindings: python: fix a memory leak in Line.set_value()
We're not collecting the bulk object we're creating in this method. Add
Py_DECREF() where needed.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-14 12:01:13 +02:00
Bartosz Golaszewski
d331321f01 bindings: python: return None if line can't be found by name
For both find_line() variants (Chip's method and global function)
return None if the internal call to the C find_line function fails
with ENOENT. This is not an error so don't raise an exception.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-14 11:49:13 +02:00
Bartosz Golaszewski
f36a27f1cc bindings: python: decref the chip object in gpiod_Module_find_line()
When returning back to the interpreter from gpiod_Module_find_line(),
there's only one chip object reference holder, but we set the refcount
to 2 (as if the chip object existed separately in the interpreter).

Fix it by decreasing the chip's refcount by one before returning the
line object.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-12 14:51:28 +02:00
Bartosz Golaszewski
039b301b17 bindings: python: release the GIL during blocking I/O operations
We should release the python global interpreter lock when performing
blocking operations and reacquire it again when we're done. Use the
relevant APIs provided by libpython.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-04-22 23:16:40 +02:00
Bartosz Golaszewski
696de1b973 build: fix cross-compilation of python bindings
AX_PYTHON_DEVEL macro from the autoconf-archive collection doesn't work
when we're trying to cross-compile python bindings in environments such
as buildroot or yocto. It can't properly detect the location of python
headers.

Let's use AM_PATH_PYTHON to detect the python3 interpreter and then
either take the PYTHON_CPPFLAGS and PYTHON_LIBS variables from the
environment (so that cross-compiling build systems can pass their
custom locations to autotools) or try to detect them ourselves using
the python3-config tool.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-04-20 14:10:09 +02:00
Bartosz Golaszewski
96c524c495 bindings: implement python bindings
Create a C extension module wrapping the functionality of libgpiod
in a set of Python classes.

Add code examples including simplified reimplementations of gpio-tools
in Python.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-04-17 10:19:34 +02:00