[Python-bugs-list] [ python-Bugs-423728 ] improper value of sys.argv with '-c' opt

noreply@sourceforge.net noreply@sourceforge.net
Mon, 14 May 2001 00:47:47 -0700


Bugs item #423728, was updated on 2001-05-13 08:04
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=423728&group_id=5470

Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Frederic Giacometti (giacometti)
Assigned to: Nobody/Anonymous (nobody)
Summary: improper value of sys.argv with '-c' opt

Initial Comment:

Python 2,1 gives the following result:

---
python -c "import sys; print sys.argv"
[ '-c']
---

This is uncorrect and error-prone.

The two (possible) correct answers are:
    [] # strip off the '-c' stuff
  or
    [ '-c', 'import sys; print sys.argv'] # keep the '-c' stuff

  but ['-c'] alone is definitely uncorrect, as well as it does not make sense.

The source of the problem can explicitly be traced to file Python-2.1 -main.c, line 287:
  argv[_PyOS_optind] = "-c"; 
        (cf. [ python-Bugs-422678 ] (argv is modified in Py_Main())

which modifies 'argv' as side effect; this then messes up the initialization of 'sys.argv'.

Note: This bug is another side effect of [ python-Bugs-422678 ]

FG


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-05-14 00:47

Message:
Logged In: YES 
user_id=31435

Changing documented behavior is always a risk, so is never 
done lightly.  The tradeoff you propose here is that we 
break long-documented behavior so that you don't have to 
fix your own code that made a bad assumption about an 
undocumented internal C API function.

That isn't an argument for incompatible behavior I could 
sell to anyone -- and I bet not even to you a month from 
now.  Changing -c behavior is simply a bad idea.  I have a 
lot more sympathy for the original bug report (this one 
wasn't even reporting a *problem*:  the original claims in 
this report that current -c behavior is "uncorrect and 
error-prone" and "definitely uncorrect" and "does not make 
sense" were wrong on all counts, as you yourself realized 
by the time you added your first comment, retracting your 
original two (claimed to be) "correct" suggestions).

BTW, if you look at the timestamps, you'll see that I was 
writing my first comment at the same time you were writing 
yours:  I had not seen your first comment by the time I 
wrote my first.  Given the unbroken successsion of 
incorrect claims in the original bug report to which I was 
responding, I think my first comment showed all the 
consideration it deserved, and then some.


----------------------------------------------------------------------

Comment By: Frederic Giacometti (giacometti)
Date: 2001-05-13 12:41

Message:
Logged In: YES 
user_id=93657


Come on... this would be one of the least minor changes ever submitted, and it will be for all the better in 
regard to Python's technical quality (coherency, robustness, and ease of integration into other systems).

I can't see any down-sides to the change, and you could hardly thing of a smallest and easiest patch.

I tested the change, and submitted the patch (patch number 423759): it is the suppression of one single 
line in the source code, and updating the LaTex documentation for sys.argv.

Thanks,

FG


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-05-13 11:50

Message:
Logged In: YES 
user_id=31435

Passing "-c" in this situation has been documented behavior 
for 10 years:  we couldn't change that now even if we 
wanted to.  That's why I pointed you to the docs (which 
this report showed no awareness of).


----------------------------------------------------------------------

Comment By: Frederic Giacometti (giacometti)
Date: 2001-05-13 11:27

Message:
Logged In: YES 
user_id=93657

I am sorry, but:

- 'some; arg' is a legal Unix/Windows file name
- it is better than than '-c'
- it does not 'bloat' the code, while creating problems elsewhere
- if you really want something 'as good as anything', as you say: --> use '', the empty name, or even None.

I wish you would give a little more consideration, and stop refering me to reference manuals, obfuscating 
the issue, and as if I did not know them.

Thanks,

FG


FG

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-05-13 11:17

Message:
Logged In: YES 
user_id=31435

Not a bug.  Note also, e.g.,

> python -c "import sys; print sys.argv" a b c
['-c', 'a', 'b', 'c']

See the docs for sys.argv (in the Library manual) -- this 
is the documented and intended behavior.  argv[0] is (as 
also in C) supposed to be the name of the script, but when 
the script is passed literally on the cmdline there is no 
sensible name.  "-c" is as good as anything then.  
*Something* name-like must be passed in argv[0] else code 
that examines sys.argv would have to know whether or not 
Python started via -c.

argv[1:] is supposed contain the arguments to the script.  
Since the expression following -c *is* the script, it would 
be incorrect to leave that in argv:  the script is not an 
argument to itself, after all.


----------------------------------------------------------------------

Comment By: Frederic Giacometti (giacometti)
Date: 2001-05-13 11:17

Message:
Logged In: YES 
user_id=93657

Actually, I need to correct something.

When using 'python -c "some; cmd", the expected value for sys.argv would be either:
  [ ''] # sys.argv value in interactive mode -> sys.argv[ 0] == ''
or
  [ 'some; cmd'] # value of the '-c' option: -> sys.argv[ 0] == 'some; cmd' - this contains more information

In this case, lines 283-288 from main.c could just be deleted.

FG


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=423728&group_id=5470