Hi Cameron,

  While you are waiting for an answer about the proper way to silence errors, I 
can give you a work-around which will help with the metaphorical reams of 
teletype paper you are printing out.

However, it is a very crude solution. Basically, close the C/C++ stderr file 
descriptor, and tell Python to route its error messages to a new stderr.

# Copy the stderr descriptor
new_fd = os.dup(2)
# Close the original stderr
os.close(2)
# Tell Python about the new stderr
sys.__stderr__ = sys.stderr = os.fdopen(new_fd, "w", 0)


Here it is in context:

import os
import sys
from rdkit import Chem

print("Error?")
Chem.MolFromSmiles("Q") # error message

# Copy the stderr descriptor
new_fd = os.dup(2)
# Close the original stderr
os.close(2)
# Tell Python about the new stderr
sys.__stderr__ = sys.stderr = os.fdopen(new_fd, "w", 0)

print("Error again?")
Chem.MolFromSmiles("Q")  # No warning or error message.

This assumes that neither RDKit nor some other code at the C level write 
directly to file descriptor 2 assuming it's stderr. Otherwise there's a problem 
if you ever open up a new file and it's assigned fd 2. If you *really* want to 
be sure that C/C++ code doesn't write to stderr, and the above doesn't work, 
and you are on a Unix-like system, then add the following after the 
"sys.__stderr__ = .." line:

# Reroute the original descriptor number 2 to /dev/null
fd2 = os.open("/dev/null", os.O_WRONLY)
if fd2 != 2:
    raise SystemExit("Unable to reroute file descriptor 2: %d\n" % (fd2,))


                                Andrew
                                da...@dalkescientific.com



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to