Code Monkey home page Code Monkey logo

python-pickle's People

Contributors

noteed avatar tacopeland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

python-pickle's Issues

Prelude.undefined while unpickling

Hi, I am running this code on a protocol 2 file:

main :: IO ()
main = do putStrLn "Start"
          byteString <- S.readFile worldFileName :: IO S.ByteString
          let res = unpickle byteString
          case res of
               Left err -> putStrLn $ "Can't unpickle .\nUnpickling error:\n " ++ err
               Right v -> putStrLn "Well done!"
          putStrLn "Done"

And I keep getting:

Start
civs: Prelude.undefined

where civs is the name of my app. Do you know what I am doing wrong?

Python RestrictedUnpickler issue

We had written code as -

import os
import builtins
import pickle
import sys
sys.tracebacklimit=0
import traceback
import io
from logging import Logger

safe_builtins = {
        'range',
        'complex',
        'set',
        'frozenset'
        }

class RestrictedUnpickler(pickle.Unpickler):
  def find_class(self, module, name):
  # Only allow safe classes from builtins.
   if module == "builtins" and name in safe_builtins:
       return getattr(builtins, name)
    # Forbid everything else.
   raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
   (module, name))

def restricted_loads(s):
    """Helper function analogous to pickle.loads()."""
    return RestrictedUnpickler(io.BytesIO(s)).load()

def func1(a): 
  try: 
    x= restricted_loads(pickle.dumps(a))
    return x 
  except pickle.UnpicklingError : 
    s= pickle.UnpicklingError("unsupported persistent object") 
    return s

def func2(s):
  try:
    x=restricted_loads(pickle.dumps(s))
    return s[x]
  except pickle.UnpicklingError :
    s=traceback.format_exc()
    return s
    
if __name__ == "__main__":
       a=range(int(input())) 
       b=func1(a)
       print(b)
       y=tuple(input())
       z=func2(y)
       print(z)

Getting
TypeError: tuple indices must be integers or slices, not tuple

Input (stdin)

50 
"a", "b", "c", "d", "e", "f", "g", "h"

Your Output (stdout)
range(0, 50)

Expected Output

range(0, 50) 
Traceback (most recent call last): 
_pickle.UnpicklingError: global 'builtins.slice' is forbidden

image

can't pickle instancemethod objects

Trying to save Random Forest model. (sklearn, python 2.7x)

All the ways fail :

def save_model(self, fname):
    with open(fname,'wb') as f :
        pickle.dump(self.model, f)



pickle: TypeError: can't pickle instancemethod objects

joblib : PicklingError: Can't pickle <type 'instancemethod'>: it's not found as __builtin__.instancemethod

cPickle : TypeError: can't pickle instancemethod objects

dill : ValueError: pickle protocol must be <= 2

.......
/usr/lib/python2.7/pickle.pyc in save(self, obj)
    284         f = self.dispatch.get(t)
    285         if f:
--> 286             f(self, obj) # Call unbound method with explicit self
    287             return
    288 

/usr/lib/python2.7/pickle.pyc in save_dict(self, obj)
    653 
    654         self.memoize(obj)
--> 655         self._batch_setitems(obj.iteritems())
    656 
    657     dispatch[DictionaryType] = save_dict

/usr/lib/python2.7/pickle.pyc in _batch_setitems(self, items)
    667             for k, v in items:
    668                 save(k)
--> 669                 save(v)
    670                 write(SETITEM)
    671             return

/usr/lib/python2.7/pickle.pyc in save(self, obj)
    304             reduce = getattr(obj, "__reduce_ex__", None)
    305             if reduce:
--> 306                 rv = reduce(self.proto)
    307             else:
    308                 reduce = getattr(obj, "__reduce__", None)

/usr/lib/python2.7/copy_reg.pyc in _reduce_ex(self, proto)
     68     else:
     69         if base is self.__class__:
---> 70             raise TypeError, "can't pickle %s objects" % base.__name__
     71         state = base(self)
     72     args = (self.__class__, base, state)

TypeError: can't pickle instancemethod objects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.