INN commit: trunk (doc/pod/hook-python.pod samples/filter_innd.py)
INN Commit
Russ_Allbery at isc.org
Tue Dec 23 12:24:52 UTC 2008
Date: Tuesday, December 23, 2008 @ 04:24:52
Author: iulius
Revision: 8252
Add notes about buffers and memory views in documentation.
Also fix a wrong syntax to have access to a dictionary key.
see #78
Modified:
trunk/doc/pod/hook-python.pod
trunk/samples/filter_innd.py
-------------------------+
doc/pod/hook-python.pod | 23 ++++++++++++++++++++---
samples/filter_innd.py | 4 ++--
2 files changed, 22 insertions(+), 5 deletions(-)
Modified: doc/pod/hook-python.pod
===================================================================
--- doc/pod/hook-python.pod 2008-12-23 12:22:03 UTC (rev 8251)
+++ doc/pod/hook-python.pod 2008-12-23 12:24:52 UTC (rev 8252)
@@ -95,14 +95,28 @@
same named article headers, except for the special C<__BODY__> and C<__LINES__>
items. Items not present in the article will contain C<None>.
-C<art('__BODY__')> is a buffer object containing the article's entire body, and
-C<art('__LINES__')> is an int holding B<innd>'s reckoning of the number of lines
+C<art['__BODY__']> is a buffer object containing the article's entire body, and
+C<art['__LINES__']> is an int holding B<innd>'s reckoning of the number of lines
in the article. All the other elements will be buffers with the contents
of the same-named article headers.
The Newsgroups: header of the article is accessible inside the Python
filter as C<art['Newsgroups']>.
+If interned strings are used in the filter, calls to C<art[__BODY__]> or
+C<art[Newsgroups]> are faster:
+
+ # Syntax for Python 2.x.
+ Newsgroups = intern("Newsgroups")
+ if art[Newsgroups] == buffer("misc.test"):
+ print("Test group")
+
+ # Syntax for Python 3.x.
+ import sys
+ Newsgroups = sys.intern("Newsgroups")
+ if art[Newsgroups] == memoryview(b"misc.test"):
+ print("Test group")
+
If you want to accept an article, return C<None> or an empty string. To
reject, return a non-empty string. The rejection strings will be shown to
local clients and your peers, so keep that in mind when phrasing your
@@ -175,6 +189,9 @@
=head2 A Note regarding Buffer Objects
+This section is not applicable to S<Python 3.x> where buffer objects have
+been replaced with memory views.
+
Buffer objects are cousins of strings, new in S<Python 1.5.2>. Using buffer
objects may take some getting used to, but we can create buffers much faster
and with less memory than strings.
@@ -277,7 +294,7 @@
# Here we are running a local spam filter, so why eat all those
# cancels? We can add fake entries to history so they'll get
# refused. Returns 1 on success, 0 on failure.
- cancelled_id = buffer('<meow$123.456 at isc.org>')
+ cancelled_id = '<meow$123.456 at isc.org>'
if INN.addhist("<cancel." + cancelled_id[1:]):
thought = "Eat my dust, roadkill!"
else:
Modified: samples/filter_innd.py
===================================================================
--- samples/filter_innd.py 2008-12-23 12:22:03 UTC (rev 8251)
+++ samples/filter_innd.py 2008-12-23 12:24:52 UTC (rev 8252)
@@ -183,8 +183,8 @@
X-PGP-Key, X-PGP-Sig, X-Poster-Trace, X-Postfilter, X-Proxy-User,
X-Submissions-To, X-Trace, X-Usenet-Provider, X-User-ID, Xref.
- The body is the buffer in art['__BODY__'] and the INN-reckoned
- line count is held as an integer in art['__LINES__']. (The
+ The body is the buffer in art[__BODY__] and the INN-reckoned
+ line count is held as an integer in art[__LINES__]. (The
Lines: header is often generated by the poster, and large
differences can be a good indication of a corrupt article.)
More information about the inn-committers
mailing list