Python pickle.PicklingError() Examples

The following are code examples for showing how to use pickle.PicklingError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: gipc   Author: jgehrcke   File: gipc.py    MIT License 6 votes vote down vote up
def put(self, o):
        """Encode object ``o`` and write it to the pipe.
        Block gevent-cooperatively until all data is written. The default
        encoder is ``pickle.dumps``.

        :arg o: a Python object that is encodable with the encoder of choice.

        Raises:
            - :exc:`GIPCError`
            - :exc:`GIPCClosed`
            - :exc:`pickle.PicklingError`

        """
        self._validate()
        with self._lock:
            bindata = self._encoder(o)
            self._write(struct.pack("!i", len(bindata)) + bindata) 
Example 2
Project: NiujiaoDebugger   Author: MrSrc   File: pickletester.py    GNU General Public License v3.0 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Python implementation is less strict and also accepts iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except pickle.PicklingError:
                pass
            try:
                self.dumps(D(), proto)
            except pickle.PicklingError:
                pass 
Example 3
Project: NiujiaoDebugger   Author: MrSrc   File: pickletester.py    GNU General Public License v3.0 6 votes vote down vote up
def test_local_lookup_error(self):
        # Test that whichmodule() errors out cleanly when looking up
        # an assumed globally-reachable object fails.
        def f():
            pass
        # Since the function is local, lookup will fail
        for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((AttributeError, pickle.PicklingError)):
                pickletools.dis(self.dumps(f, proto))
        # Same without a __module__ attribute (exercises a different path
        # in _pickle.c).
        del f.__module__
        for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((AttributeError, pickle.PicklingError)):
                pickletools.dis(self.dumps(f, proto))
        # Yet a different path.
        f.__name__ = f.__qualname__
        for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((AttributeError, pickle.PicklingError)):
                pickletools.dis(self.dumps(f, proto)) 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_xml_etree.py    GNU General Public License v3.0 6 votes vote down vote up
def pickleRoundTrip(self, obj, name, dumper, loader, proto):
        save_m = sys.modules[name]
        try:
            sys.modules[name] = dumper
            temp = pickle.dumps(obj, proto)
            sys.modules[name] = loader
            result = pickle.loads(temp)
        except pickle.PicklingError as pe:
            # pyET must be second, because pyET may be (equal to) ET.
            human = dict([(ET, "cET"), (pyET, "pyET")])
            raise support.TestFailed("Failed to round-trip %r from %r to %r"
                                     % (obj,
                                        human.get(dumper, dumper),
                                        human.get(loader, loader))) from pe
        finally:
            sys.modules[name] = save_m
        return result 
Example 5
Project: ironpython2   Author: IronLanguages   File: pickletester.py    Apache License 2.0 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Protocol 0 in Python implementation is less strict and also accepts
        # iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except (AttributeError, pickle.PicklingError, cPickle.PicklingError):
                pass
            try:
                self.dumps(D(), proto)
            except (AttributeError, pickle.PicklingError, cPickle.PicklingError):
                pass 
Example 6
Project: PADME   Author: simonfqy   File: tensor_graph.py    MIT License 6 votes vote down vote up
def get_pickling_errors(self, obj, seen=None):
    if seen == None:
      seen = []
    try:
      state = obj.__getstate__()
    except AttributeError:
      return
    if state == None:
      return
    if isinstance(state, tuple):
      if not isinstance(state[0], dict):
        state = state[1]
      else:
        state = state[0].update(state[1])
    result = {}
    for i in state:
      try:
        pickle.dumps(state[i], protocol=2)
      except pickle.PicklingError:
        if not state[i] in seen:
          seen.append(state[i])
          result[i] = self.get_pickling_errors(state[i], seen)
    return result 
Example 7
Project: async-ipython-magic   Author: leriomaggio   File: async_run_magic.py    MIT License 6 votes vote down vote up
def on_connected(self, f):
        """Callback fired /on_connection/ established.

        Once the connection to the websocket has been established,
        all the currenct namespace is pickled and written to the
        corresponding web_socket connection.
        """
        try:
            ws_conn = f.result()
            self.ws_conn = ws_conn
            data = {'connection_id': self.connection_id,
                    'nb_code_to_run_async': self.cell_source,}
            msg = json.dumps(data)
            ws_conn.write_message(message=msg)
            white_ns = self._pack_namespace()
            ws_conn.write_message(message=pickle_dumps(white_ns), binary=True)
        except PicklingError as e:
            print(str(e)) 
Example 8
Project: async-ipython-magic   Author: leriomaggio   File: async_run_magic.py    MIT License 6 votes vote down vote up
def _pack_namespace(self):
        """Collect all the /pickable/ objects from the namespace
        so to pass them to the async execution environment."""
        white_ns = dict()
        white_ns.setdefault('import_modules', list())
        for k, v in self.shell.user_ns.items():
            if not k in DEFAULT_BLACKLIST:
                try:
                    if inspect_ismodule(v):
                        white_ns['import_modules'].append((k, v.__name__))
                    else:
                        _ = pickle_dumps({k: v})
                        white_ns[k] = v
                except PicklingError:
                    continue
                except Exception:
                    continue
        white_ns['connection_id'] = self.connection_id
        return white_ns 
Example 9
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 6 votes vote down vote up
def save_global(self, obj, name=None, pack=struct.pack):
        # We have to override this method in order to deal with objects
        # defined interactively in IPython that are not injected in
        # __main__
        kwargs = dict(name=name, pack=pack)
        if sys.version_info >= (3, 4):
            del kwargs['pack']
        try:
            Pickler.save_global(self, obj, **kwargs)
        except pickle.PicklingError:
            Pickler.save_global(self, obj, **kwargs)
            module = getattr(obj, "__module__", None)
            if module == '__main__':
                my_name = name
                if my_name is None:
                    my_name = obj.__name__
                mod = sys.modules[module]
                if not hasattr(mod, my_name):
                    # IPython doesn't inject the variables define
                    # interactively in __main__
                    setattr(mod, my_name, obj) 
Example 10
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def save_func(self, obj):
        try:
            self.save_global(obj)
            return
        except pickle.PicklingError:
            pass
        assert type(obj) is types.FunctionType
        self.save(types.FunctionType)
        self.save((
            obj.func_code,
            obj.func_globals,
            obj.func_name,
            obj.func_defaults,
            obj.func_closure,
        ))
        self.write(pickle.REDUCE)
        if id(obj) not in self.memo:    # Could be if we recursively landed here. See also pickle.save_tuple().
            self.memoize(obj) 
Example 11
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 6 votes vote down vote up
def save_global(self, obj, name=None, pack=struct.pack):
        # We have to override this method in order to deal with objects
        # defined interactively in IPython that are not injected in
        # __main__
        kwargs = dict(name=name, pack=pack)
        if sys.version_info >= (3, 4):
            del kwargs['pack']
        try:
            Pickler.save_global(self, obj, **kwargs)
        except pickle.PicklingError:
            Pickler.save_global(self, obj, **kwargs)
            module = getattr(obj, "__module__", None)
            if module == '__main__':
                my_name = name
                if my_name is None:
                    my_name = obj.__name__
                mod = sys.modules[module]
                if not hasattr(mod, my_name):
                    # IPython doesn't inject the variables define
                    # interactively in __main__
                    setattr(mod, my_name, obj) 
Example 12
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: pickletester.py    GNU General Public License v2.0 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Python implementation is less strict and also accepts iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except pickle.PicklingError:
                pass
            try:
                self.dumps(D(), proto)
            except pickle.PicklingError:
                pass 
Example 13
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: pickletester.py    GNU General Public License v2.0 6 votes vote down vote up
def test_local_lookup_error(self):
        # Test that whichmodule() errors out cleanly when looking up
        # an assumed globally-reachable object fails.
        def f():
            pass
        # Since the function is local, lookup will fail
        for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((AttributeError, pickle.PicklingError)):
                pickletools.dis(self.dumps(f, proto))
        # Same without a __module__ attribute (exercises a different path
        # in _pickle.c).
        del f.__module__
        for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((AttributeError, pickle.PicklingError)):
                pickletools.dis(self.dumps(f, proto))
        # Yet a different path.
        f.__name__ = f.__qualname__
        for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((AttributeError, pickle.PicklingError)):
                pickletools.dis(self.dumps(f, proto)) 
Example 14
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_xml_etree.py    GNU General Public License v2.0 6 votes vote down vote up
def pickleRoundTrip(self, obj, name, dumper, loader, proto):
        save_m = sys.modules[name]
        try:
            sys.modules[name] = dumper
            temp = pickle.dumps(obj, proto)
            sys.modules[name] = loader
            result = pickle.loads(temp)
        except pickle.PicklingError as pe:
            # pyET must be second, because pyET may be (equal to) ET.
            human = dict([(ET, "cET"), (pyET, "pyET")])
            raise support.TestFailed("Failed to round-trip %r from %r to %r"
                                     % (obj,
                                        human.get(dumper, dumper),
                                        human.get(loader, loader))) from pe
        finally:
            sys.modules[name] = save_m
        return result 
Example 15
Project: latigo   Author: equinor   File: __init__.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def serialize_task(task, mode="json") -> typing.Optional[bytes]:
    """
    Serialize a task to bytes
    """
    task_bytes = None
    if mode == "pickle":
        try:
            task_bytes = pickle.dumps(task)
        except pickle.PicklingError as e:
            logger.error(f"Could not serialize task to json pickle: {e}")
            traceback.print_exc()
    else:
        try:
            # Rely on dataclass_json
            task_bytes = task.to_json()
        except Exception as e:
            logger.error(f"Could not serialize task to json: {e}")
            traceback.print_exc()
    return task_bytes 
Example 16
Project: bigquerylayers   Author: smandaric   File: client.py    GNU General Public License v3.0 5 votes vote down vote up
def __getstate__(self):
        """Explicitly state that clients are not pickleable."""
        raise PicklingError(
            "\n".join(
                [
                    "Pickling client objects is explicitly not supported.",
                    "Clients have non-trivial state that is local and unpickleable.",
                ]
            )
        ) 
Example 17
Project: pick   Author: bernardorufino   File: test_enum.py    MIT License 5 votes vote down vote up
def test_class_nested_enum_and_pickle_protocol_four(self):
            # would normally just have this directly in the class namespace
            class NestedEnum(Enum):
                twigs = 'common'
                shiny = 'rare'

            self.__class__.NestedEnum = NestedEnum
            self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__
            test_pickle_exception(
                    self.assertRaises, PicklingError, self.NestedEnum.twigs,
                    protocol=(0, 3))
            test_pickle_dump_load(self.assertTrue, self.NestedEnum.twigs,
                    protocol=(4, HIGHEST_PROTOCOL)) 
Example 18
Project: pick   Author: bernardorufino   File: test_enum.py    MIT License 5 votes vote down vote up
def test_exploding_pickle(self):
        BadPickle = Enum('BadPickle', 'dill sweet bread-n-butter')
        enum._make_class_unpicklable(BadPickle)
        globals()['BadPickle'] = BadPickle
        test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill)
        test_pickle_exception(self.assertRaises, PicklingError, BadPickle) 
Example 19
Project: pywren-ibm-cloud   Author: pywren   File: cloudpickle.py    Apache License 2.0 5 votes vote down vote up
def dump(self, obj):
        self.inject_addons()
        try:
            return Pickler.dump(self, obj)
        except RuntimeError as e:
            if 'recursion' in e.args[0]:
                msg = """Could not pickle object as excessively deep recursion required."""
                raise pickle.PicklingError(msg)
            else:
                raise 
Example 20
Project: pywren-ibm-cloud   Author: pywren   File: cloudpickle.py    Apache License 2.0 5 votes vote down vote up
def save_file(self, obj):
        """Save a file"""
        try:
            import StringIO as pystringIO  # we can't use cStringIO as it lacks the name attribute
        except ImportError:
            import io as pystringIO

        if not hasattr(obj, 'name') or not hasattr(obj, 'mode'):
            raise pickle.PicklingError("Cannot pickle files that do not map to an actual file")
        if obj is sys.stdout:
            return self.save_reduce(getattr, (sys, 'stdout'), obj=obj)
        if obj is sys.stderr:
            return self.save_reduce(getattr, (sys, 'stderr'), obj=obj)
        if obj is sys.stdin:
            raise pickle.PicklingError("Cannot pickle standard input")
        if obj.closed:
            raise pickle.PicklingError("Cannot pickle closed files")
        if hasattr(obj, 'isatty') and obj.isatty():
            raise pickle.PicklingError("Cannot pickle files that map to tty objects")
        if 'r' not in obj.mode and '+' not in obj.mode:
            raise pickle.PicklingError("Cannot pickle files that are not opened for reading: %s" % obj.mode)

        name = obj.name

        retval = pystringIO.StringIO()

        try:
            # Read the whole file
            curloc = obj.tell()
            obj.seek(0)
            contents = obj.read()
            obj.seek(curloc)
        except IOError:
            raise pickle.PicklingError("Cannot pickle file %s as it cannot be read" % name)
        retval.write(contents)
        retval.seek(curloc)

        retval.name = name
        self.save(retval)
        self.memoize(obj) 
Example 21
Project: NiujiaoDebugger   Author: MrSrc   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def __reduce__(self):
        # In general, only the root logger will not be accessible via its name.
        # However, the root logger's class has its own __reduce__ method.
        if getLogger(self.name) is not self:
            import pickle
            raise pickle.PicklingError('logger cannot be pickled')
        return getLogger, (self.name,) 
Example 22
Project: NiujiaoDebugger   Author: MrSrc   File: test_zlib.py    GNU General Public License v3.0 5 votes vote down vote up
def test_compresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto) 
Example 23
Project: NiujiaoDebugger   Author: MrSrc   File: test_zlib.py    GNU General Public License v3.0 5 votes vote down vote up
def test_decompresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.decompressobj(), proto)

    # Memory use of the following functions takes into account overallocation 
Example 24
Project: NiujiaoDebugger   Author: MrSrc   File: test_coroutines.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pickle(self):
        async def func(): pass
        coro = func()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(coro, proto)

        aw = coro.__await__()
        try:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                with self.assertRaises((TypeError, pickle.PicklingError)):
                    pickle.dumps(aw, proto)
        finally:
            aw.close() 
Example 25
Project: NiujiaoDebugger   Author: MrSrc   File: test_concurrent_futures.py    GNU General Public License v3.0 5 votes vote down vote up
def __reduce__(self):
        from pickle import PicklingError
        raise PicklingError("Error in pickle") 
Example 26
Project: NiujiaoDebugger   Author: MrSrc   File: test_enum.py    GNU General Public License v3.0 5 votes vote down vote up
def test_exploding_pickle(self):
        BadPickle = Enum(
                'BadPickle', 'dill sweet bread-n-butter', module=__name__)
        globals()['BadPickle'] = BadPickle
        # now break BadPickle to test exception raising
        enum._make_class_unpicklable(BadPickle)
        test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill)
        test_pickle_exception(self.assertRaises, PicklingError, BadPickle) 
Example 27
Project: NiujiaoDebugger   Author: MrSrc   File: pickletester.py    GNU General Public License v3.0 5 votes vote down vote up
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(self.Pickler):
            def __init__(self): pass

        class BadUnpickler(self.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load) 
Example 28
Project: NiujiaoDebugger   Author: MrSrc   File: test_generators.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pickle(self):
        def f():
            yield 1
        g = f()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(g, proto) 
Example 29
Project: NiujiaoDebugger   Author: MrSrc   File: test_xml_etree.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pickle(self):
        a = ET.Element('a')
        it = a.iter()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(it, proto) 
Example 30
Project: NiujiaoDebugger   Author: MrSrc   File: test_dictviews.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pickle(self):
        d = {1: 10, "a": "ABC"}
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            self.assertRaises((TypeError, pickle.PicklingError),
                pickle.dumps, d.keys(), proto)
            self.assertRaises((TypeError, pickle.PicklingError),
                pickle.dumps, d.values(), proto)
            self.assertRaises((TypeError, pickle.PicklingError),
                pickle.dumps, d.items(), proto) 
Example 31
Project: NiujiaoDebugger   Author: MrSrc   File: rpc.py    GNU General Public License v3.0 5 votes vote down vote up
def putmessage(self, message):
        self.debug("putmessage:%d:" % message[0])
        try:
            s = dumps(message)
        except pickle.PicklingError:
            print("Cannot pickle:", repr(message), file=sys.__stderr__)
            raise
        s = struct.pack("<i", len(s)) + s
        while len(s) > 0:
            try:
                r, w, x = select.select([], [self.sock], [])
                n = self.sock.send(s[:BUFSIZE])
            except (AttributeError, TypeError):
                raise OSError("socket no longer exists")
            s = s[n:] 
Example 32
Project: mendelmd   Author: raonyguimaraes   File: forms.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_to_cache(self):
        """
        Add widget object to Django's cache.

        You may need to overwrite this method, to pickle all information
        that is required to serve your JSON response view.
        """
        try:
            cache.set(self._get_cache_key(), {
                'widget': self,
                'url': self.get_url(),
            })
        except (PicklingError, cPicklingError, AttributeError):
            msg = "You need to overwrite \"set_to_cache\" or ensure that %s is serialisable."
            raise NotImplementedError(msg % self.__class__.__name__) 
Example 33
Project: mendelmd   Author: raonyguimaraes   File: forms.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_to_cache(self):
        """
        Add widget object to Django's cache.

        You may need to overwrite this method, to pickle all information
        that is required to serve your JSON response view.
        """
        try:
            cache.set(self._get_cache_key(), {
                'widget': self,
                'url': self.get_url(),
            })
        except (PicklingError, cPicklingError, AttributeError):
            msg = "You need to overwrite \"set_to_cache\" or ensure that %s is serialisable."
            raise NotImplementedError(msg % self.__class__.__name__) 
Example 34
Project: mendelmd   Author: raonyguimaraes   File: forms.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_to_cache(self):
        """
        Add widget object to Django's cache.

        You may need to overwrite this method, to pickle all information
        that is required to serve your JSON response view.
        """
        try:
            cache.set(self._get_cache_key(), {
                'widget': self,
                'url': self.get_url(),
            })
        except (PicklingError, cPicklingError, AttributeError):
            msg = "You need to overwrite \"set_to_cache\" or ensure that %s is serialisable."
            raise NotImplementedError(msg % self.__class__.__name__) 
Example 35
Project: vagrant-prometheus-lab   Author: alexclear   File: core.py    MIT License 5 votes vote down vote up
def pickled(cls, obj, **kwargs):
        self = cls(**kwargs)
        try:
            self.data = pickle.dumps(obj, protocol=2)
        except pickle.PicklingError:
            e = sys.exc_info()[1]
            self.data = pickle.dumps(CallError(e), protocol=2)
        return self 
Example 36
Project: ycml   Author: skylander86   File: base.py    Apache License 2.0 5 votes vote down vote up
def save(self, f):
        if not hasattr(self, 'uuid_'):
            raise NotFittedError('This featurizer is not fitted yet.')

        with tarfile.open(fileobj=f, mode='w') as tf:
            with BytesIO() as model_f:
                try: pickle.dump(self, model_f, protocol=4)
                except pickle.PicklingError:
                    logger.error('PicklingError: Did you check to make sure that the classifier mixins (i.e., KerasNNClassifierMixin) is ahead of BaseClassifier in the MRO?')
                    raise
                #end try

                model_data = model_f.getvalue()
                model_f.seek(0)
                model_tarinfo = tarfile.TarInfo(name='model.pkl')
                model_tarinfo.size = len(model_data)
                model_tarinfo.mtime = int(time.time())
                tf.addfile(tarinfo=model_tarinfo, fileobj=model_f)
            #end with

            self.save_to_tarfile(tf)
        #end with

        f.close()

        logger.info('{} saved to <{}>.'.format(self, f.name))

        return self
    #end def 
Example 37
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_RSA.py    MIT License 5 votes vote down vote up
def test_serialization(self):
        """RSA keys are unpickable"""

        rsa_key = self.rsa.generate(1024)
        self.assertRaises(PicklingError, pickle.dumps, rsa_key) 
Example 38
Project: ironpython2   Author: IronLanguages   File: test_zlib.py    Apache License 2.0 5 votes vote down vote up
def test_compresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto) 
Example 39
Project: ironpython2   Author: IronLanguages   File: test_zlib.py    Apache License 2.0 5 votes vote down vote up
def test_decompresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.decompressobj(), proto)

    # Memory use of the following functions takes into account overallocation 
Example 40
Project: ironpython2   Author: IronLanguages   File: test_xml_etree.py    Apache License 2.0 5 votes vote down vote up
def test_pickle(self):
        a = ET.Element('a')
        it = a.iter()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(it, proto) 
Example 41
Project: ironpython2   Author: IronLanguages   File: test_dictviews.py    Apache License 2.0 5 votes vote down vote up
def test_pickle(self):
        d = {1: 10, "a": "ABC"}
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            self.assertRaises((TypeError, pickle.PicklingError),
                pickle.dumps, d.viewkeys(), proto)
            self.assertRaises((TypeError, pickle.PicklingError),
                pickle.dumps, d.viewvalues(), proto)
            self.assertRaises((TypeError, pickle.PicklingError),
                pickle.dumps, d.viewitems(), proto) 
Example 42
Project: open-recipe   Author: dspray95   File: test_styles.py    The Unlicense 5 votes vote down vote up
def test_handledByPickleModule(self):
        """
        Handling L{pickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        self.assertRaises(pickle.PicklingError,
                          self.raise_UniversalPicklingError) 
Example 43
Project: open-recipe   Author: dspray95   File: test_styles.py    The Unlicense 5 votes vote down vote up
def test_handledBycPickleModule(self):
        """
        Handling L{cPickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        try:
            import cPickle
        except ImportError:
            raise unittest.SkipTest("cPickle not available.")
        else:
            self.assertRaises(cPickle.PicklingError,
                              self.raise_UniversalPicklingError) 
Example 44
Project: open-recipe   Author: dspray95   File: test_styles.py    The Unlicense 5 votes vote down vote up
def test_lambdaRaisesPicklingError(self):
        """
        Pickling a C{lambda} function ought to raise a L{pickle.PicklingError}.
        """
        self.assertRaises(pickle.PicklingError, pickle.dumps, lambdaExample)
        try:
            import cPickle
        except:
            pass
        else:
            self.assertRaises(cPickle.PicklingError, cPickle.dumps,
                              lambdaExample) 
Example 45
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_zlib.py    GNU General Public License v3.0 5 votes vote down vote up
def test_compresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto) 
Example 46
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_zlib.py    GNU General Public License v3.0 5 votes vote down vote up
def test_decompresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.decompressobj(), proto)

    # Memory use of the following functions takes into account overallocation 
Example 47
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_serialization.py    GNU General Public License v3.0 5 votes vote down vote up
def test_curried_bad_qualname():
    @toolz.curry
    class Bad(object):
        __qualname__ = 'toolz.functoolz.not.a.valid.path'

    assert raises(pickle.PicklingError, lambda: pickle.dumps(Bad)) 
Example 48
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_RSA.py    GNU General Public License v3.0 5 votes vote down vote up
def test_serialization(self):
        """RSA keys are unpickable"""

        rsa_key = self.rsa.generate(1024)
        self.assertRaises(PicklingError, pickle.dumps, rsa_key) 
Example 49
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_serialization.py    GNU General Public License v3.0 5 votes vote down vote up
def test_curried_bad_qualname():
    @cytoolz.curry
    class Bad(object):
        __qualname__ = 'cytoolz.functoolz.not.a.valid.path'

    assert raises(pickle.PicklingError, lambda: pickle.dumps(Bad)) 
Example 50
Project: tweet-the-wolf   Author: theshteves   File: test_enum.py    MIT License 5 votes vote down vote up
def test_class_nested_enum_and_pickle_protocol_four(self):
            # would normally just have this directly in the class namespace
            class NestedEnum(Enum):
                twigs = 'common'
                shiny = 'rare'

            self.__class__.NestedEnum = NestedEnum
            self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__
            test_pickle_exception(
                    self.assertRaises, PicklingError, self.NestedEnum.twigs,
                    protocol=(0, 3))
            test_pickle_dump_load(self.assertTrue, self.NestedEnum.twigs,
                    protocol=(4, HIGHEST_PROTOCOL)) 
Example 51
Project: tweet-the-wolf   Author: theshteves   File: test_enum.py    MIT License 5 votes vote down vote up
def test_exploding_pickle(self):
        BadPickle = Enum('BadPickle', 'dill sweet bread-n-butter')
        enum._make_class_unpicklable(BadPickle)
        globals()['BadPickle'] = BadPickle
        test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill)
        test_pickle_exception(self.assertRaises, PicklingError, BadPickle) 
Example 52
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 5 votes vote down vote up
def hash(self, obj, return_digest=True):
        try:
            self.dump(obj)
        except pickle.PicklingError as e:
            e.args += ('PicklingError while hashing %r: %r' % (obj, e),)
            raise
        dumps = self.stream.getvalue()
        self._hash.update(dumps)
        if return_digest:
            return self._hash.hexdigest() 
Example 53
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_method(self, obj):
        try:
            self.save_global(obj)
            return
        except pickle.PicklingError:
            pass
        assert type(obj) is types.MethodType
        self.save(types.MethodType)
        self.save((obj.im_func, obj.im_self, obj.im_class))
        self.write(pickle.REDUCE)
        self.memoize(obj) 
Example 54
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_module(self, obj):
        modname = getModNameForModDict(obj.__dict__)
        if modname:
            self.save(import_module)
            self.save((modname,))
            self.write(pickle.REDUCE)
            self.memoize(obj)
            return
        # We could maybe construct it manually. For now, just fail.
        raise pickle.PicklingError('cannot pickle module %r' % obj) 
Example 55
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_global(self, obj, name=None, pack=pickle.struct.pack):
        assert obj
        assert id(obj) not in self.memo
        if name is None:
            name = obj.__name__

        module = getattr(obj, "__module__", None)
        if module is None or module == "__main__":
            module = pickle.whichmodule(obj, name)
        if module is None or module == "__main__":
            raise pickle.PicklingError(
                    "Can't pickle %r: module not found: %s" % (obj, module))

        try:
            __import__(module)
            mod = sys.modules[module]
            klass = getattr(mod, name)
        except (ImportError, KeyError, AttributeError):
            raise pickle.PicklingError(
                    "Can't pickle %r: it's not found as %s.%s" % (obj, module, name))
        else:
            if klass is not obj:
                raise pickle.PicklingError(
                        "Can't pickle %r: it's not the same object as %s.%s" % (obj, module, name))

        assert "\n" not in module
        assert "\n" not in name
        self.write(pickle.GLOBAL + module + '\n' + name + '\n')
        self.memoize(obj)

    # Some types in the types modules are not correctly referenced,
    # such as types.FunctionType. This is fixed here. 
Example 56
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fixedsave_type(self, obj):
        try:
            self.save_global(obj)
            return
        except pickle.PicklingError:
            pass
        for modname in ["types"]:
            moddict = sys.modules[modname].__dict__
            for modobjname,modobj in moddict.iteritems():
                if modobj is obj:
                    self.write(pickle.GLOBAL + modname + '\n' + modobjname + '\n')
                    self.memoize(obj)
                    return
        self.save_global(obj) 
Example 57
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 5 votes vote down vote up
def hash(self, obj, return_digest=True):
        try:
            self.dump(obj)
        except pickle.PicklingError as e:
            e.args += ('PicklingError while hashing %r: %r' % (obj, e),)
            raise
        dumps = self.stream.getvalue()
        self._hash.update(dumps)
        if return_digest:
            return self._hash.hexdigest() 
Example 58
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: __init__.py    GNU General Public License v2.0 5 votes vote down vote up
def __reduce__(self):
        # In general, only the root logger will not be accessible via its name.
        # However, the root logger's class has its own __reduce__ method.
        if getLogger(self.name) is not self:
            import pickle
            raise pickle.PicklingError('logger cannot be pickled')
        return getLogger, (self.name,) 
Example 59
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_zlib.py    GNU General Public License v2.0 5 votes vote down vote up
def test_compresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto) 
Example 60
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_zlib.py    GNU General Public License v2.0 5 votes vote down vote up
def test_decompresspickle(self):
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(zlib.decompressobj(), proto)

    # Memory use of the following functions takes into account overallocation 
Example 61
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_coroutines.py    GNU General Public License v2.0 5 votes vote down vote up
def test_pickle(self):
        async def func(): pass
        coro = func()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(coro, proto)

        aw = coro.__await__()
        try:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                with self.assertRaises((TypeError, pickle.PicklingError)):
                    pickle.dumps(aw, proto)
        finally:
            aw.close() 
Example 62
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_concurrent_futures.py    GNU General Public License v2.0 5 votes vote down vote up
def __reduce__(self):
        from pickle import PicklingError
        raise PicklingError("Error in pickle") 
Example 63
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_enum.py    GNU General Public License v2.0 5 votes vote down vote up
def test_exploding_pickle(self):
        BadPickle = Enum(
                'BadPickle', 'dill sweet bread-n-butter', module=__name__)
        globals()['BadPickle'] = BadPickle
        # now break BadPickle to test exception raising
        enum._make_class_unpicklable(BadPickle)
        test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill)
        test_pickle_exception(self.assertRaises, PicklingError, BadPickle) 
Example 64
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: pickletester.py    GNU General Public License v2.0 5 votes vote down vote up
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(self.Pickler):
            def __init__(self): pass

        class BadUnpickler(self.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load) 
Example 65
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_generators.py    GNU General Public License v2.0 5 votes vote down vote up
def test_pickle(self):
        def f():
            yield 1
        g = f()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(g, proto) 
Example 66
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_xml_etree.py    GNU General Public License v2.0 5 votes vote down vote up
def test_pickle(self):
        a = ET.Element('a')
        it = a.iter()
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            with self.assertRaises((TypeError, pickle.PicklingError)):
                pickle.dumps(it, proto) 
Example 67
Project: aws-lambda-runtime-pypy   Author: uscheller   File: rpc.py    Apache License 2.0 5 votes vote down vote up
def putmessage(self, message):
        self.debug("putmessage:%d:" % message[0])
        try:
            s = dumps(message)
        except pickle.PicklingError:
            print("Cannot pickle:", repr(message), file=sys.__stderr__)
            raise
        s = struct.pack("<i", len(s)) + s
        while len(s) > 0:
            try:
                r, w, x = select.select([], [self.sock], [])
                n = self.sock.send(s[:BUFSIZE])
            except (AttributeError, TypeError):
                raise OSError("socket no longer exists")
            s = s[n:] 
Example 68
Project: oss-ftp   Author: aliyun   File: test_enum.py    MIT License 5 votes vote down vote up
def test_class_nested_enum_and_pickle_protocol_four(self):
            # would normally just have this directly in the class namespace
            class NestedEnum(Enum):
                twigs = 'common'
                shiny = 'rare'

            self.__class__.NestedEnum = NestedEnum
            self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__
            test_pickle_exception(
                    self.assertRaises, PicklingError, self.NestedEnum.twigs,
                    protocol=(0, 3))
            test_pickle_dump_load(self.assertTrue, self.NestedEnum.twigs,
                    protocol=(4, HIGHEST_PROTOCOL)) 
Example 69
Project: oss-ftp   Author: aliyun   File: test_enum.py    MIT License 5 votes vote down vote up
def test_exploding_pickle(self):
        BadPickle = Enum('BadPickle', 'dill sweet bread-n-butter')
        enum._make_class_unpicklable(BadPickle)
        globals()['BadPickle'] = BadPickle
        test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill)
        test_pickle_exception(self.assertRaises, PicklingError, BadPickle) 
Example 70
Project: Safejumper-for-Desktop   Author: proxysh   File: test_styles.py    GNU General Public License v2.0 5 votes vote down vote up
def test_handledByPickleModule(self):
        """
        Handling L{pickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        self.assertRaises(pickle.PicklingError,
                          self.raise_UniversalPicklingError) 
Example 71
Project: Safejumper-for-Desktop   Author: proxysh   File: test_styles.py    GNU General Public License v2.0 5 votes vote down vote up
def test_handledBycPickleModule(self):
        """
        Handling L{cPickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        try:
            import cPickle
        except ImportError:
            raise unittest.SkipTest("cPickle not available.")
        else:
            self.assertRaises(cPickle.PicklingError,
                              self.raise_UniversalPicklingError) 
Example 72
Project: Safejumper-for-Desktop   Author: proxysh   File: test_styles.py    GNU General Public License v2.0 5 votes vote down vote up
def test_lambdaRaisesPicklingError(self):
        """
        Pickling a C{lambda} function ought to raise a L{pickle.PicklingError}.
        """
        self.assertRaises(pickle.PicklingError, pickle.dumps, lambdaExample)
        try:
            import cPickle
        except:
            pass
        else:
            self.assertRaises(cPickle.PicklingError, cPickle.dumps,
                              lambdaExample) 
Example 73
Project: pick   Author: bernardorufino   File: test_enum.py    MIT License 4 votes vote down vote up
def test_subclasses_without_direct_pickle_support(self):
        class NamedInt(int):
            __qualname__ = 'NamedInt'
            def __new__(cls, *args):
                _args = args
                name, args = args[0], args[1:]
                if len(args) == 0:
                    raise TypeError("name and value must be specified")
                self = int.__new__(cls, *args)
                self._intname = name
                self._args = _args
                return self
            @property
            def __name__(self):
                return self._intname
            def __repr__(self):
                # repr() is updated to include the name and type info
                return "%s(%r, %s)" % (type(self).__name__,
                                             self.__name__,
                                             int.__repr__(self))
            def __str__(self):
                # str() is unchanged, even if it relies on the repr() fallback
                base = int
                base_str = base.__str__
                if base_str.__objclass__ is object:
                    return base.__repr__(self)
                return base_str(self)
            # for simplicity, we only define one operator that
            # propagates expressions
            def __add__(self, other):
                temp = int(self) + int( other)
                if isinstance(self, NamedInt) and isinstance(other, NamedInt):
                    return NamedInt(
                        '(%s + %s)' % (self.__name__, other.__name__),
                        temp )
                else:
                    return temp

        class NEI(NamedInt, Enum):
            __qualname__ = 'NEI'
            x = ('the-x', 1)
            y = ('the-y', 2)

        self.assertTrue(NEI.__new__ is Enum.__new__)
        self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
        globals()['NamedInt'] = NamedInt
        globals()['NEI'] = NEI
        NI5 = NamedInt('test', 5)
        self.assertEqual(NI5, 5)
        self.assertEqual(NEI.y.value, 2)
        test_pickle_exception(self.assertRaises, TypeError, NEI.x)
        test_pickle_exception(self.assertRaises, PicklingError, NEI) 
Example 74
Project: NiujiaoDebugger   Author: MrSrc   File: test_enum.py    GNU General Public License v3.0 4 votes vote down vote up
def test_subclasses_without_direct_pickle_support(self):
        class NamedInt(int):
            __qualname__ = 'NamedInt'
            def __new__(cls, *args):
                _args = args
                name, *args = args
                if len(args) == 0:
                    raise TypeError("name and value must be specified")
                self = int.__new__(cls, *args)
                self._intname = name
                self._args = _args
                return self
            @property
            def __name__(self):
                return self._intname
            def __repr__(self):
                # repr() is updated to include the name and type info
                return "{}({!r}, {})".format(type(self).__name__,
                                             self.__name__,
                                             int.__repr__(self))
            def __str__(self):
                # str() is unchanged, even if it relies on the repr() fallback
                base = int
                base_str = base.__str__
                if base_str.__objclass__ is object:
                    return base.__repr__(self)
                return base_str(self)
            # for simplicity, we only define one operator that
            # propagates expressions
            def __add__(self, other):
                temp = int(self) + int( other)
                if isinstance(self, NamedInt) and isinstance(other, NamedInt):
                    return NamedInt(
                        '({0} + {1})'.format(self.__name__, other.__name__),
                        temp )
                else:
                    return temp

        class NEI(NamedInt, Enum):
            __qualname__ = 'NEI'
            x = ('the-x', 1)
            y = ('the-y', 2)

        self.assertIs(NEI.__new__, Enum.__new__)
        self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
        globals()['NamedInt'] = NamedInt
        globals()['NEI'] = NEI
        NI5 = NamedInt('test', 5)
        self.assertEqual(NI5, 5)
        self.assertEqual(NEI.y.value, 2)
        test_pickle_exception(self.assertRaises, TypeError, NEI.x)
        test_pickle_exception(self.assertRaises, PicklingError, NEI) 
Example 75
Project: async-ipython-magic   Author: leriomaggio   File: run_server.py    MIT License 4 votes vote down vote up
def execute_cell(raw_cell, current_ns):
    """
    Perform the execution of the async cell
    """
    # Create a new InteractiveShell
    shell = InteractiveShell()
    # Disable Debugger
    shell.call_pdb = False
    shell.pdb = False

    # Process and Inject in the Namespace imported modules
    module_names = current_ns.pop('import_modules')
    modules = {}
    if module_names:
        for alias, mname in module_names:
            module = import_module(mname)
            modules[alias] = module
    shell.user_ns.update(current_ns)
    if modules:
        shell.user_ns.update(modules)

    output = ''
    with capture_output() as io:
        _ = shell.run_cell(raw_cell,silent=True,
                           shell_futures=False)

    # Update Namespace
    updated_namespace = dict()
    updated_namespace.setdefault('import_modules', list())
    for k, v in shell.user_ns.items():
        try:
            if inspect_ismodule(v):
                updated_namespace['import_modules'].append((k, v.__name__))
            else:
                _ = pickle.dumps({k:v})
                updated_namespace[k] = v
        except TypeError:
            continue
        except pickle.PicklingError:
            continue
        except AttributeError:
            continue

    # if not output:
    output += io.stdout
    return output, updated_namespace 
Example 76
Project: tmtoolkit   Author: WZBSocialScienceCenter   File: _tmpreproc.py    Apache License 2.0 4 votes vote down vote up
def transform_tokens(self, transform_fn):
        """
        Transform tokens in all documents by applying `transform_fn` to each document's tokens individually.

        If `transform_fn` is "pickable" (e.g. ``pickle.dumps(transform_fn)`` doesn't raise an exception), the
        function is applied in parallel. If not, the function is applied to the documents sequentially, which may
        be very slow.

        :param transform_fn: a function to apply to all documents' tokens; it must accept a single token string and
                             vice-versa return single token string
        :return: this instance
        """
        if not callable(transform_fn):
            raise ValueError('`transform_fn` must be callable')

        process_on_workers = True
        tokens = None

        try:
            pickle.dumps(transform_fn)
        except (pickle.PicklingError, AttributeError):
            process_on_workers = False
            tokens = self._workers_tokens

        self._invalidate_workers_tokens()

        logger.info('transforming tokens')

        if process_on_workers:
            self._send_task_to_workers('transform_tokens', transform_fn=transform_fn)
        else:
            logger.debug('transforming tokens on main thread')

            new_tokens = defaultdict(dict)
            for dl, doc in tokens.items():
                new_tokens[self.docs2workers[dl]][dl] = list(map(transform_fn, doc['token']))

            for worker_id, worker_tokens in new_tokens.items():
                self.tasks_queues[worker_id].put(('replace_tokens', {'tokens': worker_tokens}))

            [q.join() for q in self.tasks_queues]

        return self 
Example 77
Project: tweet-the-wolf   Author: theshteves   File: test_enum.py    MIT License 4 votes vote down vote up
def test_subclasses_without_direct_pickle_support(self):
        class NamedInt(int):
            __qualname__ = 'NamedInt'
            def __new__(cls, *args):
                _args = args
                name, args = args[0], args[1:]
                if len(args) == 0:
                    raise TypeError("name and value must be specified")
                self = int.__new__(cls, *args)
                self._intname = name
                self._args = _args
                return self
            @property
            def __name__(self):
                return self._intname
            def __repr__(self):
                # repr() is updated to include the name and type info
                return "%s(%r, %s)" % (type(self).__name__,
                                             self.__name__,
                                             int.__repr__(self))
            def __str__(self):
                # str() is unchanged, even if it relies on the repr() fallback
                base = int
                base_str = base.__str__
                if base_str.__objclass__ is object:
                    return base.__repr__(self)
                return base_str(self)
            # for simplicity, we only define one operator that
            # propagates expressions
            def __add__(self, other):
                temp = int(self) + int( other)
                if isinstance(self, NamedInt) and isinstance(other, NamedInt):
                    return NamedInt(
                        '(%s + %s)' % (self.__name__, other.__name__),
                        temp )
                else:
                    return temp

        class NEI(NamedInt, Enum):
            __qualname__ = 'NEI'
            x = ('the-x', 1)
            y = ('the-y', 2)

        self.assertTrue(NEI.__new__ is Enum.__new__)
        self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
        globals()['NamedInt'] = NamedInt
        globals()['NEI'] = NEI
        NI5 = NamedInt('test', 5)
        self.assertEqual(NI5, 5)
        self.assertEqual(NEI.y.value, 2)
        test_pickle_exception(self.assertRaises, TypeError, NEI.x)
        test_pickle_exception(self.assertRaises, PicklingError, NEI) 
Example 78
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_enum.py    GNU General Public License v2.0 4 votes vote down vote up
def test_subclasses_without_direct_pickle_support(self):
        class NamedInt(int):
            __qualname__ = 'NamedInt'
            def __new__(cls, *args):
                _args = args
                name, *args = args
                if len(args) == 0:
                    raise TypeError("name and value must be specified")
                self = int.__new__(cls, *args)
                self._intname = name
                self._args = _args
                return self
            @property
            def __name__(self):
                return self._intname
            def __repr__(self):
                # repr() is updated to include the name and type info
                return "{}({!r}, {})".format(type(self).__name__,
                                             self.__name__,
                                             int.__repr__(self))
            def __str__(self):
                # str() is unchanged, even if it relies on the repr() fallback
                base = int
                base_str = base.__str__
                if base_str.__objclass__ is object:
                    return base.__repr__(self)
                return base_str(self)
            # for simplicity, we only define one operator that
            # propagates expressions
            def __add__(self, other):
                temp = int(self) + int( other)
                if isinstance(self, NamedInt) and isinstance(other, NamedInt):
                    return NamedInt(
                        '({0} + {1})'.format(self.__name__, other.__name__),
                        temp )
                else:
                    return temp

        class NEI(NamedInt, Enum):
            __qualname__ = 'NEI'
            x = ('the-x', 1)
            y = ('the-y', 2)

        self.assertIs(NEI.__new__, Enum.__new__)
        self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
        globals()['NamedInt'] = NamedInt
        globals()['NEI'] = NEI
        NI5 = NamedInt('test', 5)
        self.assertEqual(NI5, 5)
        self.assertEqual(NEI.y.value, 2)
        test_pickle_exception(self.assertRaises, TypeError, NEI.x)
        test_pickle_exception(self.assertRaises, PicklingError, NEI) 
Example 79
Project: Computable   Author: ktraunmueller   File: session.py    MIT License 4 votes vote down vote up
def save(variables=None, file=SAVEFILE, dictionary=None, verbose=False):

    """saves variables from a numpy session to a file.  Variables
    which won't pickle are "proxied" if possible.

    'variables'       a string of comma seperated variables: e.g. "a,b,c"
                      Defaults to dictionary.keys().

    'file'            a filename or file object for the session file.

    'dictionary'      the dictionary in which to look up the variables.
                      Defaults to the caller's globals()

    'verbose'         print additional debug output when True.
    """

    global VERBOSE
    VERBOSE = verbose

    _update_proxy_types()

    if isinstance(file, str):
        file = open(file, "wb")

    if dictionary is None:
        dictionary = _callers_globals()

    if variables is None:
        keys = list(dictionary.keys())
    else:
        keys = variables.split(",")

    source_modules = _callers_modules() + list(sys.modules.keys())

    p = pickle.Pickler(file, protocol=2)

    _verbose("variables:", keys)
    for k in keys:
        v = dictionary[k]
        _verbose("saving", k, type(v))
        try:  # Try to write an ordinary pickle
            p.dump(v)
            _verbose("pickled", k)
        except (pickle.PicklingError, TypeError, SystemError):
            # Use proxies for stuff that won't pickle
            if isinstance(v, type(sys)): # module
                proxy = _ModuleProxy(v.__name__, save=True)
            else:
                try:
                    module, name = _locate(source_modules, v)
                except ObjectNotFound:
                    _errout("warning: couldn't find object", k,
                            "in any module... skipping.")
                    continue
                else:
                    proxy = _ObjectProxy(module, name, type(v), save=True)
            p.dump(proxy)
    o = _SaveSession(keys, save=True)
    p.dump(o)
    file.close() 
Example 80
Project: oss-ftp   Author: aliyun   File: test_enum.py    MIT License 4 votes vote down vote up
def test_subclasses_without_direct_pickle_support(self):
        class NamedInt(int):
            __qualname__ = 'NamedInt'
            def __new__(cls, *args):
                _args = args
                name, args = args[0], args[1:]
                if len(args) == 0:
                    raise TypeError("name and value must be specified")
                self = int.__new__(cls, *args)
                self._intname = name
                self._args = _args
                return self
            @property
            def __name__(self):
                return self._intname
            def __repr__(self):
                # repr() is updated to include the name and type info
                return "%s(%r, %s)" % (type(self).__name__,
                                             self.__name__,
                                             int.__repr__(self))
            def __str__(self):
                # str() is unchanged, even if it relies on the repr() fallback
                base = int
                base_str = base.__str__
                if base_str.__objclass__ is object:
                    return base.__repr__(self)
                return base_str(self)
            # for simplicity, we only define one operator that
            # propagates expressions
            def __add__(self, other):
                temp = int(self) + int( other)
                if isinstance(self, NamedInt) and isinstance(other, NamedInt):
                    return NamedInt(
                        '(%s + %s)' % (self.__name__, other.__name__),
                        temp )
                else:
                    return temp

        class NEI(NamedInt, Enum):
            __qualname__ = 'NEI'
            x = ('the-x', 1)
            y = ('the-y', 2)

        self.assertTrue(NEI.__new__ is Enum.__new__)
        self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
        globals()['NamedInt'] = NamedInt
        globals()['NEI'] = NEI
        NI5 = NamedInt('test', 5)
        self.assertEqual(NI5, 5)
        self.assertEqual(NEI.y.value, 2)
        test_pickle_exception(self.assertRaises, TypeError, NEI.x)
        test_pickle_exception(self.assertRaises, PicklingError, NEI)