Skip to content

Commit 376e3f8

Browse files
committed
Safely handle file R/W operations
1 parent 2f2b8d2 commit 376e3f8

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

tests/parametric/test_CasADi_computations_parametric.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
Repo.clone_from(git_url, temp_dir.name)
2323
model_path = temp_dir.name + "/models/stickBot/model.urdf"
2424

25-
## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
26-
robot_file_read = open(model_path, "r")
27-
robot_urdf_string = robot_file_read.read()
28-
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
29-
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
30-
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
31-
robot_file_write = open(model_path, "w")
32-
robot_file_write.write(robot_urdf_string)
25+
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
26+
with open(model_path, "r", encoding="utf-8") as robot_file:
27+
robot_urdf_string = (
28+
robot_file.read()
29+
.replace("<?xml", "")
30+
.replace("version='1.0'", "")
31+
.replace("encoding='UTF-8'?>", "")
32+
)
33+
34+
with open(model_path, "w") as robot_file:
35+
robot_file.write(robot_urdf_string)
3336

3437
joints_name_list = [
3538
"torso_pitch",

tests/parametric/test_Jax_computations_parametric.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727
Repo.clone_from(git_url, temp_dir.name)
2828
model_path = temp_dir.name + "/models/stickBot/model.urdf"
2929

30-
## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
31-
robot_file_read = open(model_path, "r")
32-
robot_urdf_string = robot_file_read.read()
33-
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
34-
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
35-
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
36-
robot_file_write = open(model_path, "w")
37-
robot_file_write.write(robot_urdf_string)
30+
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
31+
with open(model_path, "r", encoding="utf-8") as robot_file:
32+
robot_urdf_string = (
33+
robot_file.read()
34+
.replace("<?xml", "")
35+
.replace("version='1.0'", "")
36+
.replace("encoding='UTF-8'?>", "")
37+
)
38+
39+
with open(model_path, "w") as robot_file:
40+
robot_file.write(robot_urdf_string)
3841

3942
joints_name_list = [
4043
"torso_pitch",

tests/parametric/test_NumPy_computations_parametric.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
Repo.clone_from(git_url, temp_dir.name)
2525
model_path = temp_dir.name + "/models/stickBot/model.urdf"
2626

27-
## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
28-
robot_file_read = open(model_path, "r")
29-
robot_urdf_string = robot_file_read.read()
30-
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
31-
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
32-
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
33-
robot_file_write = open(model_path, "w")
34-
robot_file_write.write(robot_urdf_string)
27+
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
28+
with open(model_path, "r", encoding="utf-8") as robot_file:
29+
robot_urdf_string = (
30+
robot_file.read()
31+
.replace("<?xml", "")
32+
.replace("version='1.0'", "")
33+
.replace("encoding='UTF-8'?>", "")
34+
)
35+
36+
with open(model_path, "w") as robot_file:
37+
robot_file.write(robot_urdf_string)
3538

3639
joints_name_list = [
3740
"torso_pitch",

tests/parametric/test_pytorch_computations_parametric.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727
Repo.clone_from(git_url, temp_dir.name)
2828
model_path = temp_dir.name + "/models/stickBot/model.urdf"
2929

30-
## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
31-
robot_file_read = open(model_path, "r")
32-
robot_urdf_string = robot_file_read.read()
33-
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
34-
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
35-
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
36-
robot_file_write = open(model_path, "w")
37-
robot_file_write.write(robot_urdf_string)
30+
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
31+
with open(model_path, "r", encoding="utf-8") as robot_file:
32+
robot_urdf_string = (
33+
robot_file.read()
34+
.replace("<?xml", "")
35+
.replace("version='1.0'", "")
36+
.replace("encoding='UTF-8'?>", "")
37+
)
38+
39+
with open(model_path, "w") as robot_file:
40+
robot_file.write(robot_urdf_string)
3841

3942
joints_name_list = [
4043
"torso_pitch",

0 commit comments

Comments
 (0)