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

  • SEARCH
  • Home
  • 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 254431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:50:45+00:00 2026-05-11T21:50:45+00:00

I have an object with a CookieJar that I want to pickle. However as

  • 0

I have an object with a CookieJar that I want to pickle.

However as you all probably know, pickle chokes on objects that contain lock objects. And for some horrible reason, a CookieJar has a lock object.

from cPickle import dumps
from cookielib import CookieJar

class Person(object):
    def __init__(self, name):
        self.name = name
        self.cookies = CookieJar()

bob = Person("bob")
dumps(bob)

# Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
# cPickle.UnpickleableError: Cannot pickle <type 'thread.lock'> objects

How do I persist this?

The only solution I can think of is to use FileCookieJar.save and FileCookieJar.load to a stringIO object. But is there a better way?

  • 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-11T21:50:46+00:00Added an answer on May 11, 2026 at 9:50 pm

    Here is an attempt, by deriving a class from CookieJar, which override getstate/setstate used by pickle. I haven’t used cookieJar, so don’t know if it is usable but you can dump derived class

    from cPickle import dumps
    from cookielib import CookieJar
    import threading
    
    class MyCookieJar(CookieJar):
        def __getstate__(self):
            state = self.__dict__.copy()
            del state['_cookies_lock']
            return state
    
        def __setstate__(self, state):
            self.__dict__ = state
            self._cookies_lock = threading.RLock()
    
    class Person(object):
        def __init__(self, name):
            self.name = name
            self.cookies = MyCookieJar()
    
    bob = Person("bob")
    print dumps(bob)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that holds several value types and other objects. I want
I think I read somewhere that some modules only have object oriented interfaces (
I have two tables: object that has object_id column and avalues that have object_id
I have an object Document with nested Properties (Name, Value) collection. Now I want
I have an object that cannot be copied, a NetGame because it has a
I have an object that has pairs of replacement values used for simple encoding
Assuming I have object_list list which contains objects. I want to check if my
Let's imagine that we have object Animal $.Animal = function(options) { this.defaults = {
I have a CookieContainer extracted from a HttpWebRequest/HttpWebResponse session named CookieJar . I want
I have a script that should be able to do some calls to a

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.