#7003 Python: Guard nullpointer access for summary methods

This commit is contained in:
Magne Sjaastad 2020-11-24 07:27:00 +01:00
parent 6e789afd10
commit 5e63a1b716
2 changed files with 105 additions and 44 deletions

View File

@ -1,6 +1,10 @@
import sys
import os
import math
import contextlib
import os
import shutil
import tempfile
sys.path.insert(1, os.path.join(sys.path[0], '../../'))
import rips
@ -63,3 +67,49 @@ def test_summary_resample(rips_instance, initialize_test):
summary_data_sampled = summary_case.resample_values("FOPT", "YEAR")
assert(len(summary_data_sampled.values) == 3)
assert(len(summary_data_sampled.time_steps) == 3)
@contextlib.contextmanager
def cd(newdir, cleanup=lambda: True):
prevdir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(prevdir)
cleanup()
@contextlib.contextmanager
def tempdir():
dirpath = tempfile.mkdtemp()
def cleanup():
shutil.rmtree(dirpath)
with cd(dirpath, cleanup):
yield dirpath
# This test ensures that missing unsmry file is handeled gracefully
def test_summary_no_unsmry(rips_instance, initialize_test):
casePathRelative = dataroot.PATH + "/flow_diagnostics_test/SIMPLE_SUMMARY2.SMSPEC"
# create an absolute path, as the helper functions used to create a temporary folder does not work
# with the relative (..\..\) part of the file path
casePath = os.path.abspath(casePathRelative)
with tempdir() as dirpath:
base_path = os.path.basename(casePath)
temp_path = os.path.join(dirpath, base_path)
shutil.copy2(casePath, temp_path)
summary_case = rips_instance.project.import_summary_case(temp_path)
values = summary_case.summary_vector_values()
assert(len(values.values) == 1)
time_steps = summary_case.available_time_steps()
assert(len(time_steps.values) == 1)
addresses = summary_case.available_addresses()
assert(len(addresses.values) == 1)
summary_case.resample_values()

View File

@ -55,17 +55,20 @@ caf::PdmObjectHandle* RimSummaryCase_summaryVectorValues::execute()
auto adr = RifEclipseSummaryAddress::fromEclipseTextAddress( m_addressString().toStdString() );
std::vector<double> values;
bool isOk = sumReader->values( adr, &values );
if ( isOk )
if ( sumReader )
{
auto dataObject = new RimcDataContainerDouble();
dataObject->m_doubleValues = values;
return dataObject;
bool isOk = sumReader->values( adr, &values );
if ( !isOk )
{
// Error message
}
}
return nullptr;
auto dataObject = new RimcDataContainerDouble();
dataObject->m_doubleValues = values;
return dataObject;
}
//--------------------------------------------------------------------------------------------------
@ -100,16 +103,18 @@ RimSummaryCase_availableAddresses::RimSummaryCase_availableAddresses( caf::PdmOb
//--------------------------------------------------------------------------------------------------
caf::PdmObjectHandle* RimSummaryCase_availableAddresses::execute()
{
auto* summaryCase = self<RimSummaryCase>();
RifSummaryReaderInterface* sumReader = summaryCase->summaryReader();
CAF_ASSERT( sumReader );
const std::set<RifEclipseSummaryAddress>& addresses = sumReader->allResultAddresses();
auto* summaryCase = self<RimSummaryCase>();
std::vector<QString> adr;
for ( const auto& a : addresses )
RifSummaryReaderInterface* sumReader = summaryCase->summaryReader();
if ( sumReader )
{
adr.push_back( QString::fromStdString( a.uiText() ) );
const std::set<RifEclipseSummaryAddress>& addresses = sumReader->allResultAddresses();
for ( const auto& a : addresses )
{
adr.push_back( QString::fromStdString( a.uiText() ) );
}
}
auto dataObject = new RimcDataContainerString();
@ -152,13 +157,13 @@ caf::PdmObjectHandle* RimSummaryCase_availableTimeSteps::execute()
{
auto* summaryCase = self<RimSummaryCase>();
RifSummaryReaderInterface* sumReader = summaryCase->summaryReader();
CAF_ASSERT( sumReader );
RifEclipseSummaryAddress adr;
auto timeValues = sumReader->timeSteps( adr );
auto dataObject = new RimcDataContainerTime();
dataObject->m_timeValues = timeValues;
auto dataObject = new RimcDataContainerTime();
if ( sumReader )
{
RifEclipseSummaryAddress adr;
dataObject->m_timeValues = sumReader->timeSteps( adr );
}
return dataObject;
}
@ -203,39 +208,45 @@ caf::PdmObjectHandle* RimSummaryCase_resampleValues::execute()
auto adr = RifEclipseSummaryAddress::fromEclipseTextAddress( m_addressString().toStdString() );
std::vector<double> values;
bool isOk = sumReader->values( adr, &values );
if ( !isOk ) return nullptr;
auto timeValues = sumReader->timeSteps( adr );
QString periodString = m_resamplingPeriod().trimmed();
RiaQDateTimeTools::DateTimePeriod period = RiaQDateTimeTools::DateTimePeriodEnum::fromText( periodString );
auto dataObject = new RimcSummaryResampleData();
if ( period != RiaQDateTimeTools::DateTimePeriod::NONE )
if ( sumReader )
{
RiaTimeHistoryCurveResampler resampler;
resampler.setCurveData( values, timeValues );
std::vector<double> values;
if ( RiaSummaryTools::hasAccumulatedData( adr ) )
bool isOk = sumReader->values( adr, &values );
if ( !isOk )
{
resampler.resampleAndComputePeriodEndValues( period );
// Error message
}
auto timeValues = sumReader->timeSteps( adr );
QString periodString = m_resamplingPeriod().trimmed();
RiaQDateTimeTools::DateTimePeriod period = RiaQDateTimeTools::DateTimePeriodEnum::fromText( periodString );
if ( period != RiaQDateTimeTools::DateTimePeriod::NONE )
{
RiaTimeHistoryCurveResampler resampler;
resampler.setCurveData( values, timeValues );
if ( RiaSummaryTools::hasAccumulatedData( adr ) )
{
resampler.resampleAndComputePeriodEndValues( period );
}
else
{
resampler.resampleAndComputeWeightedMeanValues( period );
}
dataObject->m_timeValues = resampler.resampledTimeSteps();
dataObject->m_doubleValues = resampler.resampledValues();
}
else
{
resampler.resampleAndComputeWeightedMeanValues( period );
dataObject->m_timeValues = timeValues;
dataObject->m_doubleValues = values;
}
dataObject->m_timeValues = resampler.resampledTimeSteps();
dataObject->m_doubleValues = resampler.resampledValues();
}
else
{
dataObject->m_timeValues = timeValues;
dataObject->m_doubleValues = values;
}
return dataObject;