fix(utils): strip the _qlib_ prefix in fname_to_code, not characters#2262
Open
he-yufeng wants to merge 1 commit into
Open
fix(utils): strip the _qlib_ prefix in fname_to_code, not characters#2262he-yufeng wants to merge 1 commit into
he-yufeng wants to merge 1 commit into
Conversation
fname_to_code used fname.lstrip("_qlib_") to undo code_to_fname's prefix, but
str.lstrip removes any leading characters that appear in its argument, not the
prefix as a whole. exists_qlib_data() lowercases the feature directory name
before calling fname_to_code, so a reserved device code such as LPT1 (stored as
"_qlib_LPT1") becomes "_qlib_lpt1" and then "pt1" instead of "lpt1" -- the
leading "l" is wrongly stripped because it is one of the prefix characters.
Remove the prefix by slicing so the original code is recovered intact.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
fname_to_code()is meant to reversecode_to_fname(), which prefixes reserved Windows device names (CON,PRN,AUX,NUL,COM0–COM9,LPT0–LPT9) with_qlib_so they can be used as directory names. The reverse usedfname.lstrip("_qlib_"), butstr.lstripremoves any leading characters contained in its argument, not the prefix as a whole.exists_qlib_data()lowercases each feature directory name before callingfname_to_code():So
LPT1is stored as_qlib_LPT1, lowercased to_qlib_lpt1, and thenlstrip("_qlib_")strips the leadingltoo (it is one of the prefix characters), yieldingpt1instead oflpt1. The data-existence check then fails to match such a stock. Any name whose body begins with a character in{_, q, l, i, b}is corrupted (e.g.fname_to_code("_qlib_lll")returns"").The fix removes the prefix by slicing, so the original code is recovered intact.
Verification
Before:
After:
Added
FileNameUtils.test_fname_code_round_tripintests/misc/test_utils.py, covering the reserved device names (via the lowercased pathexists_qlib_datauses), a prefix-only body, and a plain code.black -l 120 --checkpasses on the changed files.