Repository: microsoft/ptvsd

Issue:
Launching a Python module using `-m` with `ptvsd.enable_attach` in __init__ fails
## Environment data

- PTVSD version: Master
- OS and version: Mac (may not be OS specific)
- Python version (& distribution if applicable, e.g. Anaconda): 3.6
- Using VS Code or Visual Studio:N/A

## Actual behavior

```
Traceback (most recent call last):
  File "/Users/donjayamanne/Desktop/Development/vscode/ptvsd/tests/resources/system_tests/test_exceptions/mymod_attach1/__init__.py", line 4, in <module>
    ptvsd.enable_attach((sys.argv[1], sys.argv[2]))
  File "/Users/donjayamanne/Desktop/Development/vscode/ptvsd/ptvsd/attach_server.py", line 71, in enable_attach
    redirect_output=redirect_output,
  File "/Users/donjayamanne/Desktop/Development/vscode/ptvsd/ptvsd/_remote.py", line 27, in enable_attach
    **kwargs
  File "/Users/donjayamanne/Desktop/Development/vscode/ptvsd/ptvsd/pydevd_hooks.py", line 95, in install
    if __main__ is not pydevd and __main__.__file__ == pydevd.__file__:
AttributeError: module '__main__' has no attribute '__file__'
```

## Expected  behavior

Should not error out

## Steps to reproduce:

* Create a python module
* Add the following code in the `__init__.py`
```python
import sys
import ptvsd
ptvsd.enable_attach(('localhost', 9876))
ptvsd.wait_for_attach()

try:
    raise ArithmeticError('Hello')
except Exception:
    pass
sys.stdout.write('end')
```
* Start the module `python -m xyz`
That's when it goes kaboom


Discussion:
This is not OS Specific. Occurs on Windows as well.

Resolving patch:
diff --git a/ptvsd/pydevd_hooks.py b/ptvsd/pydevd_hooks.py
--- a/ptvsd/pydevd_hooks.py
+++ b/ptvsd/pydevd_hooks.py
@@ -92,7 +92,8 @@ def install(pydevd, address,
     pydevd.start_server = _start_server
     pydevd.start_client = _start_client
     __main__ = sys.modules['__main__']
-    if __main__ is not pydevd and __main__.__file__ == pydevd.__file__:
-        __main__.start_server = _start_server
-        __main__.start_client = _start_client
+    if __main__ is not pydevd:
+        if getattr(__main__, '__file__', None) == pydevd.__file__:
+            __main__.start_server = _start_server
+            __main__.start_client = _start_client
     return daemon


Test patch:
diff --git a/tests/system_tests/__init__.py b/tests/system_tests/__init__.py
--- a/tests/system_tests/__init__.py
+++ b/tests/system_tests/__init__.py
@@ -188,6 +188,9 @@ def pathentry(self):
             self._pathentry.install()
             return self._pathentry
 
+    def enable_verbose(self):
+        DebugAdapter.VERBOSE = True
+
     def write_script(self, name, content):
         return self.workspace.write_python_script(name, content=content)
 
diff --git a/tests/system_tests/test_basic.py b/tests/system_tests/test_basic.py
--- a/tests/system_tests/test_basic.py
+++ b/tests/system_tests/test_basic.py
@@ -219,9 +219,10 @@ def test_without_output(self):
                 starttype='attach'))
 
 
-@unittest.skip('Needs fixing #545')
-class PTVSDAttachModuleTests(BasicTests):  # noqa
+class PTVSDAttachModuleTests(BasicTests):
+
     def test_with_output(self):
+        #self.enable_verbose()
         module_name = 'mymod_attach1'
         cwd = os.path.join(TEST_FILES_DIR, 'test_output')
         env = {"PYTHONPATH": cwd}