Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Markdown' object has no attribute '_doc' #1

Closed
elsbrock opened this issue Feb 27, 2010 · 3 comments
Closed

'Markdown' object has no attribute '_doc' #1

elsbrock opened this issue Feb 27, 2010 · 3 comments

Comments

@elsbrock
Copy link

I am using discount in a Django project and recently encountered the following issue:

Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/foo/var/views.py" in edit_article
  99.                 new_rev.save()
File "/foo/bar/models.py" in save
  14.         self.contents_parsed = markdown.get_html_content()
File "/usr/lib/python2.5/site-packages/discount-0.2.0BETA-py2.5-linux-x86_64.egg/discount/__init__.py" in get_html_content
  289.         return self._generate_html_content()
File "/usr/lib/python2.5/site-packages/discount-0.2.0BETA-py2.5-linux-x86_64.egg/discount/__init__.py" in _generate_html_content
  188.             ln = libmarkdown.mkd_document(self._get_compiled_doc(), ctypes.byref(sb))
File "/usr/lib/python2.5/site-packages/discount-0.2.0BETA-py2.5-linux-x86_64.egg/discount/__init__.py" in _get_compiled_doc
  178.             return self._doc

Exception Value: 'Markdown' object has no attribute '_doc'

Code context:

markdown = discount.Markdown(self.contents, toc=True, autolink=True, safelink=True)
self.contents_parsed = markdown.get_html_content()

The problem seems to be related with special characters such as the German umlauts äöü. Since I have no problem using special characters elsewhere in my application, I assume the problem lies in the discount interface. Any hints?

Thanks for investigating this issue!

@tamask
Copy link
Contributor

tamask commented Feb 27, 2010

Thanks for reporting this. I was able to reproduce this by passing a
unicode string with a non ascii-character like u'\xeb' as the input
(like you mentioned). The AttributeError you're getting is a side
effect of a UnicodeEncodeError being raised in the
Markdown._get_compiled_doc method. I can fix this method so the
proper exception is not lost.

Currently, discount.Markdown is oblivious to encoding, it just
passes the string directly to ctypes. ctypes will try and encode
the unicode string as a bytestring using the default encoding (see
sys.getdefaultencoding()), but will likely fail on non-ascii
characters if the default encoding is not unicode (i.e if it's
'ascii').

At this time, I'm not sure if the Python discount binding should
also be responsible for managing character encoding (and if it should,
what is the best way to design it).

For now, with Django, you could use
django.utils.encoding.smart_str() to convert your unicode string to
a bytestring before you pass it as an argument to discount.Markdown,
and then use django.utils.encoding.smart_unicode() to convert the
return of Markdown.get_html_content() back to unicode.

@elsbrock
Copy link
Author

Wonderful! Thank you very much for your efforts.

@tamask
Copy link
Contributor

tamask commented Mar 2, 2010

Exception handling fixed in 0.2.1STABLE, closing.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants