HariSekhon / Nagios-Plugins

450+ AWS, Hadoop, Cloud, Kafka, Docker, Elasticsearch, RabbitMQ, Redis, HBase, Solr, Cassandra, ZooKeeper, HDFS, Yarn, Hive, Presto, Drill, Impala, Consul, Spark, Jenkins, Travis CI, Git, MySQL, Linux, DNS, Whois, SSL Certs, Yum Security Updates, Kubernetes, Cloudera etc...
https://www.linkedin.com/in/HariSekhon
Other
1.14k stars 506 forks source link

Python: Iterable moved from collections to collections.abc #430

Open eLvErDe opened 3 weeks ago

eLvErDe commented 3 weeks ago

Hello,

On recent Python version (worked with 3.9, fails with 3.11), Python script crash because Iterable import namespace has changed.

The following patch fixes it:

--- a/harisekhon/utils.py
+++ b/harisekhon/utils.py
@@ -54,6 +54,10 @@ import defusedxml.ElementTree as ET
 #     pass
 # Python 2.6 throws ExpatError instead of ParseError
 # from xml.parsers.expat import ExpatError
+try:
+    from collections import Iterable
+except ImportError:  # Recent Python like 3.11
+    from collections.abc import Iterable

 __author__ = 'Hari Sekhon'
 __version__ = '0.14.0'
@@ -1105,12 +1109,12 @@ def isIP(arg):

 def isIterable(arg):
     # collections.Iterable Python 2.6+
-    return isinstance(arg, collections.Iterable)
+    return isinstance(arg, Iterable)

 def isIterableNotStr(arg):
     # collections.Iterable Python 2.6+
-    return isinstance(arg, collections.Iterable) and not isStr(arg)
+    return isinstance(arg, Iterable) and not isStr(arg)

 def isJavaException(arg):
HariSekhon commented 3 weeks ago

Thanks for raising this.

If you want to raise it as a pull request to the

https://github.com/HariSekhon/pylib

repo then I'll approve it and you'll get contribution credit.

If you don't want to then I'll make the change.