<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Malgun Gothic";
        panose-1:2 11 5 3 2 0 0 2 0 4;}
@font-face
        {font-family:"\@Malgun Gothic";}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-ligatures:standardcontextual;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-ligatures:standardcontextual;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">I’m getting intermittent fails with our Python unit tests and I wondered if anyone has seen the same issue. I think it’s a race condition where a mock patch is either not applied at all or maybe applied too slowly so the patched function
 still has its original return value. It might be a pytest issue, but I’ve now seen it with two different GDAL methods so I’m just curious if anyone has any idea what may be going on.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The first example I reported on Stack Overflow (<a href="https://stackoverflow.com/q/75185263/3182496">https://stackoverflow.com/q/75185263/3182496</a>) and looks something like this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="vertical-align:baseline"><span style="font-size:10.0pt;font-family:"Courier New";border:none windowtext 1.0pt;padding:0cm;mso-ligatures:none">        mock_feat_defn = mocker.patch.object(ogr.FeatureDefn, 'GetFieldCount')<o:p></o:p></span></p>
<p class="MsoNormal" style="vertical-align:baseline"><span style="font-size:10.0pt;font-family:"Courier New";border:none windowtext 1.0pt;padding:0cm;mso-ligatures:none">        mock_feat_defn.side_effect = RuntimeError("Some other error message")<o:p></o:p></span></p>
<p class="MsoNormal" style="vertical-align:baseline"><span style="font-size:10.0pt;font-family:"Courier New";border:none windowtext 1.0pt;padding:0cm;mso-ligatures:none"><o:p> </o:p></span></p>
<p class="MsoNormal" style="vertical-align:baseline"><span style="font-size:10.0pt;font-family:"Courier New";border:none windowtext 1.0pt;padding:0cm;mso-ligatures:none">        with pytest.raises(RuntimeError, match="Some other error message"):<o:p></o:p></span></p>
<p class="MsoNormal" style="vertical-align:baseline"><span style="font-size:10.0pt;font-family:"Courier New";border:none windowtext 1.0pt;padding:0cm;mso-ligatures:none">            get_from_layer_schema(test_lyr, get_names=True, get_types=False)</span><span style="font-size:10.0pt;font-family:"Courier New";mso-ligatures:none"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The second example is this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">        with mock.patch('osgeo.gdal.Driver.Delete', side_effect=None):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">            with self.assertRaisesRegex(DatasetError, "Could not delete"):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">                delete_path(shp_path)<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In both cases we’re mocking a GDAL method call to check one of our functions correctly raises an error, but sometimes it works and sometimes it doesn’t. If it’s not a GDAL issue, no problem, I will keep looking elsewhere for a solution.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Jon<o:p></o:p></p>
</div>
<div dir="ltr" style="mso-line-height-rule:exactly;-webkit-text-size-adjust:100%;font-size:1px;direction:ltr;"><table cellpadding="0" cellspacing="0" border="0" style="width:100%;font-size:1px;"><tr style="font-size:0;"><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;"><tr style="font-size:0;"><td align="left" style="padding:10px 0 0;vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;color:#4A4A49;font-style:normal;font-weight:700;white-space:nowrap;"><tr style="font-size:14.67px;"><td align="left" style="padding:0;vertical-align:top;font-family:Arial;">e: <span style="font-family:remialcxesans;font-size:1px;color:#FFFFFF;line-height:1px;">​</span></td><td align="left" style="padding:0 0 0 4px;vertical-align:top;color:#F6A124;font-family:Arial;font-weight:400;"><a href="mailto:Jon.Morris@jbarisk.com" target="_blank" id="LPlnk689713" style="text-decoration:none;color:#F6A124;"><strong style="font-weight:400;">Jon.Morris@jbarisk.com</strong></a></td></tr><tr style="font-size:14.67px;"><td align="left" style="padding:0;vertical-align:top;font-family:Arial;">d:</td><td align="left" style="padding:0 0 0 4px;vertical-align:top;color:#F6A124;font-family:Arial;font-weight:400;">+44 (0)1756 587229<br /></td></tr><tr style="font-size:14.67px;"><td align="left" style="padding:0;vertical-align:top;font-family:Arial;">t: </td><td align="left" style="padding:0 0 0 4px;vertical-align:top;color:#F6A124;font-family:Arial;font-weight:400;"><a href="tel:+44%20(0)1756%20799919" target="_blank" id="LPlnk689713" style="text-decoration:none;color:#F6A124;"><strong style="font-weight:400;">+44 (0)1756 799919</strong></a></td></tr><tr style="font-size:0;"><td align="left" style="padding:0;vertical-align:top;"></td><td align="left" style="padding:0 0 0 4px;vertical-align:top;"></td></tr></table></td></tr></table></td></tr><tr style="font-size:14.67px;color:#000001;font-style:normal;font-weight:400;white-space:nowrap;"><td align="left" style="vertical-align:top;font-family:Arial;"><a href="http://www.jbarisk.com/" target="_blank" id="LPlnk689713" title="Click to visit our website" style="text-decoration:none;color:#F6A124;"><strong style="font-weight:400;">www.jbarisk.com</strong></a></td></tr><tr style="font-size:0;"><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;"><tr style="font-size:0;"><td align="left" style="padding:10px 0 0;vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;"><tr style="font-size:0;"><td rowspan="3" align="left" style="padding:0 0 5px;vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="width:100%;font-size:0;"><tr style="font-size:0;"><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;line-height:normal;"><tr style="font-size:0;"><td align="left" style="padding:0 0 11px;vertical-align:top;"><img src="cid:image978682.png@F0F5C55D.6B2FD5D3" border="0" alt="" style="font-size:0;" /></td></tr></table></td></tr><tr style="font-size:0;"><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;"><tr style="font-size:0;"><td align="left" style="padding:0 0 12px;vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="height:30px;font-size:0;"><tr style="font-size:0;"><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;line-height:normal;"><tr style="font-size:0;"><td align="left" style="padding:0 4px 5px 0;vertical-align:top;"><a href="https://www.facebook.com/TheFloodPeople" target="_blank" id="LPlnk689713" style="text-decoration:none;"><img src="cid:image945321.png@D54905E2.D1606FC1" width="34" border="0" title="Facebook" alt="Facebook" style="width:34px;min-width:34px;max-width:34px;font-size:12px;" /></a></td></tr></table></td><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;line-height:normal;"><tr style="font-size:0;"><td align="left" style="padding:0 4px 5px 0;vertical-align:top;"><a href="https://www.linkedin.com/company/jba-risk-management/" target="_blank" id="LPlnk689713" style="text-decoration:none;"><img src="cid:image904217.png@36E486E5.CF5F2139" width="34" border="0" title="LinkedIn" alt="LinkedIn" style="width:34px;min-width:34px;max-width:34px;font-size:12px;" /></a></td></tr></table></td><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;line-height:normal;"><tr style="font-size:0;"><td align="left" style="padding:0 4px 5px 0;vertical-align:top;"><a href="https://twitter.com/JBARisk" target="_blank" id="LPlnk689713" style="text-decoration:none;"><img src="cid:image622653.png@289BCF8E.9F3D7372" width="34" border="0" title="Twitter" alt="Twitter" style="width:34px;min-width:34px;max-width:34px;font-size:12px;" /></a></td></tr></table></td><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;line-height:normal;"><tr style="font-size:0;"><td align="left" style="padding:0 4px 5px 0;vertical-align:top;"><a href="https://www.youtube.com/channel/UC0iatom2jYbW96voW0rlpCw" target="_blank" id="LPlnk689713" style="text-decoration:none;"><img src="cid:image221773.png@F192EC44.29E4E1D4" width="34" border="0" title="YouTube" alt="YouTube" style="width:34px;min-width:34px;max-width:34px;font-size:12px;" /></a></td></tr></table></td></tr></table></td></tr></table></td></tr></table></td><td style="padding:0;"> </td></tr><tr style="font-size:0;"><td style="padding:0;"> </td></tr><tr style="font-size:0;"><td style="padding:0;"> </td></tr></table></td></tr></table></td></tr><tr style="font-size:0;"><td align="left" style="vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:0;"><tr style="font-size:0;"><td align="left" style="padding:7px 0 5px;border-top:solid 1px #4A4A49;border-right:none;border-bottom:none;border-left:none;vertical-align:top;"><table cellpadding="0" cellspacing="0" border="0" style="white-space:normal;color:#444444;font-size:14.67px;font-family:Calibri,Arial,sans-serif;font-weight:400;font-style:normal;text-align:left;line-height:12px;"><tr style="font-size:9px;"><td style="font-family:Arial;"><span style="background-color:#FFFFFF;">All JBA Risk Management's email messages contain confidential information and are intended only for the individual(s) named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.</span><br /><span style="background-color:#FFFFFF;">Please notify the sender immediately by email if you have received this email by mistake and delete this email from your system.</span> <br /><span style="background-color:#FFFFFF;">JBA Risk Management Limited is registered in England, company number 07732946, 1 Broughton Park, Old Lane North, Broughton, Skipton, North Yorkshire, BD23 3FD, England</span><span style="color:#000001;">.</span><br /></td></tr></table></td></tr></table></td></tr></table></div></body>
</html>