Python pickle.POP Examples

The following are 7 code examples for showing how to use pickle.POP(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pickle , or try the search function .

Example 1
Project: FATE   Author: FederatedAI   File: cloudpickle.py    License: Apache License 2.0 6 votes vote down vote up
def _save_subimports(self, code, top_level_dependencies):
        """
        Ensure de-pickler imports any package child-modules that
        are needed by the function
        """
        # check if any known dependency is an imported package
        for x in top_level_dependencies:
            if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
                # check if the package has any currently loaded sub-imports
                prefix = x.__name__ + '.'
                for name, module in sys.modules.items():
                    # Older versions of pytest will add a "None" module to sys.modules.
                    if name is not None and name.startswith(prefix):
                        # check whether the function can address the sub-module
                        tokens = set(name[len(prefix):].split('.'))
                        if not tokens - set(code.co_names):
                            # ensure unpickler executes this import
                            self.save(module)
                            # then discards the reference to it
                            self.write(pickle.POP) 
Example 2
Project: FATE   Author: FederatedAI   File: cloudpickle.py    License: Apache License 2.0 6 votes vote down vote up
def _save_subimports(self, code, top_level_dependencies):
        """
        Ensure de-pickler imports any package child-modules that
        are needed by the function
        """
        # check if any known dependency is an imported package
        for x in top_level_dependencies:
            if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
                # check if the package has any currently loaded sub-imports
                prefix = x.__name__ + '.'
                for name, module in sys.modules.items():
                    # Older versions of pytest will add a "None" module to sys.modules.
                    if name is not None and name.startswith(prefix):
                        # check whether the function can address the sub-module
                        tokens = set(name[len(prefix):].split('.'))
                        if not tokens - set(code.co_names):
                            # ensure unpickler executes this import
                            self.save(module)
                            # then discards the reference to it
                            self.write(pickle.POP) 
Example 3
Project: LearningApacheSpark   Author: runawayhorse001   File: cloudpickle.py    License: MIT License 6 votes vote down vote up
def _save_subimports(self, code, top_level_dependencies):
        """
        Ensure de-pickler imports any package child-modules that
        are needed by the function
        """
        # check if any known dependency is an imported package
        for x in top_level_dependencies:
            if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
                # check if the package has any currently loaded sub-imports
                prefix = x.__name__ + '.'
                for name, module in sys.modules.items():
                    # Older versions of pytest will add a "None" module to sys.modules.
                    if name is not None and name.startswith(prefix):
                        # check whether the function can address the sub-module
                        tokens = set(name[len(prefix):].split('.'))
                        if not tokens - set(code.co_names):
                            # ensure unpickler executes this import
                            self.save(module)
                            # then discards the reference to it
                            self.write(pickle.POP) 
Example 4
Project: code   Author: ActiveState   File: recipe-572213.py    License: MIT License 6 votes vote down vote up
def save_type(self, obj):
    if getattr(new, obj.__name__, None) is obj:
        # Types in 'new' module claim their module is '__builtin__' but are not actually there
        save_global_byname(self, obj, 'new', obj.__name__)
    elif obj.__module__ == '__main__':
        # Types in __main__ are saved by value

        # Make sure we have a reference to type.__new__        
        if id(type.__new__) not in self.memo:
            self.save_reduce(getattr, (type, '__new__'), obj=type.__new__)
            self.write(pickle.POP)

        # Copy dictproxy to real dict
        d = dict(obj.__dict__)
        # Clean up unpickleable descriptors added by Python
        d.pop('__dict__', None)
        d.pop('__weakref__', None)
        
        args = (type(obj), obj.__name__, obj.__bases__, d)
        self.save_reduce(type.__new__, args, obj=obj)
    else:
        # Fallback to default behavior: save by reference
        pickle.Pickler.save_global(self, obj) 
Example 5
Project: pywren   Author: pywren   File: cloudpickle.py    License: Apache License 2.0 6 votes vote down vote up
def _save_subimports(self, code, top_level_dependencies):
        """
        Ensure de-pickler imports any package child-modules that
        are needed by the function
        """
        # check if any known dependency is an imported package
        for x in top_level_dependencies:
            if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
                # check if the package has any currently loaded sub-imports
                prefix = x.__name__ + '.'
                for name, module in sys.modules.items():
                    # Older versions of pytest will add a "None" module to sys.modules.
                    if name is not None and name.startswith(prefix):
                        # check whether the function can address the sub-module
                        tokens = set(name[len(prefix):].split('.'))
                        if not tokens - set(code.co_names):
                            # ensure unpickler executes this import
                            self.save(module)
                            # then discards the reference to it
                            self.write(pickle.POP) 
Example 6
Project: eggroll   Author: WeBankFinTech   File: cloudpickle.py    License: Apache License 2.0 6 votes vote down vote up
def _save_subimports(self, code, top_level_dependencies):
    """
    Ensure de-pickler imports any package child-modules that
    are needed by the function
    """
    # check if any known dependency is an imported package
    for x in top_level_dependencies:
      if isinstance(x, types.ModuleType) and hasattr(x,
                                                     '__package__') and x.__package__:
        # check if the package has any currently loaded sub-imports
        prefix = x.__name__ + '.'
        for name, module in sys.modules.items():
          # Older versions of pytest will add a "None" module to sys.modules.
          if name is not None and name.startswith(prefix):
            # check whether the function can address the sub-module
            tokens = set(name[len(prefix):].split('.'))
            if not tokens - set(code.co_names):
              # ensure unpickler executes this import
              self.save(module)
              # then discards the reference to it
              self.write(pickle.POP) 
Example 7
Project: BentoML   Author: bentoml   File: cloudpickle.py    License: Apache License 2.0 4 votes vote down vote up
def _save_subimports(self, code, top_level_dependencies):
        """
        Save submodules used by a function but not listed in its globals.

        In the example below:

        ```
        import concurrent.futures
        import cloudpickle


        def func():
            x = concurrent.futures.ThreadPoolExecutor


        if __name__ == '__main__':
            cloudpickle.dumps(func)
        ```

        the globals extracted by cloudpickle in the function's state include
        the concurrent module, but not its submodule (here,
        concurrent.futures), which is the module used by func.

        To ensure that calling the depickled function does not raise an
        AttributeError, this function looks for any currently loaded submodule
        that the function uses and whose parent is present in the function
        globals, and saves it before saving the function.
        """

        # check if any known dependency is an imported package
        for x in top_level_dependencies:
            if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
                # check if the package has any currently loaded sub-imports
                prefix = x.__name__ + '.'
                # A concurrent thread could mutate sys.modules,
                # make sure we iterate over a copy to avoid exceptions
                for name in list(sys.modules):
                    # Older versions of pytest will add a "None" module to sys.modules.
                    if name is not None and name.startswith(prefix):
                        # check whether the function can address the sub-module
                        tokens = set(name[len(prefix):].split('.'))
                        if not tokens - set(code.co_names):
                            # ensure unpickler executes this import
                            self.save(sys.modules[name])
                            # then discards the reference to it
                            self.write(pickle.POP)