Georg Brandl avatar Georg Brandl committed ff4b97a

Use standard ``file:line: warning: message`` format for warning messages.

Comments (0)

Files changed (13)

sphinx/application.py

             self.emit('build-finished', None)
         self.builder.cleanup()
 
-    def warn(self, message):
+    def warn(self, message, location=None, prefix='warning: '):
+        warntext = location and '%s: %s%s\n' % (location, prefix, message) or \
+                   '%s%s\n' % (prefix, message)
         if self.warningiserror:
-            raise SphinxWarning('WARNING: %s\n' % message)
+            raise SphinxWarning(warntext)
         self._warncount += 1
         try:
-            self._warning.write('WARNING: %s\n' % message)
+            self._warning.write(warntext)
         except UnicodeEncodeError:
             encoding = getattr(self._warning, 'encoding', 'ascii')
-            self._warning.write(('WARNING: %s\n' % message).encode(encoding,
-                                                                   'replace'))
+            self._warning.write(warntext.encode(encoding, 'replace'))
 
     def info(self, message='', nonl=False):
         try:

sphinx/builders/__init__.py

                     if candidate:
                         break
                 else:
-                    self.warn('%s:%s: no matching candidate for image URI %r' %
-                              (node.source, getattr(node, 'lineno', ''),
-                               node['uri']))
+                    self.warn(
+                        'no matching candidate for image URI %r' % node['uri'],
+                        '%s:%s' % (node.source, getattr(node, 'line', '')))
                     continue
                 node['uri'] = candidate
             else:
         updated_docnames = set()
         # while reading, collect all warnings from docutils
         warnings = []
-        self.env.set_warnfunc(warnings.append)
+        self.env.set_warnfunc(lambda *args: warnings.append(args))
         self.info(bold('updating environment: '), nonl=1)
         iterator = self.env.update(self.config, self.srcdir,
                                    self.doctreedir, self.app)
             # nothing further to do, the environment has already
             # done the reading
         for warning in warnings:
-            if warning.strip():
-                self.warn(warning)
+            self.warn(*warning)
         self.env.set_warnfunc(self.warn)
 
         doccount = len(updated_docnames)
 
         # write target files
         warnings = []
-        self.env.set_warnfunc(warnings.append)
+        self.env.set_warnfunc(lambda *args: warnings.append(args))
         for docname in self.status_iterator(sorted(docnames),
                                             'writing output... ', darkgreen):
             doctree = self.env.get_and_resolve_doctree(docname, self)
             self.write_doc(docname, doctree)
         for warning in warnings:
-            if warning.strip():
-                self.warn(warning)
+            self.warn(*warning)
         self.env.set_warnfunc(self.warn)
 
     def prepare_writing(self, docnames):

sphinx/builders/html.py

             finally:
                 f.close()
         except (IOError, OSError), err:
-            self.warn("Error writing file %s: %s" % (outfilename, err))
+            self.warn("error writing file %s: %s" % (outfilename, err))
         if self.copysource and ctx.get('sourcename'):
             # copy the source file for the "show source" link
             source_name = path.join(self.outdir, '_sources',

sphinx/builders/latex.py

     def init_document_data(self):
         preliminary_document_data = map(list, self.config.latex_documents)
         if not preliminary_document_data:
-            self.warn('No "latex_documents" config value found; no documents '
-                      'will be written.')
+            self.warn('no "latex_documents" config value found; no documents '
+                      'will be written')
             return
         # assign subdirs to titles
         self.titles = []
                             includefile, self.env.get_doctree(includefile))
                         self.docnames.add(includefile)
                     except Exception:
-                        self.warn('%s: toctree contains ref to nonexisting '
-                                  'file %r' % (docname, includefile))
+                        self.warn('toctree contains ref to nonexisting '
+                                  'file %r' % includefile,
+                                  self.builder.env.doc2path(docname))
                     else:
                         sof = addnodes.start_of_file(docname=includefile)
                         sof.children = subtree.children

sphinx/builders/linkcheck.py

                 self.write_entry('broken', docname, lineno, uri + ': ' + s)
                 self.broken[uri] = (r, s)
                 if self.app.quiet:
-                    self.warn('%s:%s: broken link: %s' % (docname, lineno, uri))
+                    self.warn('broken link: %s' % uri,
+                              '%s:%s' % (self.env.doc2path(docname), lineno))
             else:
                 self.info(' - ' + purple('redirected') + ' to ' + s)
                 self.write_entry('redirected', docname,
             self.warn(uri + ' - ' + red('malformed!'))
             self.write_entry('malformed', docname, lineno, uri)
             if self.app.quiet:
-                self.warn('%s:%s: malformed link: %s' % (docname, lineno, uri))
+                self.warn('malformed link: %s' % uri,
+                          '%s:%s' % (self.env.doc2path(docname), lineno))
             self.app.statuscode = 1
 
         if self.broken:

sphinx/builders/text.py

             finally:
                 f.close()
         except (IOError, OSError), err:
-            self.warn("Error writing file %s: %s" % (outfilename, err))
+            self.warn("error writing file %s: %s" % (outfilename, err))
 
     def finish(self):
         pass

sphinx/environment.py

 dummy_reporter = Reporter('', 4, 4)
 
 
-class RedirStream(object):
-    def __init__(self, writefunc):
-        self.writefunc = writefunc
+class WarningStream(object):
+    def __init__(self, warnfunc):
+        self.warnfunc = warnfunc
     def write(self, text):
         if text.strip():
-            self.writefunc(text)
+            self.warnfunc(text, None, '')
 
 
 class NoUri(Exception):
 
     def set_warnfunc(self, func):
         self._warnfunc = func
-        self.settings['warning_stream'] = RedirStream(func)
+        self.settings['warning_stream'] = WarningStream(func)
 
     def warn(self, docname, msg, lineno=None):
         if docname:
             if lineno is None:
                 lineno = ''
-            self._warnfunc('%s:%s: %s' % (self.doc2path(docname), lineno, msg))
+            self._warnfunc(msg, '%s:%s' % (self.doc2path(docname), lineno))
         else:
-            self._warnfunc('GLOBAL:: ' + msg)
+            self._warnfunc(msg)
 
     def clear_doc(self, docname):
         """Remove all traces of a source file in the inventory."""
                 filepath = path.normpath(path.join(docdir, node['reftarget']))
             self.dependencies.setdefault(docname, set()).add(filepath)
             if not os.access(path.join(self.srcdir, filepath), os.R_OK):
-                self.warn(docname, 'Download file not readable: %s' % filepath,
+                self.warn(docname, 'download file not readable: %s' % filepath,
                           getattr(node, 'line', None))
                 continue
             uniquename = self.dlfiles.add_file(docname, filepath)
             node['candidates'] = candidates = {}
             imguri = node['uri']
             if imguri.find('://') != -1:
-                self.warn(docname, 'Nonlocal image URI found: %s' % imguri,
+                self.warn(docname, 'nonlocal image URI found: %s' % imguri,
                           node.line)
                 candidates['?'] = imguri
                 continue
                                 f.close()
                         except (OSError, IOError):
                             self.warn(docname,
-                                      'Image file %s not readable' % filename)
+                                      'image file %s not readable' % filename)
                         if imgtype:
                             candidates['image/' + imgtype] = new_imgpath
             else:
             for imgpath in candidates.itervalues():
                 self.dependencies.setdefault(docname, set()).add(imgpath)
                 if not os.access(path.join(self.srcdir, imgpath), os.R_OK):
-                    self.warn(docname, 'Image file not readable: %s' % imgpath,
+                    self.warn(docname, 'image file not readable: %s' % imgpath,
                               node.line)
                     continue
                 self.images.add_file(docname, imgpath)
             f.close()
         doctree.settings.env = self
         doctree.reporter = Reporter(self.doc2path(docname), 2, 4,
-                                    stream=RedirStream(self._warnfunc))
+                                    stream=WarningStream(self._warnfunc))
         return doctree
 
 

sphinx/theming.py

                         tname = theme[:-4]
                         tinfo = zfile
                     except Exception:
-                        builder.warn('File %r on theme path is not a valid '
+                        builder.warn('file %r on theme path is not a valid '
                                      'zipfile or contains no theme' % theme)
                         continue
                 else:

sphinx/writers/latex.py

         elif self.this_is_the_title:
             if len(node.children) != 1 and not isinstance(node.children[0],
                                                           nodes.Text):
-                self.builder.warn('document title is not a single Text node')
+                self.builder.warn(
+                    'document title is not a single Text node',
+                    '%s:%s' % (self.builder.env.doc2path(self.curfilestack[-1]),
+                               node.line or ''))
             if not self.elements['title']:
                 # text needs to be escaped since it is inserted into
                 # the output literally
             self.table.caption = self.encode(node.astext())
             raise nodes.SkipNode
         else:
-            self.builder.warn('encountered title node not in section, topic, '
-                              'table, admonition or sidebar')
+            self.builder.warn(
+                'encountered title node not in section, topic, table, '
+                'admonition or sidebar',
+                '%s:%s' % (self.builder.env.doc2path(self.curfilestack[-1]),
+                           node.line or ''))
             self.body.append('\\textbf{')
             self.context.append('}\n')
         self.in_title = 1
                 self.body.append('\\grammartoken{')
             self.context.append('}')
         else:
-            self.builder.warn('unusable reference target found: %s' % uri)
+            self.builder.warn(
+                'unusable reference target found: %s' % uri,
+                '%s:%s' % (self.builder.env.doc2path(self.curfilestack[-1]),
+                           node.line or ''))
             self.context.append('')
     def depart_reference(self, node):
         self.body.append(self.context.pop())

tests/test_application.py

 
         old_count = app._warncount
         app.warn("Bad news!")
-        assert warnings.getvalue() == "WARNING: Bad news!\n"
+        assert warnings.getvalue() == "warning: Bad news!\n"
         assert app._warncount == old_count + 1
     finally:
         app.cleanup()

tests/test_autodoc.py

         platform = '',
         deprecated = False,
         members = [],
+        member_order = 'alphabetic',
     )
 
     directive = Struct(

tests/test_build.py

 latex_warnfile = StringIO()
 
 ENV_WARNINGS = """\
-WARNING: %(root)s/images.txt:9: Image file not readable: foo.png
-WARNING: %(root)s/images.txt:23: Nonlocal image URI found: \
+%(root)s/images.txt:9: warning: image file not readable: foo.png
+%(root)s/images.txt:23: warning: nonlocal image URI found: \
 http://www.python.org/logo.png
-WARNING: %(root)s/includes.txt:: (WARNING/2) Encoding 'utf-8' used for reading \
+%(root)s/includes.txt:: (WARNING/2) Encoding 'utf-8' used for reading \
 included file u'wrongenc.inc' seems to be wrong, try giving an :encoding: option
-WARNING: %(root)s/includes.txt:56: Download file not readable: nonexisting.png
+%(root)s/includes.txt:56: warning: download file not readable: nonexisting.png
 """
 
 HTML_WARNINGS = ENV_WARNINGS + """\
-WARNING: %(root)s/images.txt:: no matching candidate for image URI u'foo.*'
-WARNING: %(root)s/markup.txt:: invalid index entry u''
-WARNING: %(root)s/markup.txt:: invalid pair index entry u''
-WARNING: %(root)s/markup.txt:: invalid pair index entry u'keyword; '
+%(root)s/images.txt:20: warning: no matching candidate for image URI u'foo.*'
+%(root)s/markup.txt:: warning: invalid index entry u''
+%(root)s/markup.txt:: warning: invalid pair index entry u''
+%(root)s/markup.txt:: warning: invalid pair index entry u'keyword; '
 """
 
 LATEX_WARNINGS = ENV_WARNINGS + """\
-WARNING: None:: no matching candidate for image URI u'foo.*'
-WARNING: invalid pair index entry u''
+None:None: warning: no matching candidate for image URI u'foo.*'
+warning: invalid pair index entry u''
 """
 
 HTML_XPATH = {

tests/test_env.py

     global app, env
     app = TestApp(srcdir='(temp)')
     env = BuildEnvironment(app.srcdir, app.doctreedir, app.config)
-    env.set_warnfunc(warnings.append)
+    env.set_warnfunc(lambda *args: warnings.append(args))
 
 def teardown_module():
     app.cleanup()
 
 def warning_emitted(file, text):
     for warning in warnings:
-        if file+':' in warning and text in warning:
+        if len(warning) == 2 and file+':' in warning[1] and text in warning[0]:
             return True
     return False
 
     assert docnames == env.found_docs == set(env.all_docs)
 
 def test_images():
-    assert warning_emitted('images.txt', 'Image file not readable: foo.png')
-    assert warning_emitted('images.txt', 'Nonlocal image URI found: '
+    assert warning_emitted('images.txt', 'image file not readable: foo.png')
+    assert warning_emitted('images.txt', 'nonlocal image URI found: '
                            'http://www.python.org/logo.png')
 
     tree = env.get_doctree('images')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.