From ff49538c047dfe4bf7954abe00942bf38000b9bd Mon Sep 17 00:00:00 2001 From: Foo Date: Wed, 11 Mar 2015 22:49:36 +0300 Subject: [PATCH] Do not leak environment and home directory in documentation Fixes #1329 --- docs/source/powerline_autodoc.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/source/powerline_autodoc.py b/docs/source/powerline_autodoc.py index 971717df..eba42edb 100644 --- a/docs/source/powerline_autodoc.py +++ b/docs/source/powerline_autodoc.py @@ -1,6 +1,8 @@ # vim:fileencoding=utf-8:noet from __future__ import (unicode_literals, division, absolute_import, print_function) +import os + from inspect import formatargspec from sphinx.ext import autodoc @@ -29,6 +31,34 @@ class ThreadedDocumenter(autodoc.FunctionDocumenter): return formatargspec(*argspec, formatvalue=formatvalue).replace('\\', '\\\\') +class Repr(object): + def __init__(self, repr_contents): + self.repr_contents = repr_contents + + def __repr__(self): + return '<{0}>'.format(self.repr_contents) + + +class EnvironDocumenter(autodoc.AttributeDocumenter): + @classmethod + def can_document_member(cls, member, membername, isattr, parent): + if type(member) is dict and member.get('environ') is os.environ: + return True + else: + return False + + def import_object(self, *args, **kwargs): + ret = super(EnvironDocumenter, self).import_object(*args, **kwargs) + if not ret: + return ret + self.object = self.object.copy() + if 'home' in self.object: + self.object.update(home=Repr('home directory')) + self.object.update(environ=Repr('environ dictionary')) + return True + + def setup(app): autodoc.setup(app) app.add_autodocumenter(ThreadedDocumenter) + app.add_autodocumenter(EnvironDocumenter)