We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pyramid.scripting.get_root() triggers AttributeError 
In Pyramid 1.9.1, get_root() triggers an AttributeError on threadlocal_manager:

```
# bin/pshell mything/development.ini 
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help" for more information.

Environment:
  app          The WSGI application.
  registry     Active Pyramid registry.
  request      Active request object.
  root         Root of the default resource tree.
  root_factory Default root factory used to create `root`.

>>> from pyramid.scripting import get_root
>>> x = get_root(app)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/shane/src/pyramidtest/eggs/pyramid-1.9.1-py3.5.egg/pyramid/scripting.py", line 30, in get_root
    app.threadlocal_manager.push(threadlocals)
AttributeError: 'Router' object has no attribute 'threadlocal_manager'
>>>
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pyramid/scripting.py`
Content:
```
1 from pyramid.config import global_registries
2 from pyramid.exceptions import ConfigurationError
3 
4 from pyramid.interfaces import (
5     IRequestFactory,
6     IRootFactory,
7     )
8 from pyramid.request import Request
9 from pyramid.request import apply_request_extensions
10 
11 from pyramid.threadlocal import manager as threadlocal_manager
12 from pyramid.traversal import DefaultRootFactory
13 
14 def get_root(app, request=None):
15     """ Return a tuple composed of ``(root, closer)`` when provided a
16     :term:`router` instance as the ``app`` argument.  The ``root``
17     returned is the application root object.  The ``closer`` returned
18     is a callable (accepting no arguments) that should be called when
19     your scripting application is finished using the root.
20 
21     ``request`` is passed to the :app:`Pyramid` application root
22     factory to compute the root. If ``request`` is None, a default
23     will be constructed using the registry's :term:`Request Factory`
24     via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.
25     """
26     registry = app.registry
27     if request is None:
28         request = _make_request('/', registry)
29     threadlocals = {'registry':registry, 'request':request}
30     app.threadlocal_manager.push(threadlocals)
31     def closer(request=request): # keep request alive via this function default
32         app.threadlocal_manager.pop()
33     root = app.root_factory(request)
34     return root, closer
35 
36 def prepare(request=None, registry=None):
37     """ This function pushes data onto the Pyramid threadlocal stack
38     (request and registry), making those objects 'current'.  It
39     returns a dictionary useful for bootstrapping a Pyramid
40     application in a scripting environment.
41 
42     ``request`` is passed to the :app:`Pyramid` application root
43     factory to compute the root. If ``request`` is None, a default
44     will be constructed using the registry's :term:`Request Factory`
45     via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.
46 
47     If ``registry`` is not supplied, the last registry loaded from
48     :attr:`pyramid.config.global_registries` will be used. If you
49     have loaded more than one :app:`Pyramid` application in the
50     current process, you may not want to use the last registry
51     loaded, thus you can search the ``global_registries`` and supply
52     the appropriate one based on your own criteria.
53 
54     The function returns a dictionary composed of ``root``,
55     ``closer``, ``registry``, ``request`` and ``root_factory``.  The
56     ``root`` returned is the application's root resource object.  The
57     ``closer`` returned is a callable (accepting no arguments) that
58     should be called when your scripting application is finished
59     using the root.  ``registry`` is the resolved registry object.
60     ``request`` is the request object passed or the constructed request
61     if no request is passed.  ``root_factory`` is the root factory used
62     to construct the root.
63 
64     This function may be used as a context manager to call the ``closer``
65     automatically:
66 
67     .. code-block:: python
68 
69        registry = config.registry
70        with prepare(registry) as env:
71            request = env['request']
72            # ...
73 
74     .. versionchanged:: 1.8
75 
76        Added the ability to use the return value as a context manager.
77 
78     """
79     if registry is None:
80         registry = getattr(request, 'registry', global_registries.last)
81     if registry is None:
82         raise ConfigurationError('No valid Pyramid applications could be '
83                                  'found, make sure one has been created '
84                                  'before trying to activate it.')
85     if request is None:
86         request = _make_request('/', registry)
87     # NB: even though _make_request might have already set registry on
88     # request, we reset it in case someone has passed in their own
89     # request.
90     request.registry = registry 
91     threadlocals = {'registry':registry, 'request':request}
92     threadlocal_manager.push(threadlocals)
93     apply_request_extensions(request)
94     def closer():
95         threadlocal_manager.pop()
96     root_factory = registry.queryUtility(IRootFactory,
97                                          default=DefaultRootFactory)
98     root = root_factory(request)
99     if getattr(request, 'context', None) is None:
100         request.context = root
101     return AppEnvironment(
102         root=root,
103         closer=closer,
104         registry=registry,
105         request=request,
106         root_factory=root_factory,
107     )
108 
109 class AppEnvironment(dict):
110     def __enter__(self):
111         return self
112 
113     def __exit__(self, type, value, traceback):
114         self['closer']()
115 
116 def _make_request(path, registry=None):
117     """ Return a :meth:`pyramid.request.Request` object anchored at a
118     given path. The object returned will be generated from the supplied
119     registry's :term:`Request Factory` using the
120     :meth:`pyramid.interfaces.IRequestFactory.blank` method.
121 
122     This request object can be passed to :meth:`pyramid.scripting.get_root`
123     or :meth:`pyramid.scripting.prepare` to initialize an application in
124     preparation for executing a script with a proper environment setup.
125     URLs can then be generated with the object, as well as rendering
126     templates.
127 
128     If ``registry`` is not supplied, the last registry loaded from
129     :attr:`pyramid.config.global_registries` will be used. If you have
130     loaded more than one :app:`Pyramid` application in the current
131     process, you may not want to use the last registry loaded, thus
132     you can search the ``global_registries`` and supply the appropriate
133     one based on your own criteria.
134     """
135     if registry is None:
136         registry = global_registries.last
137     request_factory = registry.queryUtility(IRequestFactory, default=Request)
138     request = request_factory.blank(path)
139     request.registry = registry
140     return request
141
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pyramid/scripting.py b/pyramid/scripting.py
--- a/pyramid/scripting.py
+++ b/pyramid/scripting.py
@@ -8,7 +8,7 @@
 from pyramid.request import Request
 from pyramid.request import apply_request_extensions
 
-from pyramid.threadlocal import manager as threadlocal_manager
+from pyramid.threadlocal import RequestContext
 from pyramid.traversal import DefaultRootFactory
 
 def get_root(app, request=None):
@@ -26,10 +26,11 @@
     registry = app.registry
     if request is None:
         request = _make_request('/', registry)
-    threadlocals = {'registry':registry, 'request':request}
-    app.threadlocal_manager.push(threadlocals)
-    def closer(request=request): # keep request alive via this function default
-        app.threadlocal_manager.pop()
+    request.registry = registry
+    ctx = RequestContext(request)
+    ctx.begin()
+    def closer():
+        ctx.end()
     root = app.root_factory(request)
     return root, closer
 
@@ -87,12 +88,12 @@
     # NB: even though _make_request might have already set registry on
     # request, we reset it in case someone has passed in their own
     # request.
-    request.registry = registry 
-    threadlocals = {'registry':registry, 'request':request}
-    threadlocal_manager.push(threadlocals)
+    request.registry = registry
+    ctx = RequestContext(request)
+    ctx.begin()
     apply_request_extensions(request)
     def closer():
-        threadlocal_manager.pop()
+        ctx.end()
     root_factory = registry.queryUtility(IRootFactory,
                                          default=DefaultRootFactory)
     root = root_factory(request)

{"golden_diff": "diff --git a/pyramid/scripting.py b/pyramid/scripting.py\n--- a/pyramid/scripting.py\n+++ b/pyramid/scripting.py\n@@ -8,7 +8,7 @@\n from pyramid.request import Request\n from pyramid.request import apply_request_extensions\n \n-from pyramid.threadlocal import manager as threadlocal_manager\n+from pyramid.threadlocal import RequestContext\n from pyramid.traversal import DefaultRootFactory\n \n def get_root(app, request=None):\n@@ -26,10 +26,11 @@\n     registry = app.registry\n     if request is None:\n         request = _make_request('/', registry)\n-    threadlocals = {'registry':registry, 'request':request}\n-    app.threadlocal_manager.push(threadlocals)\n-    def closer(request=request): # keep request alive via this function default\n-        app.threadlocal_manager.pop()\n+    request.registry = registry\n+    ctx = RequestContext(request)\n+    ctx.begin()\n+    def closer():\n+        ctx.end()\n     root = app.root_factory(request)\n     return root, closer\n \n@@ -87,12 +88,12 @@\n     # NB: even though _make_request might have already set registry on\n     # request, we reset it in case someone has passed in their own\n     # request.\n-    request.registry = registry \n-    threadlocals = {'registry':registry, 'request':request}\n-    threadlocal_manager.push(threadlocals)\n+    request.registry = registry\n+    ctx = RequestContext(request)\n+    ctx.begin()\n     apply_request_extensions(request)\n     def closer():\n-        threadlocal_manager.pop()\n+        ctx.end()\n     root_factory = registry.queryUtility(IRootFactory,\n                                          default=DefaultRootFactory)\n     root = root_factory(request)\n", "issue": "pyramid.scripting.get_root() triggers AttributeError \nIn Pyramid 1.9.1, get_root() triggers an AttributeError on threadlocal_manager:\r\n\r\n```\r\n# bin/pshell mything/development.ini \r\nPython 3.5.2 (default, Nov 23 2017, 16:37:01) \r\n[GCC 5.4.0 20160609] on linux\r\nType \"help\" for more information.\r\n\r\nEnvironment:\r\n  app          The WSGI application.\r\n  registry     Active Pyramid registry.\r\n  request      Active request object.\r\n  root         Root of the default resource tree.\r\n  root_factory Default root factory used to create `root`.\r\n\r\n>>> from pyramid.scripting import get_root\r\n>>> x = get_root(app)\r\nTraceback (most recent call last):\r\n  File \"<console>\", line 1, in <module>\r\n  File \"/home/shane/src/pyramidtest/eggs/pyramid-1.9.1-py3.5.egg/pyramid/scripting.py\", line 30, in get_root\r\n    app.threadlocal_manager.push(threadlocals)\r\nAttributeError: 'Router' object has no attribute 'threadlocal_manager'\r\n>>>\r\n```\n", "before_files": [{"content": "from pyramid.config import global_registries\nfrom pyramid.exceptions import ConfigurationError\n\nfrom pyramid.interfaces import (\n    IRequestFactory,\n    IRootFactory,\n    )\nfrom pyramid.request import Request\nfrom pyramid.request import apply_request_extensions\n\nfrom pyramid.threadlocal import manager as threadlocal_manager\nfrom pyramid.traversal import DefaultRootFactory\n\ndef get_root(app, request=None):\n    \"\"\" Return a tuple composed of ``(root, closer)`` when provided a\n    :term:`router` instance as the ``app`` argument.  The ``root``\n    returned is the application root object.  The ``closer`` returned\n    is a callable (accepting no arguments) that should be called when\n    your scripting application is finished using the root.\n\n    ``request`` is passed to the :app:`Pyramid` application root\n    factory to compute the root. If ``request`` is None, a default\n    will be constructed using the registry's :term:`Request Factory`\n    via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.\n    \"\"\"\n    registry = app.registry\n    if request is None:\n        request = _make_request('/', registry)\n    threadlocals = {'registry':registry, 'request':request}\n    app.threadlocal_manager.push(threadlocals)\n    def closer(request=request): # keep request alive via this function default\n        app.threadlocal_manager.pop()\n    root = app.root_factory(request)\n    return root, closer\n\ndef prepare(request=None, registry=None):\n    \"\"\" This function pushes data onto the Pyramid threadlocal stack\n    (request and registry), making those objects 'current'.  It\n    returns a dictionary useful for bootstrapping a Pyramid\n    application in a scripting environment.\n\n    ``request`` is passed to the :app:`Pyramid` application root\n    factory to compute the root. If ``request`` is None, a default\n    will be constructed using the registry's :term:`Request Factory`\n    via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.\n\n    If ``registry`` is not supplied, the last registry loaded from\n    :attr:`pyramid.config.global_registries` will be used. If you\n    have loaded more than one :app:`Pyramid` application in the\n    current process, you may not want to use the last registry\n    loaded, thus you can search the ``global_registries`` and supply\n    the appropriate one based on your own criteria.\n\n    The function returns a dictionary composed of ``root``,\n    ``closer``, ``registry``, ``request`` and ``root_factory``.  The\n    ``root`` returned is the application's root resource object.  The\n    ``closer`` returned is a callable (accepting no arguments) that\n    should be called when your scripting application is finished\n    using the root.  ``registry`` is the resolved registry object.\n    ``request`` is the request object passed or the constructed request\n    if no request is passed.  ``root_factory`` is the root factory used\n    to construct the root.\n\n    This function may be used as a context manager to call the ``closer``\n    automatically:\n\n    .. code-block:: python\n\n       registry = config.registry\n       with prepare(registry) as env:\n           request = env['request']\n           # ...\n\n    .. versionchanged:: 1.8\n\n       Added the ability to use the return value as a context manager.\n\n    \"\"\"\n    if registry is None:\n        registry = getattr(request, 'registry', global_registries.last)\n    if registry is None:\n        raise ConfigurationError('No valid Pyramid applications could be '\n                                 'found, make sure one has been created '\n                                 'before trying to activate it.')\n    if request is None:\n        request = _make_request('/', registry)\n    # NB: even though _make_request might have already set registry on\n    # request, we reset it in case someone has passed in their own\n    # request.\n    request.registry = registry \n    threadlocals = {'registry':registry, 'request':request}\n    threadlocal_manager.push(threadlocals)\n    apply_request_extensions(request)\n    def closer():\n        threadlocal_manager.pop()\n    root_factory = registry.queryUtility(IRootFactory,\n                                         default=DefaultRootFactory)\n    root = root_factory(request)\n    if getattr(request, 'context', None) is None:\n        request.context = root\n    return AppEnvironment(\n        root=root,\n        closer=closer,\n        registry=registry,\n        request=request,\n        root_factory=root_factory,\n    )\n\nclass AppEnvironment(dict):\n    def __enter__(self):\n        return self\n\n    def __exit__(self, type, value, traceback):\n        self['closer']()\n\ndef _make_request(path, registry=None):\n    \"\"\" Return a :meth:`pyramid.request.Request` object anchored at a\n    given path. The object returned will be generated from the supplied\n    registry's :term:`Request Factory` using the\n    :meth:`pyramid.interfaces.IRequestFactory.blank` method.\n\n    This request object can be passed to :meth:`pyramid.scripting.get_root`\n    or :meth:`pyramid.scripting.prepare` to initialize an application in\n    preparation for executing a script with a proper environment setup.\n    URLs can then be generated with the object, as well as rendering\n    templates.\n\n    If ``registry`` is not supplied, the last registry loaded from\n    :attr:`pyramid.config.global_registries` will be used. If you have\n    loaded more than one :app:`Pyramid` application in the current\n    process, you may not want to use the last registry loaded, thus\n    you can search the ``global_registries`` and supply the appropriate\n    one based on your own criteria.\n    \"\"\"\n    if registry is None:\n        registry = global_registries.last\n    request_factory = registry.queryUtility(IRequestFactory, default=Request)\n    request = request_factory.blank(path)\n    request.registry = registry\n    return request\n", "path": "pyramid/scripting.py"}], "after_files": [{"content": "from pyramid.config import global_registries\nfrom pyramid.exceptions import ConfigurationError\n\nfrom pyramid.interfaces import (\n    IRequestFactory,\n    IRootFactory,\n    )\nfrom pyramid.request import Request\nfrom pyramid.request import apply_request_extensions\n\nfrom pyramid.threadlocal import RequestContext\nfrom pyramid.traversal import DefaultRootFactory\n\ndef get_root(app, request=None):\n    \"\"\" Return a tuple composed of ``(root, closer)`` when provided a\n    :term:`router` instance as the ``app`` argument.  The ``root``\n    returned is the application root object.  The ``closer`` returned\n    is a callable (accepting no arguments) that should be called when\n    your scripting application is finished using the root.\n\n    ``request`` is passed to the :app:`Pyramid` application root\n    factory to compute the root. If ``request`` is None, a default\n    will be constructed using the registry's :term:`Request Factory`\n    via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.\n    \"\"\"\n    registry = app.registry\n    if request is None:\n        request = _make_request('/', registry)\n    request.registry = registry\n    ctx = RequestContext(request)\n    ctx.begin()\n    def closer():\n        ctx.end()\n    root = app.root_factory(request)\n    return root, closer\n\ndef prepare(request=None, registry=None):\n    \"\"\" This function pushes data onto the Pyramid threadlocal stack\n    (request and registry), making those objects 'current'.  It\n    returns a dictionary useful for bootstrapping a Pyramid\n    application in a scripting environment.\n\n    ``request`` is passed to the :app:`Pyramid` application root\n    factory to compute the root. If ``request`` is None, a default\n    will be constructed using the registry's :term:`Request Factory`\n    via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.\n\n    If ``registry`` is not supplied, the last registry loaded from\n    :attr:`pyramid.config.global_registries` will be used. If you\n    have loaded more than one :app:`Pyramid` application in the\n    current process, you may not want to use the last registry\n    loaded, thus you can search the ``global_registries`` and supply\n    the appropriate one based on your own criteria.\n\n    The function returns a dictionary composed of ``root``,\n    ``closer``, ``registry``, ``request`` and ``root_factory``.  The\n    ``root`` returned is the application's root resource object.  The\n    ``closer`` returned is a callable (accepting no arguments) that\n    should be called when your scripting application is finished\n    using the root.  ``registry`` is the resolved registry object.\n    ``request`` is the request object passed or the constructed request\n    if no request is passed.  ``root_factory`` is the root factory used\n    to construct the root.\n\n    This function may be used as a context manager to call the ``closer``\n    automatically:\n\n    .. code-block:: python\n\n       registry = config.registry\n       with prepare(registry) as env:\n           request = env['request']\n           # ...\n\n    .. versionchanged:: 1.8\n\n       Added the ability to use the return value as a context manager.\n\n    \"\"\"\n    if registry is None:\n        registry = getattr(request, 'registry', global_registries.last)\n    if registry is None:\n        raise ConfigurationError('No valid Pyramid applications could be '\n                                 'found, make sure one has been created '\n                                 'before trying to activate it.')\n    if request is None:\n        request = _make_request('/', registry)\n    # NB: even though _make_request might have already set registry on\n    # request, we reset it in case someone has passed in their own\n    # request.\n    request.registry = registry\n    ctx = RequestContext(request)\n    ctx.begin()\n    apply_request_extensions(request)\n    def closer():\n        ctx.end()\n    root_factory = registry.queryUtility(IRootFactory,\n                                         default=DefaultRootFactory)\n    root = root_factory(request)\n    if getattr(request, 'context', None) is None:\n        request.context = root\n    return AppEnvironment(\n        root=root,\n        closer=closer,\n        registry=registry,\n        request=request,\n        root_factory=root_factory,\n    )\n\nclass AppEnvironment(dict):\n    def __enter__(self):\n        return self\n\n    def __exit__(self, type, value, traceback):\n        self['closer']()\n\ndef _make_request(path, registry=None):\n    \"\"\" Return a :meth:`pyramid.request.Request` object anchored at a\n    given path. The object returned will be generated from the supplied\n    registry's :term:`Request Factory` using the\n    :meth:`pyramid.interfaces.IRequestFactory.blank` method.\n\n    This request object can be passed to :meth:`pyramid.scripting.get_root`\n    or :meth:`pyramid.scripting.prepare` to initialize an application in\n    preparation for executing a script with a proper environment setup.\n    URLs can then be generated with the object, as well as rendering\n    templates.\n\n    If ``registry`` is not supplied, the last registry loaded from\n    :attr:`pyramid.config.global_registries` will be used. If you have\n    loaded more than one :app:`Pyramid` application in the current\n    process, you may not want to use the last registry loaded, thus\n    you can search the ``global_registries`` and supply the appropriate\n    one based on your own criteria.\n    \"\"\"\n    if registry is None:\n        registry = global_registries.last\n    request_factory = registry.queryUtility(IRequestFactory, default=Request)\n    request = request_factory.blank(path)\n    request.registry = registry\n    return request\n", "path": "pyramid/scripting.py"}]}