-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
PI: Remove b_ calls #986
PI: Remove b_ calls #986
Conversation
Function calls are cheap, but not for free in Python. The b_ function converts a string to a bytes object. When we have a constant string, we can use the constant byte representation instead (a b"byte literal" instead of b_("string literal"))
See also: #986 Also: Minor type improvement
Performance Improvements (PI): - Remove b_ calls (#992, #986) - Apply improvements to _utils suggested by perflint (#993) Robustness (ROB): - utf-16-be\' codec can\'t decode (...) (#995) Documentation (DOC): - Remove reference to Scripts (#987) Developer Experience (DEV): - Fix type annotations for add_bookmarks (#1000) Testing (TST): - Add test for PdfMerger (#1001) - Add tests for XMP information (#996) - reader.get_fields / zlib issue / LZW decode issue (#1004) - reader.get_fields with report generation (#1002) - Improve test coverage by extracting texts (#998) Code Style (STY): - Apply fixes suggested by pylint (#999) Full Changelog: 2.2.0...2.2.1
If b_ doesn't work anymore, what do we replace it with? I'm having issues updating the PyPDF2 module because my code uses b_. |
You don't need For simplicty, you can do this: def b_(s):
if type(s) == bytes:
return s
else:
try:
r = s.encode("latin-1")
return r
except Exception:
r = s.encode("utf-8")
return r However, I strongly encourage you to look carefully if you can simply drop using |
Appreciate it, worked like a charm :) |
@shubhamshah02, an other useful encoding is |
Function calls are cheap, but not for free in Python.
The b_ function converts a string to a bytes object. When we have
a constant string, we can use the constant byte representation
instead (a b"byte literal" instead of b_("string literal"))