diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ec3fce4a..3206dd26 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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) diff --git a/httprunner/ext/har2case/utils.py b/httprunner/ext/har2case/utils.py index 110f8b40..ead83b30 100644 --- a/httprunner/ext/har2case/utils.py +++ b/httprunner/ext/har2case/utils.py @@ -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}") diff --git a/httprunner/loader.py b/httprunner/loader.py index b389c716..8a28f0ed 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -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 diff --git a/httprunner/scaffold.py b/httprunner/scaffold.py index 07d1f33a..3f42f32d 100644 --- a/httprunner/scaffold.py +++ b/httprunner/scaffold.py @@ -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)