austgl / python-markdown2

Automatically exported from code.google.com/p/python-markdown2
Other
0 stars 0 forks source link

interesting error when using markdown2 as a postgres plpythonu stored function #20

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
First, download and install the markdown2 package.

Next, create a stored procedure for Postgres using:

CREATE OR REPLACE FUNCTION markdown(src text) RETURNS text
    AS $$

import markdown2
return markdown2.markdown(src)

$$
    LANGUAGE plpythonu STRICT;

(it is preferable to put the above into a file, for example test.sql)

Next, install the above function into your database and test using:

db=# \i test.sql
CREATE FUNCTION

db=# SELECT markdown('test');
ERROR:  plpython: function "markdown" failed
DETAIL:  <type 'exceptions.AttributeError'>: 'module' object has no attribute 
'argv'
db=# ^D\q

What version of the product are you using? On what operating system?

Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Database is Postgres 8.3.3, compiled from source to work with Apple's 
pre-installed
version of Python.

Platform is Apple's Leopard 10.5.5 (9F33)

Please provide any additional information below.

Workaround: removing all the mainline stuff in lib/markdown2.py makes the 
module work.

However, that workaround isn't optimal since the command-line functionality as 
given
is removed thus impairing module functions, not to mention bad practice.

You must create a plpythonu language wrapper before attempting any of the above
within postgres.  Do that by issuing:

shell% psql db # db is your database
db# CREATE PROCEDURAL LANGUAGE plpythonu;
db# \q
shell%

Original issue reported on code.google.com by drink...@gmail.com on 22 Sep 2008 at 5:46

GoogleCodeExporter commented 9 years ago
@drinksev: Does it work if markdown2.py is changed with this patch?

{{{
Index: lib/markdown2.py
===================================================================
--- lib/markdown2.py    (revision 166)
+++ lib/markdown2.py    (working copy)
@@ -1742,7 +1742,9 @@
     import doctest
     doctest.testmod()

-def main(argv=sys.argv):
+def main(argv=None):
+    if argv is None:
+        argv = sys.argv
     if not logging.root.handlers:
         logging.basicConfig()
}}}

In other words, don't rely in 'sys.argv' being defined at the top-level.

I'm not able to test with Postgres easily. I hadn't known that `sys.argv` isn't
defined in a postgres stored procedure.

Original comment by tre...@gmail.com on 22 Sep 2008 at 6:46

GoogleCodeExporter commented 9 years ago
Works perfectly with the above changes.

Original comment by drink...@gmail.com on 22 Sep 2008 at 8:13

GoogleCodeExporter commented 9 years ago
Checked in: r167.

This will be in markdown2 v1.0.1.11 when I release that (don't know when that 
will be
yet). Let me know if that holds you up.

Thanks for the report.

Original comment by tre...@gmail.com on 22 Sep 2008 at 8:20