Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python exception IOError : Bad file descriptor under Windows #313

Closed
ghost opened this issue Jul 13, 2018 · 4 comments
Closed

Python exception IOError : Bad file descriptor under Windows #313

ghost opened this issue Jul 13, 2018 · 4 comments

Comments

@ghost
Copy link

@ghost ghost commented Jul 13, 2018

Hi,

With latest 1.9 CRAN or 1.9.0.9001 Github version of reticulate, every time repl_python() is used (manually via R Console, or automatically via RStudio (run selection)), reticulate package throw the following Python exception :

> reticulate::repl_python()
Python 2.7.14 (E:\Mes_Documents\Programs\RStudio-1.2.792\bin\rsession.exe) Reticulate 1.9.0.9001 REPL -- A Python interpreter in R.
>>> print 'ok ?'

Error in py_call_impl(callable, dots$args, dots$keywords) :
   IOError: [Errno 9] Bad file descriptor

Normally Bad file descriptor is thrown when trying to read / write on an manually closed file object.

Under Linux, is ok, there is no error.

``

Small bug, but rather annoying.

Thx, in advance.

Cyril.


Here is my environment :

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                   LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reticulate_1.9.0.9001

loaded via a namespace (and not attached):
[1] compiler_3.5.1  Matrix_1.2-14   tools_3.5.1     Rcpp_0.12.17    grid_3.5.1      jsonlite_1.5   
[7] lattice_0.20-35

And :

> py_config()
python:         E:\MES_DO~1\Programs\Python27\python.exe
libpython:      E:/MES_DO~1/Programs/Python27/python27.dll
pythonhome:     E:\Mes_Documents\Programs\Python27
version:        2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]
Architecture:   64bit
numpy:          E:\Mes_Documents\Programs\Python27\lib\site-packages\numpy
numpy_version:  1.14.3

Note : perhaps this is related to this previously fixed bug :
rstudio/keras#33 (comment)

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Jul 13, 2018

@kevinushey Could you take a look at this? Perhaps a windows-specific issue?

@kevinushey
Copy link
Collaborator

@kevinushey kevinushey commented Jul 13, 2018

Looks like this is indeed an issue with Python 2.7; for some reason only manifesting on Windows. From https://stackoverflow.com/questions/4230855/why-am-i-getting-ioerror-9-bad-file-descriptor-error-while-making-print-st:

You can't print because sys.stdout is not available when not running as a console session.

What confuses me is that reticulate normally attempts to remap stdout / stderr to R's own output handlers, e.g.

reticulate/R/output.R

Lines 3 to 10 in 8b5d928

remap_output_streams <- function() {
output <- import("rpytools.output")
output$remap_output_streams(
write_stdout,
write_stderr,
tty = interactive() || isatty(stdout())
)
}

However, this doesn't happen as Python initialize its stdout and stderr connections, and it seems to always do that:

def remap_output_streams(r_stdout, r_stderr, tty):
if (sys.stdout is None):
sys.stdout = OutputRemap(sys.stdout, r_stdout, tty)
if (sys.stderr is None):
sys.stderr = OutputRemap(sys.stderr, r_stderr, tty)

@jjallaire, it seems like the easiest fix here would be to unconditionally re-wire the stdout and stderr streams on Windows when Python 2.7 is being used. What do you think?

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Jul 13, 2018

Yeah, I think we could do that. The reason I'm keeping it carefully scoped now is that I don't know if the remapping leads to other side effects and I want to limit it's impact in the case that it does. However, it seems like we definitely need it here. I'll merge your PR (could you add a NEWS item for it?)

@ghost
Copy link
Author

@ghost ghost commented Jul 16, 2018

I've just tested latest GitHub commit : under Windows & Python 2.7, reticulate works now like a charm. Thx for the fix ... and long life to reticulate ! ... :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants