Skip to content

Commit a8a280e

Browse files
csteegzmigueldeicaza
authored andcommitted
Runner.AddInput will no longer throw NullReferenceException (migueldeicaza#418)
1 parent 081199f commit a8a280e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

TensorFlowSharp/Tensorflow.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,14 +2929,28 @@ public Runner AddTarget (params TFOperation [] targets)
29292929
// Parses user strings that contain both the operation name and an index.
29302930
TFOutput ParseOutput (string operation)
29312931
{
2932+
TFOperation tfOperation;
29322933
var p = operation.IndexOf (':');
2933-
if (p != -1 && p != operation.Length - 1){
2934+
if (p != -1 && p != operation.Length - 1)
2935+
{
2936+
29342937
var op = operation.Substring (0, p);
2935-
if (int.TryParse (operation.Substring (p + 1), out var idx)){
2936-
return session.Graph [op] [idx];
2938+
if (int.TryParse (operation.Substring (p + 1), out var idx))
2939+
{
2940+
tfOperation = session.Graph [op];
2941+
if (tfOperation == null)
2942+
{
2943+
throw new ArgumentOutOfRangeException($"An operation named {op} is not in this graph.");
2944+
}
2945+
return tfOperation [idx];
29372946
}
29382947
}
2939-
return session.Graph [operation] [0];
2948+
tfOperation = session.Graph [operation];
2949+
if (tfOperation == null)
2950+
{
2951+
throw new ArgumentOutOfRangeException($"An operation named {operation} is not in this graph.");
2952+
}
2953+
return tfOperation [0];
29402954
}
29412955

29422956
/// <summary>

tests/TensorFlowSharp.Tests.CSharp/SessionTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using TensorFlow;
34
using Xunit;
45

@@ -16,5 +17,18 @@ public void Should_ListDeviceReturnDevices ()
1617
Assert.True(devices.Any());
1718
}
1819
}
20+
21+
[Theory]
22+
[InlineData("Placeholder")]
23+
[InlineData("Placeholder:0")]
24+
public void ParseOutput_ThrowsForMissingOp (string name)
25+
{
26+
using (var graph = new TFGraph ())
27+
using (var session = new TFSession (graph))
28+
{
29+
var runner = session.GetRunner();
30+
Assert.Throws<ArgumentOutOfRangeException>(() => runner.AddInput(name, new TFTensor(1)));
31+
}
32+
}
1933
}
2034
}

0 commit comments

Comments
 (0)