Skip to content
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

Merged
merged 1 commit into from Jun 14, 2022
Merged

PI: Remove b_ calls #986

merged 1 commit into from Jun 14, 2022

Conversation

MartinThoma
Copy link
Member

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"))

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"))
@MartinThoma MartinThoma added the nf-performance Non-functional change: Performance label Jun 14, 2022
@MartinThoma MartinThoma merged commit 8441da4 into main Jun 14, 2022
@MartinThoma MartinThoma deleted the pi-remove-b-calls branch June 14, 2022 10:35
MartinThoma added a commit that referenced this pull request Jun 14, 2022
@MartinThoma MartinThoma mentioned this pull request Jun 14, 2022
MartinThoma added a commit that referenced this pull request Jun 14, 2022
See also: #986

Also: Minor type improvement
@MartinThoma MartinThoma mentioned this pull request Jun 14, 2022
MartinThoma added a commit that referenced this pull request Jun 17, 2022
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
@shubhamshah02
Copy link

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_.

@MartinThoma
Copy link
Member Author

MartinThoma commented Aug 29, 2022

You don't need b("foo") any longer. Literal strings "foo" can be converted to b"foo". Otherwise you should have byte objects already.

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 b_(...) everywhere.

@shubhamshah02
Copy link

You don't need b("foo") any longer. Literal strings "foo" can be converted to b"foo". Otherwise you should have byte objects already.

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 b_(...) everywhere.

Appreciate it, worked like a charm :)

@pubpub-zz
Copy link
Collaborator

pubpub-zz commented Aug 29, 2022

@shubhamshah02, an other useful encoding is charmap which converts bytes literally: b"\xFE" -> "\xFE"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nf-performance Non-functional change: Performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants