ciscoconfparse2 icon indicating copy to clipboard operation
ciscoconfparse2 copied to clipboard

[Bug]: Ciscoconfparse2 doesn't support python pickle

Open mdeng10 opened this issue 1 year ago • 4 comments

Contact Details

No response

What happened?

CiscoConfParse objects don't support python's pickle module - this is a very handy module when you're working with 1000s of configs (it takes a long time to call CiscoConfParse() on this many objects)

I've had a look at the source code - it looks ike it's trying to save CiscoConfParse.config but it wasn't set during the __init__ - it looks like you can fix this by just specifying

self.config = config

during the __init__ here

it seems like CiscoConfParse.config should be set somewhere? it's entirely unused in this module it seems, perhaps a remnant of CiscoConfParse 1.0?

CiscoConfParse Version

0.7.74

What Operating System are you using?

MacOS

What Python version(s) have this problem?

Python 3.10

Show us how to reproduce the problem. Please tell us if the problem is specific to certain inputs or situations.

import pickle
from ciscoconfparse2 import CiscoConfParse

config_obj = CiscoConfParse(["Test config"], factory=True, syntax="ios")
pickle.dump(config_obj, open("test_data.pkl", "wb"))

Python tracebacks

Traceback (most recent call last):
  File "/redacted/path/main.py", line 247, in <module>
    lambda_function({}, {})
  File "/redacted/path/main.py", line 212, in lambda_function
    pickle.dump(configs, open("configs.pkl", "wb"))
  File "/redacted/path/venv/lib/python3.10/site-packages/attr/_make.py", line 1044, in slots_getstate
    return {name: getattr(self, name) for name in state_attr_names}
  File "/redacted/path/venv/lib/python3.10/site-packages/attr/_make.py", line 1044, in <dictcomp>
    return {name: getattr(self, name) for name in state_attr_names}
AttributeError: 'CiscoConfParse' object has no attribute 'config'

Relevant log output

No response

Code of Conduct

mdeng10 avatar Aug 21 '24 02:08 mdeng10

seems like there might be more than i've detailed - when attempting to load a pickled object i hit a AttributeError: 'ConfigList' object has no attribute 'data' error which gets stuck into an infinite loop (or recursion loop) and crashes

i didn't have this problem in ciscoconfparse 1.0 - i'm unsure what specifically changed but will try to look further into it

mdeng10 avatar Aug 21 '24 02:08 mdeng10

looks like it's only an issue when i'm in debugging mode in my IDE and i have a breakout that contains a ciscoconfparse object, likewise if i print it, it also throws an exception

but if i don't have breakpoints and just let the code run, no issues - very weird, this is most likely beyond me

mdeng10 avatar Aug 21 '24 03:08 mdeng10

One problem is that attrs does not support this Optional[] typing syntax...

@attrs.define(repr=False)
class CiscoConfParse(object): 
    """Parse Cisco IOS configurations and answer queries about the configs."""
    config: Optional[Union[str,List[str]]] = None

There are a number of things that have to be fixed for CiscoConfParse() pickling / un-pickling to work... for now, consider it unsupported and it won't be fixed anytime soon. It also looks like I can't use attrs on BaseCfgLine() if I want un-pickling to work correctly.

mpenning avatar Aug 21 '24 20:08 mpenning

There are a number of things that have to be fixed for CiscoConfParse() pickling / un-pickling to work... for now, consider it unsupported

That's fair enough - feel free to close this issue or just keep it open for tracking so others don't re-raise 😄

mdeng10 avatar Aug 22 '24 00:08 mdeng10

Thank you for your patience. Please test version 0.7.77 and advise whether the issue is fixed.

mpenning avatar Jan 04 '25 15:01 mpenning

Thank you for your patience. Please test version 0.7.77 and advise whether the issue is fixed.

thanks for the update, i will test when i get a chance

mdeng10 avatar Jan 13 '25 23:01 mdeng10