]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/generic_linux/sysfs_pin.py
tests for dig in, an in, an out, dig out all passing
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / sysfs_pin.py
index c5b56e69869603a83308da2255d113646688d674..86c9a5fd69cda0d07100bfd0afce1b8a08467585 100644 (file)
@@ -1,3 +1,6 @@
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
 """
 Much code from https://github.com/vsergeev/python-periphery/blob/master/periphery/gpio.py
 Copyright (c) 2015-2019 vsergeev / Ivan (Vanya) A. Sergeev
 """
 Much code from https://github.com/vsergeev/python-periphery/blob/master/periphery/gpio.py
 Copyright (c) 2015-2019 vsergeev / Ivan (Vanya) A. Sergeev
@@ -8,6 +11,7 @@ import os
 import errno
 import time
 
 import errno
 import time
 
+
 # pylint: disable=unnecessary-pass
 class GPIOError(IOError):
     """Base class for GPIO errors."""
 # pylint: disable=unnecessary-pass
 class GPIOError(IOError):
     """Base class for GPIO errors."""
@@ -132,10 +136,10 @@ class Pin:
         if not os.path.isdir(gpio_path):
             # Export the line
             try:
         if not os.path.isdir(gpio_path):
             # Export the line
             try:
-                with open("/sys/class/gpio/export", "w") as f_export:
+                with open("/sys/class/gpio/export", "w", encoding="utf-8") as f_export:
                     f_export.write("{:d}\n".format(self.id))
             except IOError as e:
                     f_export.write("{:d}\n".format(self.id))
             except IOError as e:
-                raise GPIOError(e.errno, "Exporting GPIO: " + e.strerror)
+                raise GPIOError(e.errno, "Exporting GPIO: " + e.strerror) from IOError
 
             # Loop until GPIO is exported
             exported = False
 
             # Loop until GPIO is exported
             exported = False
@@ -155,7 +159,9 @@ class Pin:
             # permission rule application after export
             for i in range(self.GPIO_OPEN_RETRIES):
                 try:
             # permission rule application after export
             for i in range(self.GPIO_OPEN_RETRIES):
                 try:
-                    with open(os.path.join(gpio_path, "direction"), "w") as f_direction:
+                    with open(
+                        os.path.join(gpio_path, "direction"), "w", encoding="utf-8"
+                    ) as f_direction:
                         f_direction.write(direction.lower() + "\n")
                     break
                 except IOError as e:
                         f_direction.write(direction.lower() + "\n")
                     break
                 except IOError as e:
@@ -164,22 +170,26 @@ class Pin:
                     ):
                         raise GPIOError(
                             e.errno, "Setting GPIO direction: " + e.strerror
                     ):
                         raise GPIOError(
                             e.errno, "Setting GPIO direction: " + e.strerror
-                        )
+                        ) from IOError
 
                 time.sleep(self.GPIO_OPEN_DELAY)
         else:
             # Write direction
             try:
 
                 time.sleep(self.GPIO_OPEN_DELAY)
         else:
             # Write direction
             try:
-                with open(os.path.join(gpio_path, "direction"), "w") as f_direction:
+                with open(
+                    os.path.join(gpio_path, "direction"), "w", encoding="utf-8"
+                ) as f_direction:
                     f_direction.write(direction.lower() + "\n")
             except IOError as e:
                     f_direction.write(direction.lower() + "\n")
             except IOError as e:
-                raise GPIOError(e.errno, "Setting GPIO direction: " + e.strerror)
+                raise GPIOError(
+                    e.errno, "Setting GPIO direction: " + e.strerror
+                ) from IOError
 
         # Open value
         try:
             self._fd = os.open(os.path.join(gpio_path, "value"), os.O_RDWR)
         except OSError as e:
 
         # Open value
         try:
             self._fd = os.open(os.path.join(gpio_path, "value"), os.O_RDWR)
         except OSError as e:
-            raise GPIOError(e.errno, "Opening GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Opening GPIO: " + e.strerror) from OSError
 
         self._path = gpio_path
 
 
         self._path = gpio_path
 
@@ -192,7 +202,7 @@ class Pin:
         try:
             os.close(self._fd)
         except OSError as e:
         try:
             os.close(self._fd)
         except OSError as e:
-            raise GPIOError(e.errno, "Closing GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Closing GPIO: " + e.strerror) from OSError
 
         self._fd = None
 
 
         self._fd = None
 
@@ -202,20 +212,20 @@ class Pin:
             os.write(unexport_fd, "{:d}\n".format(self.id).encode())
             os.close(unexport_fd)
         except OSError as e:
             os.write(unexport_fd, "{:d}\n".format(self.id).encode())
             os.close(unexport_fd)
         except OSError as e:
-            raise GPIOError(e.errno, "Unexporting GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Unexporting GPIO: " + e.strerror) from OSError
 
     def _read(self):
         # Read value
         try:
             buf = os.read(self._fd, 2)
         except OSError as e:
 
     def _read(self):
         # Read value
         try:
             buf = os.read(self._fd, 2)
         except OSError as e:
-            raise GPIOError(e.errno, "Reading GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Reading GPIO: " + e.strerror) from OSError
 
         # Rewind
         try:
             os.lseek(self._fd, 0, os.SEEK_SET)
         except OSError as e:
 
         # Rewind
         try:
             os.lseek(self._fd, 0, os.SEEK_SET)
         except OSError as e:
-            raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror) from OSError
 
         if buf[0] == b"0"[0]:
             return False
 
         if buf[0] == b"0"[0]:
             return False
@@ -235,13 +245,13 @@ class Pin:
             else:
                 os.write(self._fd, b"0\n")
         except OSError as e:
             else:
                 os.write(self._fd, b"0\n")
         except OSError as e:
-            raise GPIOError(e.errno, "Writing GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Writing GPIO: " + e.strerror) from OSError
 
         # Rewind
         try:
             os.lseek(self._fd, 0, os.SEEK_SET)
         except OSError as e:
 
         # Rewind
         try:
             os.lseek(self._fd, 0, os.SEEK_SET)
         except OSError as e:
-            raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror)
+            raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror) from OSError
 
     @property
     def chip_name(self):
 
     @property
     def chip_name(self):
@@ -266,13 +276,17 @@ class Pin:
         gpio_path = "/sys/class/gpio/{:s}/label".format(self.chip_name)
 
         try:
         gpio_path = "/sys/class/gpio/{:s}/label".format(self.chip_name)
 
         try:
-            with open(gpio_path, "r") as f_label:
+            with open(gpio_path, "r", encoding="utf-8") as f_label:
                 label = f_label.read()
         except (GPIOError, IOError) as e:
             if isinstance(e, IOError):
                 label = f_label.read()
         except (GPIOError, IOError) as e:
             if isinstance(e, IOError):
-                raise GPIOError(e.errno, "Reading gpiochip label: " + e.strerror)
+                raise GPIOError(
+                    e.errno, "Reading gpiochip label: " + e.strerror
+                ) from IOError
 
 
-            raise GPIOError(None, "Reading gpiochip label: " + e.strerror)
+            raise GPIOError(
+                None, "Reading gpiochip label: " + e.strerror
+            ) from GPIOError
 
         return label.strip()
 
 
         return label.strip()
 
@@ -281,10 +295,14 @@ class Pin:
     def _get_direction(self):
         # Read direction
         try:
     def _get_direction(self):
         # Read direction
         try:
-            with open(os.path.join(self._path, "direction"), "r") as f_direction:
+            with open(
+                os.path.join(self._path, "direction"), "r", encoding="utf-8"
+            ) as f_direction:
                 direction = f_direction.read()
         except IOError as e:
                 direction = f_direction.read()
         except IOError as e:
-            raise GPIOError(e.errno, "Getting GPIO direction: " + e.strerror)
+            raise GPIOError(
+                e.errno, "Getting GPIO direction: " + e.strerror
+            ) from IOError
 
         return direction.strip()
 
 
         return direction.strip()
 
@@ -296,10 +314,14 @@ class Pin:
 
         # Write direction
         try:
 
         # Write direction
         try:
-            with open(os.path.join(self._path, "direction"), "w") as f_direction:
+            with open(
+                os.path.join(self._path, "direction"), "w", encoding="utf-8"
+            ) as f_direction:
                 f_direction.write(direction.lower() + "\n")
         except IOError as e:
                 f_direction.write(direction.lower() + "\n")
         except IOError as e:
-            raise GPIOError(e.errno, "Setting GPIO direction: " + e.strerror)
+            raise GPIOError(
+                e.errno, "Setting GPIO direction: " + e.strerror
+            ) from IOError
 
     direction = property(_get_direction, _set_direction)
 
 
     direction = property(_get_direction, _set_direction)