Repository: pyinstaller/pyinstaller

Issue:
Failure in 'Looking for ctypes DLLs' when specifying 'ctypes' as a hidden import.
With `hiddenimports=["ctypes"]`, I get the following exception:
```
24 INFO: PyInstaller: 3.4
24 INFO: Python: 3.7.1
25 INFO: Platform: Linux-4.18.16-arch1-1-ARCH-x86_64-with-arch
26 INFO: UPX is not available.
26 INFO: Extending PYTHONPATH with paths
['…/snakes.py']
26 INFO: checking Analysis
28 INFO: Building because hiddenimports changed
28 INFO: Initializing module dependency graph...
31 INFO: Initializing module graph hooks...
31 INFO: Analyzing base_library.zip ...
2372 INFO: Analyzing hidden import 'ctypes'
2415 INFO: running Analysis Analysis-00.toc
2441 INFO: Caching module hooks...
2446 INFO: Analyzing snakes.py
2474 INFO: Loading module hooks...
2474 INFO: Loading module hook "hook-xml.py"...
2654 INFO: Loading module hook "hook-pydoc.py"...
2654 INFO: Loading module hook "hook-encodings.py"...
2699 INFO: Looking for ctypes DLLs
Traceback (most recent call last):
  File "/usr/bin/pyinstaller", line 11, in <module>
    load_entry_point('PyInstaller==3.4', 'console_scripts', 'pyinstaller')()
  File "/usr/lib/python3.7/site-packages/PyInstaller/__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "/usr/lib/python3.7/site-packages/PyInstaller/__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "/usr/lib/python3.7/site-packages/PyInstaller/building/build_main.py", line 838, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "/usr/lib/python3.7/site-packages/PyInstaller/building/build_main.py", line 784, in build
    exec(text, spec_namespace)
  File "<string>", line 21, in <module>
  File "/usr/lib/python3.7/site-packages/PyInstaller/building/build_main.py", line 241, in __init__
    self.__postinit__()
  File "/usr/lib/python3.7/site-packages/PyInstaller/building/datastruct.py", line 158, in __postinit__
    self.assemble()
  File "/usr/lib/python3.7/site-packages/PyInstaller/building/build_main.py", line 531, in assemble
    ctypes_code_objs = self.graph.get_co_using_ctypes()  # dict like:  {'module1': code_obj, 'module2': code_obj}
  File "/usr/lib/python3.7/site-packages/PyInstaller/depend/analysis.py", line 554, in get_co_using_ctypes
    r_ident =  r.identifier
AttributeError: 'NoneType' object has no attribute 'identifier'
```
On the other hand, when I needlessly `import ctypes` in my main script, everything works like a charm.

I want to set `ctypes` as a hidden import because it is required by `bearlibterminal`, a Python dependency that I add manually to work around #3584.


Discussion:
Perhaps this is related to: https://bitbucket.org/ronaldoussoren/altgraph/pull-requests/4/default/diff
> Perhaps this is related to: https://bitbucket.org/ronaldoussoren/altgraph/pull-requests/4/default/diff

This link is not public. Can't access it.
I'm having this same issue. Project on python 2, getting this exception when importing this module. Anyone knows some workaround ?

Resolving patch:
diff --git a/PyInstaller/depend/analysis.py b/PyInstaller/depend/analysis.py
--- a/PyInstaller/depend/analysis.py
+++ b/PyInstaller/depend/analysis.py
@@ -741,6 +741,11 @@ def get_co_using_ctypes(self):
         if node:
             referers = self.getReferers(node)
             for r in referers:
+                # Under python 3.7 and earlier, if ctypes is added to
+                # hidden imports, one of referers ends up being None,
+                # causing #3825. Work around it.
+                if r is None:
+                    continue
                 r_ident =  r.identifier
                 # Ensure that modulegraph objects has attribute 'code'.
                 if type(r).__name__ in pure_python_module_types:


Test patch: