Skip to content

Commit 1abdb0f

Browse files
scripts/augeas/gen-nutupsconf-aug.py.in, scripts/python/module/setup.py.in: fix codecs.open() deprecation warning (3.14+) [networkupstools#3256]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com> Co-Authored-by: Paul Donald <newtwen+github@gmail.com>
1 parent afe33f0 commit 1abdb0f

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

scripts/augeas/gen-nutupsconf-aug.py.in

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!@PYTHON_DEFAULT@
22
# Copyright (C)
33
# 2010 - Arnaud Quette <arnaud.quette@gmail.com>
4-
# 2020 - 2024 Jim Klimov <jimklimov+nut@gmail.com>
4+
# 2020 - 2026 Jim Klimov <jimklimov+nut@gmail.com>
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -25,7 +25,23 @@ from __future__ import print_function
2525
import sys
2626
import re
2727
import glob
28-
import codecs
28+
29+
try:
30+
# NOTE: Deprecated as of python3.14
31+
import codecs
32+
HAVE_CODECS = True
33+
fd = None
34+
try:
35+
# Open self to test that it works
36+
fd = codecs.open(os.path.abspath(inspect.getfile(inspect.currentframe())), mode="r", encoding='utf-8')
37+
USE_CODECS = True
38+
except DeprecationWarning:
39+
USE_CODECS = False
40+
if fd is not None:
41+
close(fd)
42+
except:
43+
HAVE_CODECS = False
44+
USE_CODECS = False
2945

3046
# Return a sorted list of unique entries, based on the input 'list'
3147
def sortUnique(list):
@@ -80,7 +96,10 @@ if __name__ == '__main__':
8096
for filename in glob.glob('../../drivers/*.c'):
8197
# 1.2/ Exclude main.c, which defines addvar() and skel.c (example driver)
8298
if filename not in Exceptionlist:
83-
fd = codecs.open(filename, encoding='utf-8')
99+
if USE_CODECS:
100+
fd = codecs.open(filename, encoding='utf-8')
101+
else:
102+
fd = open(filename, encoding='utf-8')
84103
# 1.3/ Grep for the "addvar(..." pattern
85104
matchResults = grep (r'.*addvar[\ ]*\(.*(VAR_FLAG|VAR_VALUE)*,.*', fd)
86105

@@ -95,7 +114,10 @@ if __name__ == '__main__':
95114
# Let's grep in .ch related files
96115
if (row[1].find('"') == -1):
97116
for defFilename in glob.glob(filename.replace('.c', '.[ch]')):
98-
defFd = codecs.open(defFilename, encoding='utf-8')
117+
if USE_CODECS:
118+
defFd = codecs.open(defFilename, encoding='utf-8')
119+
else:
120+
defFd = open(defFilename, encoding='utf-8')
99121
matchString = '^#define.*' + row[1].replace('"', '').lstrip() + '.*'
100122
matchResult = grep (matchString, defFd)
101123
for varDefine in matchResult:
@@ -115,12 +137,18 @@ if __name__ == '__main__':
115137
specificVars += " | \"%s\"\n" %(name)
116138

117139
# 2/ Load the template lens
118-
tplFd = codecs.open(dirPrefix + templateFilename, encoding='utf-8')
140+
if USE_CODECS:
141+
tplFd = codecs.open(dirPrefix + templateFilename, encoding='utf-8')
142+
else:
143+
tplFd = open(dirPrefix + templateFilename, encoding='utf-8')
119144

120145
# 2.1/ Search for the pattern to replace
121146
outputText = tplFd.read()
122147
outputText = outputText.replace('@SPECIFIC_DRV_VARS@', specificVars)
123148

124149
# 3/ Output final lens
125-
outFd = codecs.open(outputFilename, mode='w', encoding='utf-8')
150+
if USE_CODECS:
151+
outFd = codecs.open(outputFilename, mode='w', encoding='utf-8')
152+
else:
153+
outFd = open(outputFilename, mode='w', encoding='utf-8')
126154
outFd.write(outputText)

scripts/python/module/setup.py.in

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,35 @@ The setup.py file for PyNUTClient
55
# See also .github/workflows/PyNUTClient.yml for active packaging steps
66

77
from setuptools import setup, find_packages
8-
import codecs
98
import os
109

1110
here = os.path.abspath(os.path.dirname(__file__))
1211

12+
try:
13+
# NOTE: Deprecated as of python3.14
14+
import codecs
15+
HAVE_CODECS = True
16+
fd = None
17+
try:
18+
# Open self to test that it works
19+
fd = codecs.open(os.path.abspath(inspect.getfile(inspect.currentframe())), mode="r", encoding='utf-8')
20+
USE_CODECS = True
21+
except DeprecationWarning:
22+
USE_CODECS = False
23+
if fd is not None:
24+
close(fd)
25+
except:
26+
HAVE_CODECS = False
27+
USE_CODECS = False
28+
29+
1330
# README.txt appears from README.adoc during package or CI build
14-
with codecs.open(os.path.join(here, "README.txt"), encoding="utf-8") as fh:
15-
long_description = "\n" + fh.read()
31+
if USE_CODECS:
32+
with codecs.open(os.path.join(here, "README.txt"), encoding="utf-8") as fh:
33+
long_description = "\n" + fh.read()
34+
else:
35+
with open(os.path.join(here, "README.txt"), encoding="utf-8") as fh:
36+
long_description = "\n" + fh.read()
1637

1738
setup(
1839
name = "pynutclient", ### "PyNUTClient" lower-cased due to PEP-0625

0 commit comments

Comments
 (0)