Skip to content

Commit 71642e3

Browse files
authored
Add Process.get_label/1 to expose :proc_lib.get_label (#15169)
1 parent d0ce39e commit 71642e3

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

lib/elixir/lib/process.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,30 @@ defmodule Process do
979979
@spec unalias(alias) :: boolean
980980
defdelegate unalias(alias), to: :erlang
981981

982+
@doc """
983+
Returns the label set for the process `pid` as set with `set_label/1`
984+
or `:proc_lib.set_label/1`.
985+
986+
Defaults to the current process when `pid` is not passed.
987+
988+
## Examples
989+
990+
Process.set_label({:any, "term"})
991+
Process.get_label()
992+
#=> {:any, "term"}
993+
994+
Returns `nil` when not set:
995+
996+
Process.get_label(pid)
997+
#=> nil
998+
999+
"""
1000+
@doc since: "1.20.0"
1001+
@spec get_label(pid()) :: term()
1002+
def get_label(pid \\ self()) do
1003+
nilify(:proc_lib.get_label(pid))
1004+
end
1005+
9821006
@doc """
9831007
Add a descriptive term to the current process.
9841008

lib/elixir/test/elixir/process_test.exs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,22 @@ defmodule ProcessTest do
180180
end
181181
end
182182

183-
describe "set_label/1" do
184-
@compile {:no_warn_undefined, :proc_lib}
183+
describe "get_label/1" do
184+
test "gets a process label, compatible with `:proc_lib.set_label/1`" do
185+
label = {:some_label, :rand.uniform(99999)}
186+
assert :ok = :proc_lib.set_label(label)
187+
188+
assert Process.get_label() == label
189+
assert Process.get_label(self()) == label
190+
end
185191

192+
test "returns nil when not set" do
193+
pid = spawn(fn -> :ok end)
194+
assert Process.get_label(pid) == nil
195+
end
196+
end
197+
198+
describe "set_label/1" do
186199
test "sets a process label, compatible with `:proc_lib.get_label/1`" do
187200
label = {:some_label, :rand.uniform(99999)}
188201
assert :ok = Process.set_label(label)

0 commit comments

Comments
 (0)