Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3595274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:49:50+00:00 2026-05-18T19:49:50+00:00

What’s an accurate way of checking whether an object can be atomically pickled? When

  • 0

What’s an accurate way of checking whether an object can be atomically pickled? When I say “atomically pickled”, I mean without considering other objects it may refer to. For example, this list:

l = [threading.Lock()]

is not a a pickleable object, because it refers to a Lock which is not pickleable. But atomically, this list itself is pickleable.

So how do you check whether an object is atomically pickleable? (I’m guessing the check should be done on the class, but I’m not sure.)

I want it to behave like this:

>>> is_atomically_pickleable(3)
True
>>> is_atomically_pickleable(3.1)
True
>>> is_atomically_pickleable([1, 2, 3])
True
>>> is_atomically_pickleable(threading.Lock())
False
>>> is_atomically_pickleable(open('whatever', 'r'))
False

Etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:49:51+00:00Added an answer on May 18, 2026 at 7:49 pm

    I ended up coding my own solution to this.

    Here’s the code. Here are the tests. It’s part of GarlicSim, so you can use it by installing garlicsim and doing from garlicsim.general_misc import pickle_tools.

    If you want to use it on Python 3 code, use the Python 3 fork of garlicsim.

    Here is an excerpt from the module (may be outdated):

    import re
    import cPickle as pickle_module
    import pickle # Importing just to get dispatch table, not pickling with it.
    import copy_reg
    import types
    
    from garlicsim.general_misc import address_tools
    from garlicsim.general_misc import misc_tools
    
    
    def is_atomically_pickleable(thing):
        '''
        Return whether `thing` is an atomically pickleable object.
    
        "Atomically-pickleable" means that it's pickleable without considering any
        other object that it contains or refers to. For example, a `list` is
        atomically pickleable, even if it contains an unpickleable object, like a
        `threading.Lock()`.
    
        However, the `threading.Lock()` itself is not atomically pickleable.
        '''
        my_type = misc_tools.get_actual_type(thing)
        return _is_type_atomically_pickleable(my_type, thing)
    
    
    def _is_type_atomically_pickleable(type_, thing=None):
        '''Return whether `type_` is an atomically pickleable type.'''
        try:
            return _is_type_atomically_pickleable.cache[type_]
        except KeyError:
            pass
    
        if thing is not None:
            assert isinstance(thing, type_)
    
        # Sub-function in order to do caching without crowding the main algorithm:
        def get_result():
    
            # We allow a flag for types to painlessly declare whether they're
            # atomically pickleable:
            if hasattr(type_, '_is_atomically_pickleable'):
                return type_._is_atomically_pickleable
    
            # Weird special case: `threading.Lock` objects don't have `__class__`.
            # We assume that objects that don't have `__class__` can't be pickled.
            # (With the exception of old-style classes themselves.)
            if not hasattr(thing, '__class__') and \
               (not isinstance(thing, types.ClassType)):
                return False
    
            if not issubclass(type_, object):
                return True
    
            def assert_legit_pickling_exception(exception):
                '''Assert that `exception` reports a problem in pickling.'''
                message = exception.args[0]
                segments = [
                    "can't pickle",
                    'should only be shared between processes through inheritance',
                    'cannot be passed between processes or pickled'
                ]
                assert any((segment in message) for segment in segments)
                # todo: turn to warning
    
            if type_ in pickle.Pickler.dispatch:
                return True
    
            reduce_function = copy_reg.dispatch_table.get(type_)
            if reduce_function:
                try:
                    reduce_result = reduce_function(thing)
                except Exception, exception:
                    assert_legit_pickling_exception(exception)
                    return False
                else:
                    return True
    
            reduce_function = getattr(type_, '__reduce_ex__', None)
            if reduce_function:
                try:
                    reduce_result = reduce_function(thing, 0)
                    # (The `0` is the protocol argument.)
                except Exception, exception:
                    assert_legit_pickling_exception(exception)
                    return False
                else:
                    return True
    
            reduce_function = getattr(type_, '__reduce__', None)
            if reduce_function:
                try:
                    reduce_result = reduce_function(thing)
                except Exception, exception:
                    assert_legit_pickling_exception(exception)
                    return False
                else:
                    return True
    
            return False
    
        result = get_result()
        _is_type_atomically_pickleable.cache[type_] = result
        return result
    
    _is_type_atomically_pickleable.cache = {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.