INN commit: trunk/samples (9 files)

INN Commit Russ_Allbery at isc.org
Tue Dec 23 12:00:46 UTC 2008


    Date: Tuesday, December 23, 2008 @ 04:00:46
  Author: iulius
Revision: 8250

In Python samples:
* Remove tabulations for indentations (not allowed by
  Python 3.0 when spaces are used for that purpose).
* Add a comment for "except Exception *as* errmsg"
  (new syntax).
* Add a comment for memoryview() instead of buffer()
  which is absent from Python 3.0.
* Remove occurrences of types.InstanceType (absent
  from Python 3.0).
* Use parenthesis for print().
* Add a hook to use sys.intern() instead of intern()
  for Python 3.0.

All the samples are for directly use with Python 2.x.
Commented lines are present wherever the syntax has
changed with Python 3.x.

see #78

Modified:
  trunk/samples/INN.py
  trunk/samples/filter_innd.py
  trunk/samples/nnrpd.py
  trunk/samples/nnrpd_access.py
  trunk/samples/nnrpd_access_wrapper.py
  trunk/samples/nnrpd_auth.py
  trunk/samples/nnrpd_auth_wrapper.py
  trunk/samples/nnrpd_dynamic.py
  trunk/samples/nnrpd_dynamic_wrapper.py

--------------------------+
 INN.py                   |   21 +++++++++------------
 filter_innd.py           |   23 +++++++++++++++++++----
 nnrpd.py                 |    7 ++-----
 nnrpd_access.py          |   17 +++++++++--------
 nnrpd_access_wrapper.py  |   15 ++++++++++-----
 nnrpd_auth.py            |   19 ++++++++++---------
 nnrpd_auth_wrapper.py    |   11 ++++++++---
 nnrpd_dynamic.py         |   19 ++++++++++---------
 nnrpd_dynamic_wrapper.py |    3 ++-
 9 files changed, 79 insertions(+), 56 deletions(-)

Modified: INN.py
===================================================================
--- INN.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ INN.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -9,31 +9,28 @@
 from types import *
 
 def set_filter_hook(anObject):
-    if type(anObject) == InstanceType:
-        print "** set_filter_hook for " + repr(anObject)
-    else:
-        print "** <Your object is not a class instance.>"
+    print("** set_filter_hook for " + repr(anObject))
 
 def addhist(messageid):
-    print "** addhist Message-ID: " + messageid
+    print("** addhist Message-ID: " + messageid)
 
 def havehist(messageid):
-    print "** havehist Message-ID: " + messageid
+    print("** havehist Message-ID: " + messageid)
 
 def cancel(messageid):
-    print "** cancel Message-ID: " + messageid
+    print("** cancel Message-ID: " + messageid)
 
 def newsgroup(groupname):
-    print "** newsgroup: " + groupname
+    print("** newsgroup: " + groupname)
 
 def head(messageid):
-    print "** head Message-ID: " + messageid
+    print("** head Message-ID: " + messageid)
 
 def article(messageid):
-    print "** article Message-ID: " + messageid
+    print("** article Message-ID: " + messageid)
 
 def hashstring(mystring):
-    print "** hash: " + mystring
+    print("** hash: " + mystring)
 
 def syslog(level, message):
-    print "-- syslog level: %s message: %s" % (level, message)
+    print("-- syslog level: %s message: %s" % (level, message))

Modified: filter_innd.py
===================================================================
--- filter_innd.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ filter_innd.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -19,6 +19,15 @@
 import re
 from string import *
 
+
+##  The built-in intern() method has been in the sys module
+##  since Python 3.0.
+import sys
+if sys.version_info[0] >= 3:
+    def intern(headerName):
+        return sys.intern(headerName)
+
+
 ##  This looks weird, but creating and interning these strings should
 ##  let us get faster access to header keys (which innd also interns) by
 ##  losing some strcmps under the covers.
@@ -142,7 +151,7 @@
         preceded by a CHECK.)
         """
         return ""               # Deactivate the samples.
-        
+
         if self.re_none44.search(msgid):
             return "But I don't like spam!"
         if msgid[0:8] == '<cancel.':
@@ -199,6 +208,10 @@
                 if self.re_newrmgroup.match(art[Control]):
                     if self.re_meow.search(art[__BODY__]):
                         return "The fake tale meows again."
+                    # Python 3.x uses memoryview(b'mxyzptlk') because buffers
+                    # do not exist any longer.  Note that the argument is
+                    # a bytes object.
+                    # if art[Distribution] == memoryview(b'mxyzptlk'):
                     if art[Distribution] == buffer('mxyzptlk'):
                         return "Evil control message from the 10th dimension"
                 if self.re_obsctl.match(art[Control]):
@@ -252,7 +265,9 @@
 
 try:
     import sys
-except Exception, errmsg:
+
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('Error', "import boo-boo: " + errmsg[0])
 
 
@@ -275,6 +290,6 @@
 try:
     set_filter_hook(spamfilter)
     syslog('n', "spamfilter successfully hooked into INN")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('e', "Cannot obtain INN hook for spamfilter: %s" % errmsg[0])
-

Modified: nnrpd.py
===================================================================
--- nnrpd.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -9,10 +9,7 @@
 from types import *
 
 def set_auth_hook(anObject):
-    if type(anObject) == InstanceType:
-        print "** set_auth_hook for " + repr(anObject)
-    else:
-        print "** <Your object is not a class instance.>"
+    print("** set_auth_hook for " + repr(anObject))
 
 def syslog(level, message):
-    print "-- syslog level: %s message: %s" % (level, message)
+    print("-- syslog level: %s message: %s" % (level, message))

Modified: nnrpd_access.py
===================================================================
--- nnrpd_access.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd_access.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -53,14 +53,14 @@
     def access(self, attributes):
         """Called when python_access: is encountered in readers.conf."""
 
-	# Just for debugging purposes.
-	syslog('notice', 'n_a access() invoked: hostname %s, ipaddress %s, interface %s, user %s' % (\
-		attributes['hostname'], \
-		attributes['ipaddress'], \
-		attributes['interface'], \
-		attributes['user']))
+        # Just for debugging purposes.
+        syslog('notice', 'n_a access() invoked: hostname %s, ipaddress %s, interface %s, user %s' % ( \
+                attributes['hostname'], \
+                attributes['ipaddress'], \
+                attributes['interface'], \
+                attributes['user']))
 
-	# Allow newsreading from specific host only.
+        # Allow newsreading from specific host only.
         if '127.0.0.1' == str(attributes['ipaddress']):
             syslog('notice', 'authentication access by IP address succeeded')
             return {'read':'*', 'post':'*'}
@@ -88,5 +88,6 @@
 try:
     set_auth_hook(myaccess)
     syslog('notice', "access module successfully hooked into nnrpd")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('error', "Cannot obtain nnrpd hook for access method: %s" % errmsg[0])

Modified: nnrpd_access_wrapper.py
===================================================================
--- nnrpd_access_wrapper.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd_access_wrapper.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -1,7 +1,7 @@
 ##  $Id$
 ##
 ##  Example wrapper for support of old Python authentication scripts,
-##  by Erik Klavon. 
+##  by Erik Klavon.
 ##
 ##  This file contains a sample Python script which can be used to
 ##  duplicate the behaviour of the old nnrppythonauth functionality.
@@ -32,13 +32,17 @@
         self.old = OLDAUTH()
 
     def access(self, attributes):
+        # Python 3.x uses memoryview(b'connect') because buffers
+        # do not exist any longer.  Note that the argument is
+        # a bytes object.
+        # attributes['type'] = memoryview(b'connect')
         attributes['type'] = buffer('connect')
         perm = (self.old).authenticate(attributes)
-        result = dict([('users','*')])        
+        result = dict({('users', '*')})
         if perm[1] == 1:
-                result['read'] = perm[3]
+            result['read'] = perm[3]
         if perm[2] == 1:
-                result['post'] = perm[3]
+            result['post'] = perm[3]
         return result
 
     def access_close(self):
@@ -60,5 +64,6 @@
 try:
     set_auth_hook(myaccess)
     syslog('notice', "access module successfully hooked into nnrpd")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('error', "Cannot obtain nnrpd hook for access method: %s" % errmsg[0])

Modified: nnrpd_auth.py
===================================================================
--- nnrpd_auth.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd_auth.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -59,15 +59,15 @@
     def authenticate(self, attributes):
         """Called when python_auth: is encountered in readers.conf."""
 
-	# Just for debugging purposes.
-	syslog('notice', 'n_a authenticate() invoked: hostname %s, ipaddress %s, interface %s, user %s' % (\
-		attributes['hostname'], \
-		attributes['ipaddress'], \
-		attributes['interface'], \
-		attributes['user']))
+        # Just for debugging purposes.
+        syslog('notice', 'n_a authenticate() invoked: hostname %s, ipaddress %s, interface %s, user %s' % ( \
+                attributes['hostname'], \
+                attributes['ipaddress'], \
+                attributes['interface'], \
+                attributes['user']))
 
-	# Do username password authentication.
-        if 'foo' == str(attributes['user'])  \
+        # Do username password authentication.
+        if 'foo' == str(attributes['user']) \
           and 'foo' == str(attributes['pass']):
             syslog('notice', 'authentication by username succeeded')
             return (self.authcodes['ALLOWED'], 'No error', 'default_user')
@@ -95,5 +95,6 @@
 try:
     set_auth_hook(myauth)
     syslog('notice', "authentication module successfully hooked into nnrpd")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('error', "Cannot obtain nnrpd hook for authentication method: %s" % errmsg[0])

Modified: nnrpd_auth_wrapper.py
===================================================================
--- nnrpd_auth_wrapper.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd_auth_wrapper.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -32,12 +32,16 @@
         self.old = OLDAUTH()
 
     def authenticate(self, attributes):
+        # Python 3.x uses memoryview(b'authinfo') because buffers
+        # do not exist any longer.  Note that the argument is
+        # a bytes object.
+        # attributes['type'] = memoryview(b'authinfo')
         attributes['type'] = buffer('authinfo')
         perm = (self.old).authenticate(attributes)
         err_str = "No error"
         if perm[0] == 481:
-                err_str = "Python authentication error!"        
-        return (perm[0],err_str)                
+            err_str = "Python authentication error!"
+        return (perm[0],err_str)
 
     def authen_close(self):
         (self.old).close()
@@ -58,5 +62,6 @@
 try:
     set_auth_hook(myauth)
     syslog('notice', "authentication module successfully hooked into nnrpd")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('error', "Cannot obtain nnrpd hook for authentication method: %s" % errmsg[0])

Modified: nnrpd_dynamic.py
===================================================================
--- nnrpd_dynamic.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd_dynamic.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -56,15 +56,15 @@
            readers.conf and a reader requests either read or post
            permission for particular newsgroup."""
 
-	# Just for debugging purposes.
-	syslog('notice', 'n_a dynamic() invoked against type %s, hostname %s, ipaddress %s, interface %s, user %s' % (\
-		attributes['type'], \
-		attributes['hostname'], \
-		attributes['ipaddress'], \
-		attributes['interface'], \
-		attributes['user']))
+        # Just for debugging purposes.
+        syslog('notice', 'n_a dynamic() invoked against type %s, hostname %s, ipaddress %s, interface %s, user %s' % ( \
+                attributes['type'], \
+                attributes['hostname'], \
+                attributes['ipaddress'], \
+                attributes['interface'], \
+                attributes['user']))
 
-	# Allow reading of any newsgroup but not posting.
+        # Allow reading of any newsgroup but not posting.
         if 'post' == str(attributes['type']):
             syslog('notice', 'dynamic authorization access for post access denied')
             return "no posting for you"
@@ -95,5 +95,6 @@
 try:
     set_auth_hook(mydynaccess)
     syslog('notice', "dynamic access module successfully hooked into nnrpd")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('error', "Cannot obtain nnrpd hook for dynamic access method: %s" % errmsg[0])

Modified: nnrpd_dynamic_wrapper.py
===================================================================
--- nnrpd_dynamic_wrapper.py	2008-12-22 23:12:42 UTC (rev 8249)
+++ nnrpd_dynamic_wrapper.py	2008-12-23 12:00:46 UTC (rev 8250)
@@ -53,5 +53,6 @@
 try:
     set_auth_hook(mydynaccess)
     syslog('notice', "dynamic access module successfully hooked into nnrpd")
-except Exception, errmsg:
+except Exception, errmsg:    # Syntax for Python 2.x.
+#except Exception as errmsg: # Syntax for Python 3.x.
     syslog('error', "Cannot obtain nnrpd hook for dynamic access method: %s" % errmsg[0])




More information about the inn-committers mailing list