We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
_monkeypatches.py:  'NoneType' object has no attribute 'startswith'
Did not try yet to come up with minimal example to demonstrate this issue but it is reproducible for me:

```
$> datalad --dbg crawl
Traceback (most recent call last):
  File "/home/yoh/proj/datalad/datalad/venv-tests/bin/datalad", line 9, in <module>
    load_entry_point('datalad==0.1.dev0', 'console_scripts', 'datalad')()
  File "/home/yoh/proj/datalad/datalad/datalad/cmdline/main.py", line 199, in main
    cmdlineargs.func(cmdlineargs)
  File "/home/yoh/proj/datalad/datalad/datalad/interface/base.py", line 151, in call_from_parser
    return self(**kwargs)
  File "/home/yoh/proj/datalad/datalad/datalad/interface/crawl.py", line 44, in __call__
    from datalad.crawler.pipeline import load_pipeline_from_config, get_pipeline_config_path
  File "/home/yoh/proj/datalad/datalad/datalad/crawler/pipeline.py", line 21, in <module>
    from .newmain import lgr
  File "/home/yoh/proj/datalad/datalad/datalad/crawler/newmain.py", line 21, in <module>
    from .nodes.matches import *
  File "/home/yoh/proj/datalad/datalad/datalad/crawler/nodes/matches.py", line 18, in <module>
    from scrapy.selector import Selector
  File "/home/yoh/proj/datalad/datalad/venv-tests/local/lib/python2.7/site-packages/scrapy/__init__.py", line 27, in <module>
    from . import _monkeypatches
  File "/home/yoh/proj/datalad/datalad/venv-tests/local/lib/python2.7/site-packages/scrapy/_monkeypatches.py", line 24, in <module>
    and getattr(v, '__module__', '').startswith('twisted'):
AttributeError: 'NoneType' object has no attribute 'startswith'
()
> /home/yoh/proj/datalad/datalad/venv-tests/local/lib/python2.7/site-packages/scrapy/_monkeypatches.py(24)<module>()
-> and getattr(v, '__module__', '').startswith('twisted'):
(Pdb) l
 19     # to prevent bugs like Twisted#7989 while serializing requests
 20     import twisted.persisted.styles  # NOQA
 21     # Remove only entries with twisted serializers for non-twisted types.
 22     for k, v in frozenset(copyreg.dispatch_table.items()):
 23         if not getattr(k, '__module__', '').startswith('twisted') \
 24  ->             and getattr(v, '__module__', '').startswith('twisted'):
 25             copyreg.dispatch_table.pop(k)
[EOF]
(Pdb) p k
None
(Pdb) p v
None
(Pdb) p copyreg
None
```

not sure it came to it but the issue is (if I pdb before this madness happens):

```
(Pdb) p getattr(k, '__module__', '')
'__builtin__'
(Pdb) p getattr(v, '__module__', '')
None
(Pdb) p v
<function mpq_reducer at 0x7f474bb4ab90>
(Pdb) p v.__module__
None
(Pdb) p k, v
(<type 'mpq'>, <function mpq_reducer at 0x7f474bb4ab90>)
```

so assigned `__module__` is None.  As a quick resolution wrapped into str() call to assure str there

```
and str(getattr(v, '__module__', '')).startswith('twisted'):
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scrapy/_monkeypatches.py`
Content:
```
1 import sys
2 from six.moves import copyreg
3 
4 if sys.version_info[0] == 2:
5     from urlparse import urlparse
6 
7     # workaround for http://bugs.python.org/issue7904 - Python < 2.7
8     if urlparse('s3://bucket/key').netloc != 'bucket':
9         from urlparse import uses_netloc
10         uses_netloc.append('s3')
11 
12     # workaround for http://bugs.python.org/issue9374 - Python < 2.7.4
13     if urlparse('s3://bucket/key?key=value').query != 'key=value':
14         from urlparse import uses_query
15         uses_query.append('s3')
16 
17 
18 # Undo what Twisted's perspective broker adds to pickle register
19 # to prevent bugs like Twisted#7989 while serializing requests
20 import twisted.persisted.styles  # NOQA
21 # Remove only entries with twisted serializers for non-twisted types.
22 for k, v in frozenset(copyreg.dispatch_table.items()):
23     if not getattr(k, '__module__', '').startswith('twisted') \
24             and getattr(v, '__module__', '').startswith('twisted'):
25         copyreg.dispatch_table.pop(k)
26
```
--- 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/scrapy/_monkeypatches.py b/scrapy/_monkeypatches.py
--- a/scrapy/_monkeypatches.py
+++ b/scrapy/_monkeypatches.py
@@ -20,6 +20,6 @@
 import twisted.persisted.styles  # NOQA
 # Remove only entries with twisted serializers for non-twisted types.
 for k, v in frozenset(copyreg.dispatch_table.items()):
-    if not getattr(k, '__module__', '').startswith('twisted') \
-            and getattr(v, '__module__', '').startswith('twisted'):
+    if not str(getattr(k, '__module__', '')).startswith('twisted') \
+            and str(getattr(v, '__module__', '')).startswith('twisted'):
         copyreg.dispatch_table.pop(k)

{"golden_diff": "diff --git a/scrapy/_monkeypatches.py b/scrapy/_monkeypatches.py\n--- a/scrapy/_monkeypatches.py\n+++ b/scrapy/_monkeypatches.py\n@@ -20,6 +20,6 @@\n import twisted.persisted.styles  # NOQA\n # Remove only entries with twisted serializers for non-twisted types.\n for k, v in frozenset(copyreg.dispatch_table.items()):\n-    if not getattr(k, '__module__', '').startswith('twisted') \\\n-            and getattr(v, '__module__', '').startswith('twisted'):\n+    if not str(getattr(k, '__module__', '')).startswith('twisted') \\\n+            and str(getattr(v, '__module__', '')).startswith('twisted'):\n         copyreg.dispatch_table.pop(k)\n", "issue": "_monkeypatches.py:  'NoneType' object has no attribute 'startswith'\nDid not try yet to come up with minimal example to demonstrate this issue but it is reproducible for me:\n\n```\n$> datalad --dbg crawl\nTraceback (most recent call last):\n  File \"/home/yoh/proj/datalad/datalad/venv-tests/bin/datalad\", line 9, in <module>\n    load_entry_point('datalad==0.1.dev0', 'console_scripts', 'datalad')()\n  File \"/home/yoh/proj/datalad/datalad/datalad/cmdline/main.py\", line 199, in main\n    cmdlineargs.func(cmdlineargs)\n  File \"/home/yoh/proj/datalad/datalad/datalad/interface/base.py\", line 151, in call_from_parser\n    return self(**kwargs)\n  File \"/home/yoh/proj/datalad/datalad/datalad/interface/crawl.py\", line 44, in __call__\n    from datalad.crawler.pipeline import load_pipeline_from_config, get_pipeline_config_path\n  File \"/home/yoh/proj/datalad/datalad/datalad/crawler/pipeline.py\", line 21, in <module>\n    from .newmain import lgr\n  File \"/home/yoh/proj/datalad/datalad/datalad/crawler/newmain.py\", line 21, in <module>\n    from .nodes.matches import *\n  File \"/home/yoh/proj/datalad/datalad/datalad/crawler/nodes/matches.py\", line 18, in <module>\n    from scrapy.selector import Selector\n  File \"/home/yoh/proj/datalad/datalad/venv-tests/local/lib/python2.7/site-packages/scrapy/__init__.py\", line 27, in <module>\n    from . import _monkeypatches\n  File \"/home/yoh/proj/datalad/datalad/venv-tests/local/lib/python2.7/site-packages/scrapy/_monkeypatches.py\", line 24, in <module>\n    and getattr(v, '__module__', '').startswith('twisted'):\nAttributeError: 'NoneType' object has no attribute 'startswith'\n()\n> /home/yoh/proj/datalad/datalad/venv-tests/local/lib/python2.7/site-packages/scrapy/_monkeypatches.py(24)<module>()\n-> and getattr(v, '__module__', '').startswith('twisted'):\n(Pdb) l\n 19     # to prevent bugs like Twisted#7989 while serializing requests\n 20     import twisted.persisted.styles  # NOQA\n 21     # Remove only entries with twisted serializers for non-twisted types.\n 22     for k, v in frozenset(copyreg.dispatch_table.items()):\n 23         if not getattr(k, '__module__', '').startswith('twisted') \\\n 24  ->             and getattr(v, '__module__', '').startswith('twisted'):\n 25             copyreg.dispatch_table.pop(k)\n[EOF]\n(Pdb) p k\nNone\n(Pdb) p v\nNone\n(Pdb) p copyreg\nNone\n```\n\nnot sure it came to it but the issue is (if I pdb before this madness happens):\n\n```\n(Pdb) p getattr(k, '__module__', '')\n'__builtin__'\n(Pdb) p getattr(v, '__module__', '')\nNone\n(Pdb) p v\n<function mpq_reducer at 0x7f474bb4ab90>\n(Pdb) p v.__module__\nNone\n(Pdb) p k, v\n(<type 'mpq'>, <function mpq_reducer at 0x7f474bb4ab90>)\n```\n\nso assigned `__module__` is None.  As a quick resolution wrapped into str() call to assure str there\n\n```\nand str(getattr(v, '__module__', '')).startswith('twisted'):\n```\n\n", "before_files": [{"content": "import sys\nfrom six.moves import copyreg\n\nif sys.version_info[0] == 2:\n    from urlparse import urlparse\n\n    # workaround for http://bugs.python.org/issue7904 - Python < 2.7\n    if urlparse('s3://bucket/key').netloc != 'bucket':\n        from urlparse import uses_netloc\n        uses_netloc.append('s3')\n\n    # workaround for http://bugs.python.org/issue9374 - Python < 2.7.4\n    if urlparse('s3://bucket/key?key=value').query != 'key=value':\n        from urlparse import uses_query\n        uses_query.append('s3')\n\n\n# Undo what Twisted's perspective broker adds to pickle register\n# to prevent bugs like Twisted#7989 while serializing requests\nimport twisted.persisted.styles  # NOQA\n# Remove only entries with twisted serializers for non-twisted types.\nfor k, v in frozenset(copyreg.dispatch_table.items()):\n    if not getattr(k, '__module__', '').startswith('twisted') \\\n            and getattr(v, '__module__', '').startswith('twisted'):\n        copyreg.dispatch_table.pop(k)\n", "path": "scrapy/_monkeypatches.py"}], "after_files": [{"content": "import sys\nfrom six.moves import copyreg\n\nif sys.version_info[0] == 2:\n    from urlparse import urlparse\n\n    # workaround for http://bugs.python.org/issue7904 - Python < 2.7\n    if urlparse('s3://bucket/key').netloc != 'bucket':\n        from urlparse import uses_netloc\n        uses_netloc.append('s3')\n\n    # workaround for http://bugs.python.org/issue9374 - Python < 2.7.4\n    if urlparse('s3://bucket/key?key=value').query != 'key=value':\n        from urlparse import uses_query\n        uses_query.append('s3')\n\n\n# Undo what Twisted's perspective broker adds to pickle register\n# to prevent bugs like Twisted#7989 while serializing requests\nimport twisted.persisted.styles  # NOQA\n# Remove only entries with twisted serializers for non-twisted types.\nfor k, v in frozenset(copyreg.dispatch_table.items()):\n    if not str(getattr(k, '__module__', '')).startswith('twisted') \\\n            and str(getattr(v, '__module__', '')).startswith('twisted'):\n        copyreg.dispatch_table.pop(k)\n", "path": "scrapy/_monkeypatches.py"}]}