Project

General

Profile

Actions

Bug #19597

closed

Process.argv0 returns the same mutable String

Bug #19597: Process.argv0 returns the same mutable String

Added by andrykonchin (Andrew Konchin) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:113221]

Description

Process.argv0 returns a name of the script being executed. But it seems it's always the same object, that can be modified and this change will be visible to the consequent Process.argv0 calls.

It makes sense to return a frozen String that cannot be modified.

$0 initial value equals a value returned by Process.argv0 so $0 by default also could be a frozen String.

Example:

puts "Object id:"
p Process.argv0.object_id
p Process.argv0.object_id

puts "Value before modification:"
p Process.argv0
Process.argv0.upcase!

puts "Value after modification:"
p Process.argv0

puts "$0 after modification:"
p $0

It will output:

Object id:
60
60
Value before modification:
"argv0.rb"
Value after modification:
"ARGV0.RB"
$0 after modification:
"ARGV0.RB"

Updated by andrykonchin (Andrew Konchin) over 2 years ago Actions #1

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) over 2 years ago Actions #3 [ruby-core:113416]

I found $0 had been frozen till 1.9.

$ ruby1.9 -e 'p $0.upcase!; p $0; system("ps #$$")'
-e:1:in `upcase!': can't modify frozen String (RuntimeError)
	from -e:1:in `<main>'

I'm not sure why/when it changed, yet.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago Actions #4 [ruby-core:113417]

Once assigned to $0, it is frozen.

$ ruby -v -e '$0 = "test"; p $0; $0.upcase!'
ruby 3.3.0dev (2023-05-05T14:01:50Z master 40438fc4d3) [x86_64-darwin22]
"test"
-e:1:in `upcase!': can't modify frozen String: "test" (FrozenError)
	from -e:1:in `<main>'

So I'd say that only the first $0 is not frozen is a bug.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago Actions #5 [ruby-core:113418]

It's 8c9a453f2d8a.
ruby_set_script_name makes an unfrozen copy.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago Actions #6

  • Tracker changed from Misc to Bug
  • Backport set to 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN

Updated by nobu (Nobuyoshi Nakada) over 2 years ago Actions #7

  • Status changed from Open to Closed

Applied in changeset git|2d9bc3efe57fe0ec1a8171340c950fb98ca8c8f2.


[Bug #19597] Freeze script name

Actions

Also available in: PDF Atom