Repository: Theano/Theano

Issue:
Theano deprecation warning
I have started running my code with warnings as errors.  Theano trips the deprecation warning:

```
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/configparser.py", line 1209, in __init__
    DeprecationWarning, stacklevel=2
DeprecationWarning: The SafeConfigParser class has been renamed to ConfigParser in Python 3.2. This alias will be removed in future versions. Use ConfigParser directly instead.
```

Please use:

```
theano_cfg =  (ConfigParser.ConfigParser if six.PY3 else ConfigParser.SafeConfigParser)(…
```



Discussion:
Can gpu make a pr for this? Did you saw other such errors?

Le 6 août 2016 21:51, "Neil" notifications@github.com a écrit :

> I have started running my code with warnings as errors. Theano trips the
> deprecation warning:
> 
>   File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/configparser.py", line 1209, in **init**
>     DeprecationWarning, stacklevel=2
> DeprecationWarning: The SafeConfigParser class has been renamed to ConfigParser in Python 3.2. This alias will be removed in future versions. Use ConfigParser directly instead.
> 
> Please use:
> 
> theano_cfg = (ConfigParser.ConfigParser if six.PY3 else ConfigParse.SafeConfigParser)(…
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> https://github.com/Theano/Theano/issues/4832, or mute the thread
> https://github.com/notifications/unsubscribe-auth/AALC-8qPJUsN0Nvlf0W9rYLdHZgY1xAJks5qdToSgaJpZM4JeZ7s
> .

Sure, I will make a pull request soon.  I think I disabled warnings again for now.  To enable warnings do:

```
import warnings
warnings.simplefilter("error")
```

I don't see this warning with python 3.3 and 3.5 and Theano dev version. We don't support python 3.2. How do you reproduce this?

```
import scipy.special
import warnings
warnings.simplefilter("error")
import theano
```

will trip it in 3.5.

(Peripheral issue https://github.com/scipy/scipy/issues/6587)

@NeilGirdhar  - May I submit a PR for this?

@medakk yes please :)

I'm reopening as now there is a new error that appear after the fix of the first:

```
>>> import scipy.special
>>> import warnings
>>> warnings.simplefilter("error")
>>> import theano
Exception ignored in: <_io.FileIO name='/dev/null' mode='wb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedWriter name='/dev/null'>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/u/bastienf/repos/Theano/theano/__init__.py", line 83, in <module>
    from theano.scan_module import scan, map, reduce, foldl, foldr, clone
  File "/u/bastienf/repos/Theano/theano/scan_module/__init__.py", line 41, in <module>
    from theano.scan_module import scan_opt
  File "/u/bastienf/repos/Theano/theano/scan_module/scan_opt.py", line 60, in <module>
    from theano import tensor, scalar
  File "/u/bastienf/repos/Theano/theano/tensor/__init__.py", line 8, in <module>
    from theano.tensor.basic import *
  File "/u/bastienf/repos/Theano/theano/tensor/basic.py", line 19, in <module>
    from theano.tensor import elemwise
  File "/u/bastienf/repos/Theano/theano/tensor/elemwise.py", line 13, in <module>
    from theano import scalar
  File "/u/bastienf/repos/Theano/theano/scalar/__init__.py", line 5, in <module>
    from .basic_scipy import *
  File "/u/bastienf/repos/Theano/theano/scalar/basic_scipy.py", line 17, in <module>
    import scipy.stats
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/stats/__init__.py", line 321, in <module>
    from .stats import *
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/stats/stats.py", line 184, in <module>
    from . import distributions
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/stats/distributions.py", line 10, in <module>
    from ._distn_infrastructure import (entropy, rv_discrete, rv_continuous,
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/stats/_distn_infrastructure.py", line 24, in <module>
    from scipy import optimize
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/optimize/__init__.py", line 222, in <module>
    from ._minimize import *
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 30, in <module>
    from .lbfgsb import _minimize_lbfgsb
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/optimize/lbfgsb.py", line 44, in <module>
    from scipy.sparse.linalg import LinearOperator
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/sparse/linalg/__init__.py", line 109, in <module>
    from .isolve import *
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/sparse/linalg/isolve/__init__.py", line 6, in <module>
    from .iterative import *
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/sparse/linalg/isolve/iterative.py", line 78, in <module>
    @decorator
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/site-packages/scipy/_lib/decorator.py", line 205, in decorator
    first = inspect.getargspec(caller)[0][0]  # first arg
  File "/Tmp/lisa/os_v5/anaconda/envs/py35/lib/python3.5/inspect.py", line 1041, in getargspec
    stacklevel=2)
DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead

```

```
DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
```

This is a scipy issue, and it has been fixed([scipy/scipy/pull/5473](https://github.com/scipy/scipy/pull/5473))

<br />

```
ResourceWarning: unclosed file <_io.BufferedWriter name='/dev/null'>
```

The ResourceWarning is in the `output_subprocess_Popen` function in [theano/misc/windows.py](https://github.com/Theano/Theano/blob/65cff73129f3971138b8b60f1e46a49f53231bf4/theano/misc/windows.py#L68). If we keep track of whether or not os.devnull was opened by the function, we can properly close it before returning from the function. Like this:

```
def output_subprocess_Popen(command, **params):
    """
    Calls subprocess_Popen, returning the output, error and exit code
    in a tuple.
    """
    if 'stdout' in params or 'stderr' in params:
        raise TypeError("don't use stderr or stdout with output_subprocess_Popen")
    # stdin to devnull is a workaround for a crash in a weird Windows
    # environement where sys.stdin was None
    null = None
    if not hasattr(params, 'stdin'):
        null = open(os.devnull, 'wb')
        params['stdin'] = null
    params['stdout'] = subprocess.PIPE
    params['stderr'] = subprocess.PIPE
    p = subprocess_Popen(command, **params)
    # we need to use communicate to make sure we don't deadlock around
    # the stdour/stderr pipe.
    out = p.communicate()
    # if we had previously opened os.devnull for stdin, close it
    if null is not None:
        null.close()
    return out + (p.returncode,)
```

Or is there a better way to resolve this?

Unrelated, but there are some spelling mistakes in the comments in that file; to correct those should I open a separate pull request?

As this change the same part of the code that the fix, they could be in the
same PR.

I like your fix. Do you want to make a PR?

On Tue, Sep 27, 2016 at 11:34 AM, Karthik Karanth notifications@github.com
wrote:

> Unrelated, but there are some spelling mistakes in the comments for that
> file; if I wanted to correct those should I open a separate pull request?
> 
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub
> https://github.com/Theano/Theano/issues/4832#issuecomment-249902372, or mute
> the thread
> https://github.com/notifications/unsubscribe-auth/AALC-0SrPbnetgVBPhUHeIA3LXnhbGBOks5quTeFgaJpZM4JeZ7s
> .

Yes, I'll make a PR.


Resolving patch:
diff --git a/theano/misc/windows.py b/theano/misc/windows.py
--- a/theano/misc/windows.py
+++ b/theano/misc/windows.py
@@ -43,7 +43,7 @@ def subprocess_Popen(command, **params):
         proc = subprocess.Popen(command, startupinfo=startupinfo, **params)
     finally:
         if stdin is not None:
-            del stdin
+            stdin.close()
     return proc
 
 
@@ -72,15 +72,10 @@ def output_subprocess_Popen(command, **params):
     """
     if 'stdout' in params or 'stderr' in params:
         raise TypeError("don't use stderr or stdout with output_subprocess_Popen")
-    # stdin to devnull is a workaround for a crash in a weird Windows
-    # environement where sys.stdin was None
-    if not hasattr(params, 'stdin'):
-        null = open(os.devnull, 'wb')
-        params['stdin'] = null
     params['stdout'] = subprocess.PIPE
     params['stderr'] = subprocess.PIPE
     p = subprocess_Popen(command, **params)
     # we need to use communicate to make sure we don't deadlock around
-    # the stdour/stderr pipe.
+    # the stdout/stderr pipe.
     out = p.communicate()
     return out + (p.returncode,)


Test patch: