Skip to content

FIX: Mark strings containing regex escapes as raw #3106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions nipype/interfaces/diffusion_toolkit/odf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ class HARDIMat(CommandLine):

def _create_gradient_matrix(self, bvecs_file, bvals_file):
_gradient_matrix_file = "gradient_matrix.txt"
bvals = [val for val in re.split("\s+", open(bvals_file).readline().strip())]
bvals = [val for val in re.split(r"\s+", open(bvals_file).readline().strip())]
bvecs_f = open(bvecs_file)
bvecs_x = [val for val in re.split("\s+", bvecs_f.readline().strip())]
bvecs_y = [val for val in re.split("\s+", bvecs_f.readline().strip())]
bvecs_z = [val for val in re.split("\s+", bvecs_f.readline().strip())]
bvecs_x = [val for val in re.split(r"\s+", bvecs_f.readline().strip())]
bvecs_y = [val for val in re.split(r"\s+", bvecs_f.readline().strip())]
bvecs_z = [val for val in re.split(r"\s+", bvecs_f.readline().strip())]
bvecs_f.close()
gradient_matrix_f = open(_gradient_matrix_file, "w")
for i in range(len(bvals)):
Expand Down
4 changes: 2 additions & 2 deletions nipype/interfaces/dipy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def create_interface_specs(class_name, params=None, BaseClass=TraitedSpec):
attr[name] = traits_type(desc=desc[-1], mandatory=is_mandatory)
else:
attr[name] = traits_type(
p[3], desc=desc[-1], exists=True, usedefault=True,
p[3], desc=desc[-1], exists=True, usedefault=True
)

newclass = type(str(class_name), (BaseClass,), attr)
Expand Down Expand Up @@ -189,7 +189,7 @@ def dipy_to_nipype_interface(cls_name, dipy_flow, BaseClass=DipyBaseInterface):
parser = IntrospectiveArgumentParser()
flow = dipy_flow()
parser.add_workflow(flow)
default_values = inspect.getargspec(flow.run).defaults
default_values = inspect.getfullargspec(flow.run).defaults
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess I fixed another deprecation warning. This works for all Py3.

optional_params = [
args + (val,) for args, val in zip(parser.optional_parameters, default_values)
]
Expand Down
16 changes: 8 additions & 8 deletions nipype/interfaces/elastix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class EditTransform(BaseInterface):
input_spec = EditTransformInputSpec
output_spec = EditTransformOutputSpec
_out_file = ""
_pattern = '\((?P<entry>%s\s"?)([-\.\s\w]+)("?\))'
_pattern = r'\((?P<entry>%s\s"?)([-\.\s\w]+)("?\))'

_interp = {"nearest": 0, "linear": 1, "cubic": 3}

Expand All @@ -103,14 +103,14 @@ def _run_interface(self, runtime):
p = re.compile(
(self._pattern % "ResultImagePixelType").decode("string-escape")
)
rep = "(\g<entry>%s\g<3>" % self.inputs.output_type
rep = r"(\g<entry>%s\g<3>" % self.inputs.output_type
contents = p.sub(rep, contents)

if isdefined(self.inputs.output_format):
p = re.compile(
(self._pattern % "ResultImageFormat").decode("string-escape")
)
rep = "(\g<entry>%s\g<3>" % self.inputs.output_format
rep = r"(\g<entry>%s\g<3>" % self.inputs.output_format
contents = p.sub(rep, contents)

if isdefined(self.inputs.interpolation):
Expand All @@ -119,7 +119,7 @@ def _run_interface(self, runtime):
"string-escape"
)
)
rep = "(\g<entry>%s\g<3>" % self._interp[self.inputs.interpolation]
rep = r"(\g<entry>%s\g<3>" % self._interp[self.inputs.interpolation]
contents = p.sub(rep, contents)

if isdefined(self.inputs.reference_image):
Expand All @@ -130,17 +130,17 @@ def _run_interface(self, runtime):

size = " ".join(["%01d" % s for s in im.shape])
p = re.compile((self._pattern % "Size").decode("string-escape"))
rep = "(\g<entry>%s\g<3>" % size
rep = r"(\g<entry>%s\g<3>" % size
contents = p.sub(rep, contents)

index = " ".join(["0" for s in im.shape])
p = re.compile((self._pattern % "Index").decode("string-escape"))
rep = "(\g<entry>%s\g<3>" % index
rep = r"(\g<entry>%s\g<3>" % index
contents = p.sub(rep, contents)

spacing = " ".join(["%0.4f" % f for f in im.header.get_zooms()])
p = re.compile((self._pattern % "Spacing").decode("string-escape"))
rep = "(\g<entry>%s\g<3>" % spacing
rep = r"(\g<entry>%s\g<3>" % spacing
contents = p.sub(rep, contents)

itkmat = np.eye(4)
Expand All @@ -156,7 +156,7 @@ def _run_interface(self, runtime):
# contents = p.sub(rep, contents)

p = re.compile((self._pattern % "Origin").decode("string-escape"))
rep = "(\g<entry>%s\g<3>" % orig
rep = r"(\g<entry>%s\g<3>" % orig
contents = p.sub(rep, contents)

with open(self._get_outfile(), "w") as of:
Expand Down
6 changes: 3 additions & 3 deletions nipype/interfaces/freesurfer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ def _format_arg(self, name, spec, value):
# extension strings
if value.endswith(".annot"):
value = value[:-6]
if re.match("%s[\.\-_]" % self.inputs.hemi, value[:3]):
if re.match(r"%s[\.\-_]" % self.inputs.hemi, value[:3]):
value = value[3:]
return "-annotation %s" % value
return super(SurfaceSnapshots, self)._format_arg(name, spec, value)
Expand Down Expand Up @@ -1145,7 +1145,7 @@ class ImageInfo(FSCommand):
output_spec = ImageInfoOutputSpec

def info_regexp(self, info, field, delim="\n"):
m = re.search("%s\s*:\s+(.+?)%s" % (field, delim), info)
m = re.search(r"%s\s*:\s+(.+?)%s" % (field, delim), info)
if m:
return m.group(1)
else:
Expand Down Expand Up @@ -1175,7 +1175,7 @@ def aggregate_outputs(self, runtime=None, needed_outputs=None):
outputs.ph_enc_dir = self.info_regexp(info, "PhEncDir")

# File format and datatype are both keyed by "type"
ftype, dtype = re.findall("%s\s*:\s+(.+?)\n" % "type", info)
ftype, dtype = re.findall(r"%s\s*:\s+(.+?)\n" % "type", info)
outputs.file_format = ftype
outputs.data_type = dtype

Expand Down
9 changes: 5 additions & 4 deletions nipype/interfaces/spm/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,16 @@ def _make_matlab_command(self, _):
script += "condnames=names;\n"
else:
if self.inputs.use_derivs:
script += "pat = 'Sn\([0-9]*\) (.*)';\n"
script += r"pat = 'Sn\([0-9]*\) (.*)';" "\n"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully we test these. I'm less comfortable with these, since they need to work when written to MATLAB scripts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as long as we are still running the spm workflows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codecov doesn't seem to hit them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested:

>>> a = ""
>>> a += r"\s+" "\nnextline"                                                                   
>>> print(a)                                                                                   
\s+
nextline

So rawstring/string concatenation works fine, and the += is lower precedence, so I'm reasonably confident in this change.

Merging.

else:
script += (
"pat = 'Sn\([0-9]*\) (.*)\*bf\(1\)|Sn\([0-9]*\) "
".*\*bf\([2-9]\)|Sn\([0-9]*\) (.*)';\n"
r"pat = 'Sn\([0-9]*\) (.*)\*bf\(1\)|Sn\([0-9]*\) "
r".*\*bf\([2-9]\)|Sn\([0-9]*\) (.*)';"
"\n"
)
script += "t = regexp(names,pat,'tokens');\n"
# get sessidx for columns
script += "pat1 = 'Sn\(([0-9].*)\)\s.*';\n"
script += r"pat1 = 'Sn\(([0-9].*)\)\s.*';" "\n"
script += "t1 = regexp(names,pat1,'tokens');\n"
script += (
"for i0=1:numel(t),condnames{i0}='';condsess(i0)=0;if "
Expand Down
4 changes: 2 additions & 2 deletions nipype/pipeline/plugins/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def test_report_crash():
mock_node = mock.MagicMock(name="mock_node")
mock_node._id = "an_id"
mock_node.config = {
"execution": {"crashdump_dir": ".", "crashfile_format": "pklz",}
"execution": {"crashdump_dir": ".", "crashfile_format": "pklz"}
}

actual_crashfile = report_crash(mock_node)

expected_crashfile = re.compile(".*/crash-.*-an_id-[0-9a-f\-]*.pklz")
expected_crashfile = re.compile(r".*/crash-.*-an_id-[0-9a-f\-]*.pklz")

assert (
expected_crashfile.match(actual_crashfile).group() == actual_crashfile
Expand Down
2 changes: 1 addition & 1 deletion nipype/sphinxext/plot_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def remove_coding(text):
"""
Remove the coding comment, which exec doesn't like.
"""
sub_re = re.compile("^#\s*-\*-\s*coding:\s*.*-\*-$", flags=re.MULTILINE)
sub_re = re.compile(r"^#\s*-\*-\s*coding:\s*.*-\*-$", flags=re.MULTILINE)
return sub_re.sub("", text)


Expand Down