clint icon indicating copy to clipboard operation
clint copied to clipboard

Fix sys.argv import when running inside LLDB Python interpreter.

Open mblsha opened this issue 11 years ago • 4 comments

I'm using clint.colored inside LLDB scripts, and this fix is required for that to work.

mblsha avatar Feb 02 '14 15:02 mblsha

What do you think about finding a way to detect LLDB running and only do this in that scenario? I'm slightly worried that this could mask problems with argv imports in other scenarios (although I can't think why this would ever happen, so maybe this isn't an issue)

jpiper avatar Feb 06 '14 09:02 jpiper

I've checked os.environ, and there's nothing special to check. sys.platform is darwin. sys.argv is available without import for me:

> xcrun lldb
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> import sys
>>> sys.argv
[]

Can't think of anything else to check. Any ideas?

mblsha avatar Feb 07 '14 14:02 mblsha

It actually appears that lldb loads python with an lldb module which isn't usually around, so I think the following might be a good solution

try:
    from sys import argv
# Can happen when executing inside LLDB context   
except ImportError:
    if lldb:
        import sys
        sys.argv = []
    else:
        raise ImportError

jpiper avatar Feb 07 '14 15:02 jpiper

lldb module seems to be automatically present only when running inside embedded interpreter, not when inserting externals scripts:

mblsha@siruba ~> cat 1.py 
print lldb

mblsha@siruba ~> xcrun lldb
(lldb) command script import 1.py
error: module importing failed: Python error raised while importing module: reload() argument must be module - traceback:   File "temp.py", line 1, in <module>
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> print lldb
<module 'lldb' from '/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/Python/lldb/__init__.py'>
>>> ^D

mblsha avatar Feb 15 '14 12:02 mblsha