-
Notifications
You must be signed in to change notification settings - Fork 5
⚡ Use context manager for safe file handling in search #36
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,20 +49,21 @@ matches = [] | |
| for src in sources: | ||
| is_global = 'global' in src | ||
| try: | ||
| for line in open(src): | ||
| try: | ||
| d = json.loads(line) | ||
| if type_filter and d.get('type') != type_filter: | ||
| continue | ||
| if keyword: | ||
| insight = (d.get('insight', '') + ' ' + d.get('key', '')).lower() | ||
| if keyword not in insight: | ||
| with open(src) as f: | ||
| for line in f: | ||
| try: | ||
| d = json.loads(line) | ||
| if type_filter and d.get('type') != type_filter: | ||
| continue | ||
| if is_global: | ||
| d['_source'] = 'global' | ||
| matches.append(json.dumps(d)) | ||
| except Exception: | ||
| pass | ||
| if keyword: | ||
| insight = (d.get('insight', '') + ' ' + d.get('key', '')).lower() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| if keyword not in insight: | ||
| continue | ||
| if is_global: | ||
| d['_source'] = 'global' | ||
| matches.append(json.dumps(d)) | ||
| except Exception: | ||
| pass | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicitly specifying the encoding when opening files is highly recommended to ensure portability and correctness across different environments.\n\nIn environments with a non-UTF-8 default locale (such as minimal Docker containers, CI environments, or systems running under the
Clocale),open(src)will fall back to the system's default encoding (e.g., ASCII). If the learnings file contains any non-ASCII characters (like curly quotes, accented characters, or emojis), iterating over the file will raise aUnicodeDecodeError. Because of the outerexcept Exception: passblock, this exception will be silently caught, causing the script to stop reading the rest of the file without any warning. Specifyingencoding='utf-8'explicitly prevents this issue.