Skip to content

Add fickling.loads() public API function#173

Merged
thomas-chauchefoin-tob merged 3 commits intomasterfrom
add-fickling-loads-api
Nov 28, 2025
Merged

Add fickling.loads() public API function#173
thomas-chauchefoin-tob merged 3 commits intomasterfrom
add-fickling-loads-api

Conversation

@dhalf
Copy link
Copy Markdown
Contributor

@dhalf dhalf commented Nov 26, 2025

Add fickling.loads() Public API Function

Summary

This PR adds a new public API function fickling.loads() that provides safe loading of pickle data from bytes, completing the API to match Python's pickle module.

Problem

The fickling library currently only exposes fickling.load(file) for loading from file objects. While hooks for pickle.loads() exist internally, there's no corresponding public fickling.loads(data) API function for loading
from bytes data. This creates an incomplete API compared to Python's pickle module, which provides both load() and loads().

Solution

Added fickling.loads() function that:

  • Accepts bytes data (matching pickle.loads() signature)
  • Wraps bytes in io.BytesIO and delegates to existing load() function
  • Provides the same security analysis and safety guarantees as fickling.load()
  • Supports all the same parameters (max_acceptable_severity, print_results, json_output_path, etc.)

Changes

Core Implementation

  • fickling/loader.py: Added loads() function (42 lines) after existing load() function
  • fickling/init.py: Exported loads alongside load and updated documentation comment

Tests

  • test/test_loads_api.py: New test file with 4 comprehensive test cases:
    1. test_loads_safe_data - Verifies safe pickle data loads correctly
    2. test_loads_unsafe_data - Verifies unsafe data is rejected with UnsafeFileError
    3. test_loads_matches_pickle_signature - Tests compatibility with pickle.loads() arguments
    4. test_loads_with_custom_severity - Tests custom severity level support

Design Rationale

  • Code Reuse: Delegates to existing load() function to avoid duplication and maintain consistency
  • Simple Implementation: Just wraps bytes in io.BytesIO - all security logic remains in load()
  • API Completeness: Matches Python's pickle module interface for easy migration
  • Backward Compatible: No changes to existing functionality

Testing

All new tests pass:

$ pytest test/test_loads_api.py -v
============================== 4 passed in 0.02s ===============================

No regressions introduced - all previously passing tests still pass.

Usage Example

import pickle
import fickling

# Create pickle data
data = pickle.dumps([1, 2, 3])

# Before: Had to manually wrap in BytesIO
import io
obj = fickling.load(io.BytesIO(data))

# After: Direct drop-in replacement for pickle.loads()
obj = fickling.loads(data)

Benefits

1. Complete API - Now provides both load() and loads() like pickle
2. Better Ergonomics - No need to manually wrap bytes in BytesIO
3. Easy Migration - Drop-in replacement for pickle.loads()
4. Consistent Safety - Same security guarantees as fickling.load()

This commit adds a new public API function `fickling.loads()` that
mirrors Python's `pickle.loads()` signature while providing the same
safety analysis as the existing `fickling.load()` function.

Changes:
- Added loads() function to fickling/loader.py that wraps bytes in
  io.BytesIO and delegates to the existing load() function
- Exported loads from fickling/__init__.py alongside load
- Created comprehensive test suite in test/test_loads_api.py with
  4 test cases covering safe data, unsafe data, pickle signature
  compatibility, and custom severity levels

The implementation reuses the existing load() function to avoid code
duplication while maintaining identical safety guarantees. This
completes the public API to match Python's pickle module (both load
and loads functions).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@dhalf dhalf requested a review from ESultanik as a code owner November 26, 2025 10:25
@dhalf
Copy link
Copy Markdown
Contributor Author

dhalf commented Nov 26, 2025

#84

@thomas-chauchefoin-tob thomas-chauchefoin-tob linked an issue Nov 28, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Collaborator

@thomas-chauchefoin-tob thomas-chauchefoin-tob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the logic of the last test, let's merge this one as well.

@thomas-chauchefoin-tob thomas-chauchefoin-tob merged commit 5c062e9 into master Nov 28, 2025
14 checks passed
@thomas-chauchefoin-tob thomas-chauchefoin-tob deleted the add-fickling-loads-api branch November 28, 2025 14:42
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.

Support hooking pickle.loads() and add fickling.loads()

2 participants