Giter Club home page Giter Club logo

Comments (7)

TD22057 avatar TD22057 commented on July 1, 2024

FYI - possible fix for you to evaluate is the following. It appears to fix the issue on my end and I believe it passes tests. But I don't understand the rational for why it was done this way in the first place so it would be good for you to look at. If you'd rather have this as a PR, let me know and I'll do that.

def _import_module(import_name, safe=False):
    try:
        # attempt to fix git bug #634.
        m = sys.modules.get(import_name)
        if m:
           return m
        #if import_name.startswith('__runtime__.'):
        #    return sys.modules[import_name]
        elif '.' in import_name:
            items = import_name.split('.')
            module = '.'.join(items[:-1])
            obj = items[-1]
        else:
            return __import__(import_name)
        return getattr(__import__(module, None, None, [obj]), obj)
    except (ImportError, AttributeError, KeyError):
        if safe:
            return None
        raise

from dill.

mmckerns avatar mmckerns commented on July 1, 2024

Thanks for reporting, and the follow-up on this. I'll have a look... and yes, I'll make a PR if you don't.

from dill.

mmckerns avatar mmckerns commented on July 1, 2024

Slight modification:

foo/__init__.py
    from .bar import bar, zap

foo/bar.py:
   class bar:
      pass

   class zap:
      pass

With the above, I don't think there's a difference for foo.bar.zap versus foo.bar.bar. So, it would seem the module/class having the same name doesn't matter. Also, I think this is the expected behavior -- dill makes a copy of the class instead of referencing it like pickle would. The warning is a bit annoying, but innocuous.

Can you tell me what you think the behavior should be?

from dill.

TD22057 avatar TD22057 commented on July 1, 2024

I'm not at a computer where I can check this. But I assume "with the above" means my hack/fix. That could be true - I didn't test every combination. The fix I proposed may not be correct and/or complete.

However that doesn't mean this isn't a bug and IMO it's a serious bug. The problem is that I have code that unpickles something and then says:

obj = dill.loads( data )
if isinstance( obj, foo.bar ):
   ...  do something

And the isinstance check will fail because dill made a clone of the class. So the unpickled object is no longer the same type as the object that was pickled. BTW regular pickle handles this case just fine - I don't know what it's doing but it correctly finds the package and unpickles to the original type.

from dill.

mmckerns avatar mmckerns commented on July 1, 2024

Currently, the expected behavior is that dill makes a copy of the class, and doesn't pickle a reference unless it's requested that the class is pickled by reference. I initially thought that you were reporting that the bug was the warning being thrown, but if it's that dill is not pickling by reference... then this is the expected behavior.

This enables dill to deal with all sorts of different cases, such as the class being dynamically modified.

from dill.

TD22057 avatar TD22057 commented on July 1, 2024

Sorry - I didn't realize that was the desired behavior. I doubt it matters at this point but FYI in every use case we have (saving data, sending functions and data to parallel engines) it's really bad to make a copy of the class unless there is no other way to pickle it. I guess we'll try to figure out how to suppress that error and wrap the library to set byRef=True for all of our calls.

from dill.

mmckerns avatar mmckerns commented on July 1, 2024

No worries. Also, those are the most common dill use cases, I believe. If you want classes to be pickled by reference in all cases, you can adjust the global settings. dill.settings['byref'] = True. -- then you won't need to use the keyword in the dump.

from dill.

Related Issues (20)

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.