Skip to content

feat: avoid expensive str() calls in enforce_ex when logging is disabled#421

Merged
hsluoyz merged 5 commits intomasterfrom
copilot/optimize-logging-str-conversion
Feb 2, 2026
Merged

feat: avoid expensive str() calls in enforce_ex when logging is disabled#421
hsluoyz merged 5 commits intomasterfrom
copilot/optimize-logging-str-conversion

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

fix: #420

@CLAassistant
Copy link

CLAassistant commented Feb 2, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ hsluoyz
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
@hsluoyz hsluoyz marked this pull request as ready for review February 2, 2026 02:31
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize logging to avoid unnecessary str conversion Avoid expensive str() calls in enforce_ex when logging is disabled Feb 2, 2026
Copilot AI requested a review from hsluoyz February 2, 2026 02:34
@hsluoyz
Copy link
Member

hsluoyz commented Feb 2, 2026

@copilot fix: https://github.com/casbin/pycasbin/actions/runs/21575342377/job/62161536360

Run super-linter/super-linter@v5.7.2
/usr/bin/docker run --name ghcriosuperlintersuperlinterv5_9a79c3 --label 5efbf4 --workdir /github/workspace --rm -e "VALIDATE_ALL_CODEBASE" -e "VALIDATE_PYTHON_BLACK" -e "DEFAULT_BRANCH" -e "GITHUB_TOKEN" -e "LINTER_RULES_PATH" -e "PYTHON_BLACK_CONFIG_FILE" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_ENVIRONMENT" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e "ACTIONS_RESULTS_URL" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp":"/github/runner_temp" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/pycasbin/pycasbin":"/github/workspace" ghcr.io/super-linter/super-linter:v5
--------------------------------------------------------------------------------

                              /@@#///////@@/(@//@%/(@.@(       @@
                          @@//////////////////////////////#*  @@@
                        @////@//(///////////@@@@@///@//@/@**//@@(
                      @///////@///////////////@@@@    (           @,
                     @/(&/@////////////////////                     @
                    @////////////////////////@@                      @
                  @%////////(//////////%/////&@            @@       *,@           ______________
             @@@@@/@/#/////(&//////////////////                       .@         /              \
        *@@@@@.    .%///(//@//////////////////&.   .@@,                 @%      / Don't mind me  \
      @@%           .&@&&/@.@//&/////(//////////    @@@@@@@@@         .. &@    / I'm just looking \
    @@%               @@@@@   @&/////////////////#   @/       V  @@/ ,@@@ @   <  for some trash... |
@@@%                   @@@@        .%@@@@//////#@ @   @@         @     .,.     \__________________/
                                          @@@/@(  (@@@@% @/\      %
                                           @@@@(    .     .@@/\   #
                                             @                  %@%

--------------------------------------------------------------------------------
2026-02-02 02:35:01 [INFO]   ---------------------------------------------
2026-02-02 02:35:01 [INFO]   --- GitHub Actions Multi Language Linter ----
2026-02-02 02:35:01 [INFO]    - Image Creation Date:[2023-12-02T08:58:56Z]
2026-02-02 02:35:01 [INFO]    - Image Revision:[a8150b40c89574adb5f68bf9502b890a236a06b3]
2026-02-02 02:35:01 [INFO]    - Image Version:[a8150b40c89574adb5f68bf9502b890a236a06b3]
2026-02-02 02:35:01 [INFO]   ---------------------------------------------
2026-02-02 02:35:01 [INFO]   ---------------------------------------------
2026-02-02 02:35:01 [INFO]   The Super-Linter source code can be found at:
2026-02-02 02:35:01 [INFO]    - https://github.com/super-linter/super-linter
2026-02-02 02:35:01 [INFO]   ---------------------------------------------
2026-02-02 02:35:01 [INFO]   --------------------------------------------
2026-02-02 02:35:01 [INFO]   Gathering GitHub information...
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_SHA], value:[98d5443c878e66854b01ca3902855a6d86e9e429]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_WORKSPACE], value:[/github/workspace]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_EVENT_PATH], value:[/github/workflow/event.json]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_ORG], value:[casbin]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_REPO], value:[pycasbin]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_TOKEN]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_REPOSITORY], value:[casbin/pycasbin]
2026-02-02 02:35:01 [INFO]   Successfully found:[GITHUB_RUN_ID], value:[21575342377]
2026-02-02 02:35:01 [INFO]   --------------------------------------------
2026-02-02 02:35:01 [INFO]   Gathering user validation information...
2026-02-02 02:35:01 [INFO]   - Only validating [new], or [edited] files in code base...
2026-02-02 02:35:03 [INFO]   ----------------------------------------------
2026-02-02 02:35:03 [INFO]   User provided file:[/github/workspace/pyproject.toml] exists, setting rules file...
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

2026-02-02 02:35:04 [INFO]   ---------------------------------
2026-02-02 02:35:04 [INFO]   ------ File list to check: ------
2026-02-02 02:35:04 [INFO]   ---------------------------------
2026-02-02 02:35:04 [INFO]   ----------------------------------------------
2026-02-02 02:35:04 [INFO]   Successfully gathered list of files...
2026-02-02 02:35:05 [INFO]   
2026-02-02 02:35:05 [INFO]   ----------------------------------------------
2026-02-02 02:35:05 [INFO]   ----------------------------------------------
2026-02-02 02:35:05 [INFO]   Linting [PYTHON_BLACK] files...
2026-02-02 02:35:05 [INFO]   ----------------------------------------------
2026-02-02 02:35:05 [INFO]   ----------------------------------------------
2026-02-02 02:35:05 [INFO]   ---------------------------
2026-02-02 02:35:05 [INFO]   File:[/github/workspace/casbin/core_enforcer.py]
2026-02-02 02:35:05 [INFO]    - File:[core_enforcer.py] was linted with [black] successfully
2026-02-02 02:35:05 [INFO]      - Command output:
------
All done! ✨ 🍰 ✨
1 file would be left unchanged.
------
2026-02-02 02:35:05 [INFO]   ---------------------------
2026-02-02 02:35:05 [INFO]   File:[/github/workspace/tests/test_enforcer.py]
2026-02-02 02:35:06 [ERROR]   Found errors in [black] linter!
2026-02-02 02:35:06 [ERROR]   Error code: 1. Command output:
------
--- /github/workspace/tests/test_enforcer.py	2026-02-02 02:35:04.789837+00:00
+++ /github/workspace/tests/test_enforcer.py	2026-02-02 02:35:06.940858+00:00
@@ -580,10 +580,11 @@
         import logging
 
         # Create a custom class that tracks str() calls
         class TrackedObject(str):
             """A string subclass that tracks when str() is called."""
+
             str_call_count = 0
 
             def __new__(cls, value):
                 instance = super().__new__(cls, value)
                 return instance
@@ -596,11 +597,11 @@
             get_examples("basic_model.conf"),
             get_examples("basic_policy.csv"),
         )
 
         # Get the actual enforcer (for SyncedEnforcer, it's wrapped in _e)
-        actual_enforcer = e._e if hasattr(e, '_e') else e
+        actual_enforcer = e._e if hasattr(e, "_e") else e
 
         # Set logger to ERROR level (above INFO and WARNING)
         actual_enforcer.logger.disabled = False
         actual_enforcer.logger.setLevel(logging.ERROR)
 
would reformat /github/workspace/tests/test_enforcer.py

Oh no! 💥 💔 💥
1 file would be reformatted.
------
2026-02-02 02:35:07 [INFO]   ----------------------------------------------
2026-02-02 02:35:07 [INFO]   ----------------------------------------------
2026-02-02 02:35:07 [INFO]   The script has completed
2026-02-02 02:35:07 [INFO]   ----------------------------------------------
2026-02-02 02:35:07 [INFO]   ----------------------------------------------
2026-02-02 02:35:07 [ERROR]   ERRORS FOUND in PYTHON_BLACK:[1]
2026-02-02 02:35:07 [FATAL]   Exiting with errors found!

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 2, 2026

@copilot fix: https://github.com/casbin/pycasbin/actions/runs/21575342377/job/62161536360

...

Fixed the Black formatting issue in tests/test_enforcer.py. The problem was:

  1. Missing blank line after docstring in the inner TrackedObject class
  2. Inconsistent quote style (single vs double quotes)

Applied Black formatting and verified all tests still pass. (48abb7f)

@hsluoyz hsluoyz changed the title Avoid expensive str() calls in enforce_ex when logging is disabled feat: avoid expensive str() calls in enforce_ex when logging is disabled Feb 2, 2026
@hsluoyz hsluoyz merged commit feccbc2 into master Feb 2, 2026
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

str(v) in core_enforcer.enforce_ex() is done even when not needed resulting in unnecessary code execution and DB calls

3 participants