Repository: sphinx-doc/sphinx

Issue:
collectors/toctree.py reports TypeError: 'NoneType' object is not iterable
### Problem
```
sphinx-build -b html -W -d target/doctrees   src/main/sphinx target/html
Running Sphinx v1.7.5
loading pickled environment... not yet created
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 31 source files that are out of date
updating environment: 31 added, 0 changed, 0 removed
reading sources... [100%] upm
looking for now-outdated files...
Exception occurred:
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 172, in _walk_toc
    subnode[0]['secnumber'] = list(number)
TypeError: 'NoneType' object is not iterable
The full traceback has been saved in /var/folders/df/pyp5z10150q4trvmxp24jk4w0000gn/T/sphinx-err-os_3oQ.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!
make: *** [html] Error 2
```

#### Procedure to reproduce the problem


#### Error logs / results
```
# Sphinx version: 1.7.5
# Python version: 2.7.15 (CPython)
# Docutils version: 0.14
# Jinja2 version: 2.10
# Last messages:
#   reading sources... [ 77%] hadoop-server
#   reading sources... [ 80%] hive
#   reading sources... [ 83%] home
#   reading sources... [ 87%] important
#   reading sources... [ 90%] index
#   reading sources... [ 93%] media-chooser
#   reading sources... [ 96%] resource-list
#   reading sources... [100%] upm
#
#   looking for now-outdated files...
# Loaded extensions:
#   download (unknown version) from /Users/jumping.qu/Downloads/deploy-docs/dmp-deploy-docs/src/main/sphinx/ext/download.py
#   alabaster (0.7.10) from /usr/local/lib/python2.7/site-packages/alabaster/__init__.pyc
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/sphinx/cmdline.py", line 304, in main
    app.build(args.force_all, filenames)
  File "/usr/local/lib/python2.7/site-packages/sphinx/application.py", line 331, in build
    self.builder.build_update()
  File "/usr/local/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 342, in build_update
    'out of date' % len(to_build))
  File "/usr/local/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 359, in build
    for docname in self.env.check_dependents(self.app, updated_docnames):
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/__init__.py", line 620, in check_dependents
    for docnames in app.emit('env-get-updated', self):
  File "/usr/local/lib/python2.7/site-packages/sphinx/application.py", line 444, in emit
    return self.events.emit(event, self, *args)
  File "/usr/local/lib/python2.7/site-packages/sphinx/events.py", line 79, in emit
    results.append(callback(*args))
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 135, in get_updated_docs
    return self.assign_section_numbers(env) + self.assign_figure_numbers(env)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 206, in assign_section_numbers
    _walk_toctree(toctreenode, depth)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 194, in _walk_toctree
    env.titles.get(ref))
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 157, in _walk_toc
    _walk_toc(subnode, secnums, depth, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 153, in _walk_toc
    _walk_toc(subnode, secnums, depth - 1, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 177, in _walk_toc
    _walk_toctree(subnode, depth)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 194, in _walk_toctree
    env.titles.get(ref))
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 157, in _walk_toc
    _walk_toc(subnode, secnums, depth, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 153, in _walk_toc
    _walk_toc(subnode, secnums, depth - 1, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 177, in _walk_toc
    _walk_toctree(subnode, depth)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 194, in _walk_toctree
    env.titles.get(ref))
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 157, in _walk_toc
    _walk_toc(subnode, secnums, depth, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 153, in _walk_toc
    _walk_toc(subnode, secnums, depth - 1, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 157, in _walk_toc
    _walk_toc(subnode, secnums, depth, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 153, in _walk_toc
    _walk_toc(subnode, secnums, depth - 1, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 157, in _walk_toc
    _walk_toc(subnode, secnums, depth, titlenode)
  File "/usr/local/lib/python2.7/site-packages/sphinx/environment/collectors/toctree.py", line 172, in _walk_toc
    subnode[0]['secnumber'] = list(number)
TypeError: 'NoneType' object is not iterable
```

#### Expected results


### Reproducible project / your project
- <link to your project, or attach zipped small project sample>

### Environment info
- OS: Mac 10.13.4
- Python version: 2.7.15 (CPython)
- Sphinx version: 1.7.5


Discussion:

Patch:
diff --git a/CHANGES b/CHANGES
index 1b25e4b37..7db859da4 100644
--- a/CHANGES
+++ b/CHANGES
@@ -21,6 +21,7 @@ Bugs fixed
 * #5016: crashed when recommonmark.AutoStrictify is enabled
 * #5022: latex: crashed with docutils package provided by Debian/Ubuntu
 * #5009: latex: a label for table is vanished if table does not have a caption
+* #5048: crashed with numbered toctree
 
 Testing
 --------
diff --git a/sphinx/environment/collectors/toctree.py b/sphinx/environment/collectors/toctree.py
index c8b00c900..35f19b18a 100644
--- a/sphinx/environment/collectors/toctree.py
+++ b/sphinx/environment/collectors/toctree.py
@@ -165,13 +165,14 @@ def _walk_toc(node, secnums, depth, titlenode=None):
                 elif isinstance(subnode, addnodes.compact_paragraph):
                     numstack[-1] += 1
                     if depth > 0:
-                        number = tuple(numstack)
+                        number = list(numstack)
+                        secnums[subnode[0]['anchorname']] = tuple(numstack)
                     else:
                         number = None
-                    secnums[subnode[0]['anchorname']] = number
-                    subnode[0]['secnumber'] = list(number)
+                        secnums[subnode[0]['anchorname']] = None
+                    subnode[0]['secnumber'] = number
                     if titlenode:
-                        titlenode['secnumber'] = list(number)
+                        titlenode['secnumber'] = number
                         titlenode = None
                 elif isinstance(subnode, addnodes.toctree):
                     _walk_toctree(subnode, depth)


Test patch:
diff --git a/tests/test_toctree.py b/tests/test_toctree.py
index 42ec0ce89..e37862ae0 100644
--- a/tests/test_toctree.py
+++ b/tests/test_toctree.py
@@ -8,6 +8,8 @@
     :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
+import re
+
 import pytest
 
 
@@ -35,3 +37,12 @@ def test_singlehtml_toctree(app, status, warning):
         app.builder._get_local_toctree('index')
     except AttributeError:
         pytest.fail('Unexpected AttributeError in app.builder.fix_refuris')
+
+
+@pytest.mark.sphinx(testroot='toctree', srcdir="numbered-toctree")
+def test_numbered_toctree(app, status, warning):
+    # give argument to :numbered: option
+    index = (app.srcdir / 'index.rst').text()
+    index = re.sub(':numbered:.*', ':numbered: 1', index)
+    (app.srcdir / 'index.rst').write_text(index, encoding='utf-8')
+    app.builder.build_all()