Skip to contentSkip to conversation

bindings: python: don't use exec (...)

PATCH In review Phil Howard 2 participants 3 messages Oct 12, 2023 GPIO

lore
Share & tools
lore
#0 lore raw

cover bindings: python: don't use exec (...)

There's no need to open and exec the file, since "import" already does this implicitly.

Since we always expect this file to exist, the change of error in case of failure from:

FileNotFoundError: [Errno 2] No such file or directory: 'gpiod/version.py'

to the following:

ModuleNotFoundError: No module named 'gpiod.version'

is immaterial.

Phil Howard (1):
bindings: python: don't use exec to get __version__

 bindings/python/setup.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 bindings: python: don't use exec to get __version__

Import __version__ from gpiod.version instead of using exec since the latter is potentially unsafe and unnecessary.

---
 bindings/python/setup.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
bindings/python/setup.py +1 -3
index 66b7908..278baee 100644
@@ -5,6 +5,7 @@ from os import environ, path
 from setuptools import setup, Extension, find_packages
 from setuptools.command.build_ext import build_ext as orig_build_ext
 from shutil import rmtree
+from gpiod.version import __version__
 
 
 class build_ext(orig_build_ext):
@@ -54,9 +55,6 @@ if "GPIOD_WITH_TESTS" in environ and environ["GPIOD_WITH_TESTS"] == "1":
     extensions.append(gpiosim_ext)
     extensions.append(procname_ext)
 
-with open("gpiod/version.py", "r") as fd:
-    exec(fd.read())
-
 setup(
     name="libgpiod",
     packages=find_packages(exclude=["tests", "tests.*"]),
Signature · 1 line
2.34.1
#2 lore raw

Re: [libgpiod][PATCH 1/1] bindings: python: don't use exec to get __version__

Quoted 31 lines · Phil Howard

Import __version__ from gpiod.version instead of using exec since the latter is potentially unsafe and unnecessary.

---
 bindings/python/setup.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
bindings/python/setup.py +1 -4
index 66b7908..278baee 100644
@@ -5,6 +5,7 @@ from os import environ, path
 from setuptools import setup, Extension, find_packages
 from setuptools.command.build_ext import build_ext as orig_build_ext
 from shutil import rmtree
+from gpiod.version import __version__
 
 
 class build_ext(orig_build_ext):
@@ -54,9 +55,6 @@ if "GPIOD_WITH_TESTS" in environ and environ["GPIOD_WITH_TESTS"] == "1":
     extensions.append(gpiosim_ext)
     extensions.append(procname_ext)
 
-with open("gpiod/version.py", "r") as fd:
-    exec(fd.read())
-
 setup(
     name="libgpiod",
     packages=find_packages(exclude=["tests", "tests.*"]),
--
Quoted 3 lines

2.34.1

I get the following with this patch applied:

GPIOD_WITH_TESTS=1 \

/usr/bin/python setup.py build_ext --inplace \
	--include-dirs=../../include/:../../tests/gpiosim/ \
	--library-dirs=../../lib/.libs/:../../tests/gpiosim/.libs/

Traceback (most recent call last):
File "/home/brgl/workspace/gitbook/libgpiod/bindings/python/setup.py",
line 8, in <module>
from gpiod.version import __version__
File "/home/brgl/workspace/gitbook/libgpiod/bindings/python/gpiod/__init__.py",
line 10, in <module>
from . import _ext
ImportError: cannot import name '_ext' from partially initialized module 'gpiod' (most likely due to a circular import) (/home/brgl/workspace/gitbook/libgpiod/bindings/python/gpiod/__init__.py)

Bart