rpdb icon indicating copy to clipboard operation
rpdb copied to clipboard

Overriding sys.stdin causes errors in processes that actually uses attributes of sys.stdin

Open MarounMaroun opened this issue 9 years ago • 14 comments

In one of my projects I used sys.stdin.encoding, but since in this project we have:

sys.stdout = sys.stdin = handle

I got an exception during runtime.

Can we have a default values for that attribute (or even have the ability to pass its attribute in the constructor)?

(I can submit a PR if it's relevant)

MarounMaroun avatar Jan 09 '17 15:01 MarounMaroun

+1 for a default. I'm using rpdb to debug an OpenStack service which uses oslo.utils. oslo.utils has the following:

> /usr/lib/python2.7/site-packages/oslo_utils/encodeutils.py(39)safe_decode() 
-> incoming = (sys.stdin.encoding or                                          
(Pdb) l                                                                       
 34                                                                           
 35         if isinstance(text, six.text_type):                               
 36             return text                                                   
 37                                                                           
 38         if not incoming:                                                  
 39  ->         incoming = (sys.stdin.encoding or                             
 40                         sys.getdefaultencoding())                         
 41                                                                           
 42         try:                                                              
 43             return text.decode(incoming, errors)                          
 44         except UnicodeDecodeError:                                        
(Pdb) type(sys.stdin)                                                         
<class 'rpdb.FileObjectWrapper'>

When this executes I see the following error:

AttributeError: "'_fileobject' object has no attribute 'encoding'"

The code runs fine outside rpdb because sys.stdin.encoding is defined.

dougszumski avatar May 02 '17 15:05 dougszumski

@dougszumski Well, that's exactly what I'm having now (also while debugging an OpenStack service). A workaround would be adding the encoding attribute to sys.stdin just a line before it breaks.

I'm still waiting for a response from OP before submitting my PR.

MarounMaroun avatar May 03 '17 10:05 MarounMaroun

Yes that makes sense, I'll gladly review that PR.

tamentis avatar May 03 '17 13:05 tamentis

I'm not sure how this is happening since the point of rpdb.FileObjectWrapper is to allow you to access properties such as encoding.

@MarounMaroun are you using 0.1.6?

@dougszumski could you check the type of sys.stdin._io and sys.stdin._obj?

tamentis avatar May 13 '17 16:05 tamentis

@tamentis Yes, I'm on 0.1.6.

MarounMaroun avatar May 14 '17 06:05 MarounMaroun

@tamentis The solution should look something like this, can you please take a look?

@dougszumski FYI.

MarounMaroun avatar May 16 '17 10:05 MarounMaroun

Here's a bit more info:

> /usr/lib/python2.7/site-packages/oslo_utils/encodeutils.py(38)safe_decode()
-> if not incoming:
(Pdb) l
 33             raise TypeError("%s can't be decoded" % type(text))
 34
 35         if isinstance(text, six.text_type):
 36             return text
 37         import rpdb; rpdb.set_trace()
 38  ->     if not incoming:
 39             incoming = (sys.stdin.encoding or
 40                         sys.getdefaultencoding())
 41
 42         try:
 43             return text.decode(incoming, errors)
(Pdb) rpdb.__version__
'0.1.6'
(Pdb) sys.stdin._io
<open file '<stdin>', mode 'r' at 0x7f2d6d3720c0>
(Pdb) sys.stdin._obj
<socket._fileobject object at 0x239d450>
(Pdb) sys.stdin.encoding
'UTF-8'
(Pdb) n
> /usr/lib/python2.7/site-packages/oslo_utils/encodeutils.py(39)safe_decode()
-> incoming = (sys.stdin.encoding or
(Pdb)
AttributeError: "'_fileobject' object has no attribute 'encoding'"
> /usr/lib/python2.7/site-packages/oslo_utils/encodeutils.py(39)safe_decode()
-> incoming = (sys.stdin.encoding or
(Pdb) sys.stdin.encoding
'UTF-8'
(Pdb) sys.getdefaultencoding()
'ascii'

dougszumski avatar May 16 '17 12:05 dougszumski

Ah, I think I see what's happening:

> /usr/lib/python2.7/site-packages/oslo_utils/encodeutils.py(38)safe_decode()
-> if not incoming:
(Pdb) l
 33             raise TypeError("%s can't be decoded" % type(text))
 34
 35         if isinstance(text, six.text_type):
 36             return text
 37         import rpdb; rpdb.set_trace()
 38  ->     if not incoming:
 39             incoming = (sys.stdin.encoding or
 40                         sys.getdefaultencoding())
 41
 42         try:
 43             return text.decode(incoming, errors)
(Pdb) sys.stdin._obj
<socket._fileobject object at 0x1a2a450>
(Pdb) sys.stdin._obj.encoding
*** AttributeError: '_fileobject' object has no attribute 'encoding'
(Pdb) sys.stdin._io.encoding
'UTF-8'

Looks like this happens in FileObjectWrapper:

class FileObjectWrapper(object):
    def __init__(self, fileobject, stdio):
        self._obj = fileobject
        self._io = stdio

    def __getattr__(self, attr):
        if hasattr(self._obj, attr):
            attr = getattr(self._obj, attr)
        elif hasattr(self._io, attr):
            attr = getattr(self._io, attr)
        else:
            raise AttributeError("Attribute %s is not found" % attr)
        return attr

dougszumski avatar May 16 '17 12:05 dougszumski

@dougszumski good point. So the solution I suggested didn't work for you?

MarounMaroun avatar May 16 '17 14:05 MarounMaroun

Why was stdin\out assignment needed in the first place? I just commented them out and everything worked fine.

Boris-Barboris avatar Jul 20 '17 22:07 Boris-Barboris

@Boris-Barboris Which lines did you comment out?

MarounMaroun avatar Sep 12 '17 08:09 MarounMaroun

@MarounMaroun https://github.com/tamentis/rpdb/blob/master/rpdb/init.py#L61 https://github.com/tamentis/rpdb/blob/master/rpdb/init.py#L67 and 68

Boris-Barboris avatar Sep 12 '17 08:09 Boris-Barboris

Hi, everyone, is this problem solved? I also encountered this problem when debugging the OpenStack project.

airborne007 avatar Jun 19 '19 02:06 airborne007

@airborne007 Not yet. The current solution is to add encoding attribute to sys.stdin just a line before it breaks.

MarounMaroun avatar Jun 19 '19 09:06 MarounMaroun