change: open file in binary mode

This commit is contained in:
debugtalk
2020-06-29 15:20:37 +08:00
parent 804b86d16e
commit 3852849bc1
4 changed files with 12 additions and 11 deletions

View File

@@ -10,6 +10,7 @@
**Changed**
- change: import locust at beginning to monkey patch all modules
- change: open file in binary mode
## 3.1.1 (2020-06-23)

View File

@@ -27,9 +27,9 @@ def load_har_log_entries(file_path):
]
"""
with open(file_path, encoding="utf-8") as f:
with open(file_path, mode="rb") as f:
try:
content_json = json.loads(f.read())
content_json = json.load(f)
return content_json["log"]["entries"]
except (TypeError, JSONDecodeError) as ex:
logger.error(f"failed to load HAR file {file_path}: {ex}")

View File

@@ -28,7 +28,7 @@ project_meta: Union[ProjectMeta, None] = None
def _load_yaml_file(yaml_file: Text) -> Dict:
""" load yaml file and check file content format
"""
with open(yaml_file, encoding="utf-8") as stream:
with open(yaml_file, mode="rb") as stream:
try:
yaml_content = yaml.load(stream)
except yaml.YAMLError as ex:
@@ -42,7 +42,7 @@ def _load_yaml_file(yaml_file: Text) -> Dict:
def _load_json_file(json_file: Text) -> Dict:
""" load json file and check file content format
"""
with open(json_file, encoding="utf-8") as data_file:
with open(json_file, mode="rb") as data_file:
try:
json_content = json.load(data_file)
except json.JSONDecodeError as ex:
@@ -127,17 +127,17 @@ def load_dot_env_file(dot_env_path: Text) -> Dict:
logger.info(f"Loading environment variables from {dot_env_path}")
env_variables_mapping = {}
with open(dot_env_path, encoding="utf-8") as fp:
with open(dot_env_path, mode="rb") as fp:
for line in fp:
# maxsplit=1
if "=" in line:
variable, value = line.split("=", 1)
elif ":" in line:
variable, value = line.split(":", 1)
if b"=" in line:
variable, value = line.split(b"=", 1)
elif b":" in line:
variable, value = line.split(b":", 1)
else:
raise exceptions.FileFormatError(".env format error")
env_variables_mapping[variable.strip()] = value.strip()
env_variables_mapping[variable.strip().decode("utf-8")] = value.strip().decode("utf-8")
utils.set_os_environ(env_variables_mapping)
return env_variables_mapping

View File

@@ -49,7 +49,7 @@ def create_scaffold(project_name):
print(msg)
def create_file(path, file_content=""):
with open(path, "w") as f:
with open(path, "w", encoding="utf-8") as f:
f.write(file_content)
msg = f"created file: {path}"
print(msg)