Re: How a script can know if it has been called with the -i command line option?

2006-12-23 Thread vasudevram

vasudevram wrote:
> Peter  Wang wrote:
> > Michele Simionato wrote:
> > > The subject says it all, I would like a script to act differently when
> > > called as
> > > $ python script.py and when called as $ python -i script.py. I looked
> > > at the sys module
> > > but I don't see a way to retrieve the command line flags, where should
> > > I look?
> >
> > I realize this is quite a hack, but the entire command line is
> > preserved in the process's entry in the OS's  process table.  if you do
> > "ps -ax" you will see that the interpreter was invoked with -i.  I
> > didn't test this under windows, but it works on Mac and Linux.
>
> That hack might not work - at least, as described, and on Linux or Mac
> OS if the UNIX-based one, i.e. OS X). Because there could be other
> users who ran python command lines with or without the -i option. As
> described, there's no way for this user to know which python invocation
> is his/hers, and which are of other users. There might be a way,
> though, if we can get this user's python instance's process id and then
> grep for a line containing that id (in the appropriate column) in the
> ps output.
>
> Vasudev Ram
> ~~
> Dancing Bison Enterprises
> http://www.dancingbison.com
> http://dancingbison.blogspot.com
> ~~
> Check out the cool Snap.com preview feature on my web site.
> Free signup for anyone at www.snap.com
> I'm not affiliated with it.

Just realized: getting the python process's process id is possible from
the Python program itself, using os.getpid().

Vasudev

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-23 Thread vasudevram

Peter  Wang wrote:
> Michele Simionato wrote:
> > The subject says it all, I would like a script to act differently when
> > called as
> > $ python script.py and when called as $ python -i script.py. I looked
> > at the sys module
> > but I don't see a way to retrieve the command line flags, where should
> > I look?
>
> I realize this is quite a hack, but the entire command line is
> preserved in the process's entry in the OS's  process table.  if you do
> "ps -ax" you will see that the interpreter was invoked with -i.  I
> didn't test this under windows, but it works on Mac and Linux.

That hack might not work - at least, as described, and on Linux or Mac
OS if the UNIX-based one, i.e. OS X). Because there could be other
users who ran python command lines with or without the -i option. As
described, there's no way for this user to know which python invocation
is his/hers, and which are of other users. There might be a way,
though, if we can get this user's python instance's process id and then
grep for a line containing that id (in the appropriate column) in the
ps output.

Vasudev Ram
~~
Dancing Bison Enterprises
http://www.dancingbison.com
http://dancingbison.blogspot.com
~~
Check out the cool Snap.com preview feature on my web site.
Free signup for anyone at www.snap.com
I'm not affiliated with it.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-22 Thread Tim Golden
> > Michele Simionato wrote:
> >> The subject says it all, I would like a script to act differently when
> >> called as
> >> $ python script.py and when called as $ python -i script.py.

[Michael B. Trausch]
> There is a set of utilities that have UNIX-like ps behavior, but, as is
> typical for Windows, they don't work the way their UNIX and UNIX-like
> counterparts do.  Does 'ps' work from within Cygwin, and if so, would
> redistributing that be an option?

If you wanted to get the command line from within
Windows, you could use win32api.GetCommandLine.
I think the OP's on Linux, and in any case you'd have
to do your own parsing, but...


c:\>python -i
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
>>> win32api.GetCommandLine ()
'python -i'
>>>


TJG

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-22 Thread Michael B. Trausch
Peter Wang wrote:
> Michele Simionato wrote:
>> The subject says it all, I would like a script to act differently when
>> called as
>> $ python script.py and when called as $ python -i script.py. I looked
>> at the sys module
>> but I don't see a way to retrieve the command line flags, where should
>> I look?
> 
> I realize this is quite a hack, but the entire command line is
> preserved in the process's entry in the OS's  process table.  if you do
> "ps -ax" you will see that the interpreter was invoked with -i.  I
> didn't test this under windows, but it works on Mac and Linux.
> 

There is a set of utilities that have UNIX-like ps behavior, but, as is 
typical for Windows, they don't work the way their UNIX and UNIX-like 
counterparts do.  Does 'ps' work from within Cygwin, and if so, would 
redistributing that be an option?

-- Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-22 Thread Peter Wang

Michele Simionato wrote:
> The subject says it all, I would like a script to act differently when
> called as
> $ python script.py and when called as $ python -i script.py. I looked
> at the sys module
> but I don't see a way to retrieve the command line flags, where should
> I look?

I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's  process table.  if you do
"ps -ax" you will see that the interpreter was invoked with -i.  I
didn't test this under windows, but it works on Mac and Linux.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-21 Thread Carsten Haese
On Thu, 2006-12-21 at 11:22 -0800, [EMAIL PROTECTED] wrote:
> Michele Simionato wrote:
> > The subject says it all, I would like a script to act differently when
> > called as
> > $ python script.py and when called as $ python -i script.py. I looked
> > at the sys module
> > but I don't see a way to retrieve the command line flags, where should
> > I look?
> In the optparse module.

That doesn't answer the question. The OP wants to inspect the options
passed to the interpreter, not the options passed to the script.
optparse aids in parsing sys.argv, which only contains the options that
are passed to the script.

-Carsten


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-21 Thread commander . coder

Michele Simionato wrote:
> The subject says it all, I would like a script to act differently when
> called as
> $ python script.py and when called as $ python -i script.py. I looked
> at the sys module
> but I don't see a way to retrieve the command line flags, where should
> I look?
In the optparse module.

Jim

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How a script can know if it has been called with the -i command line option?

2006-12-21 Thread Thomas Heller
Michele Simionato schrieb:
> The subject says it all, I would like a script to act differently when
> called as
> $ python script.py and when called as $ python -i script.py. I looked
> at the sys module
> but I don't see a way to retrieve the command line flags, where should
> I look?
> TIA,
> 
>   Michele Simionato
> 

I don't know how to get the command line flags, but the variable you are 
interested
in is this one:

from ctypes import *
print c_int.in_dll(pythonapi, "Py_InteractiveFlag")

;-)

Thomas

-- 
http://mail.python.org/mailman/listinfo/python-list