How to use __setstate__ method in green

Best Python code snippet using green

test_functools.py

Source:test_functools.py Github

copy

Full Screen

...159 self.assertIsNot(f_copy.keywords, f.keywords)160 self.assertIsNot(f_copy.keywords['bar'], f.keywords['bar'])161 def test_setstate(self):162 f = self.partial(signature)163 f.__setstate__((capture, (1,), dict(a=10), dict(attr=[])))164 self.assertEqual(signature(f),165 (capture, (1,), dict(a=10), dict(attr=[])))166 self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20}))167 f.__setstate__((capture, (1,), dict(a=10), None))168 self.assertEqual(signature(f), (capture, (1,), dict(a=10), {}))169 self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20}))170 f.__setstate__((capture, (1,), None, None))171 #self.assertEqual(signature(f), (capture, (1,), {}, {}))172 self.assertEqual(f(2, b=20), ((1, 2), {'b': 20}))173 self.assertEqual(f(2), ((1, 2), {}))174 self.assertEqual(f(), ((1,), {}))175 f.__setstate__((capture, (), {}, None))176 self.assertEqual(signature(f), (capture, (), {}, {}))177 self.assertEqual(f(2, b=20), ((2,), {'b': 20}))178 self.assertEqual(f(2), ((2,), {}))179 self.assertEqual(f(), ((), {}))180 def test_setstate_errors(self):181 f = self.partial(signature)182 self.assertRaises(TypeError, f.__setstate__, (capture, (), {}))183 self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, {}, None))184 self.assertRaises(TypeError, f.__setstate__, [capture, (), {}, None])185 self.assertRaises(TypeError, f.__setstate__, (None, (), {}, None))186 self.assertRaises(TypeError, f.__setstate__, (capture, None, {}, None))187 self.assertRaises(TypeError, f.__setstate__, (capture, [], {}, None))188 self.assertRaises(TypeError, f.__setstate__, (capture, (), [], None))189 def test_setstate_subclasses(self):190 f = self.partial(signature)191 f.__setstate__((capture, MyTuple((1,)), MyDict(a=10), None))192 s = signature(f)193 self.assertEqual(s, (capture, (1,), dict(a=10), {}))194 self.assertIs(type(s[1]), tuple)195 self.assertIs(type(s[2]), dict)196 r = f()197 self.assertEqual(r, ((1,), {'a': 10}))198 self.assertIs(type(r[0]), tuple)199 self.assertIs(type(r[1]), dict)200 f.__setstate__((capture, BadTuple((1,)), {}, None))201 s = signature(f)202 self.assertEqual(s, (capture, (1,), {}, {}))203 self.assertIs(type(s[1]), tuple)204 r = f(2)205 self.assertEqual(r, ((1, 2), {}))206 self.assertIs(type(r[0]), tuple)207 def test_recursive_pickle(self):208 f = self.partial(capture)209 f.__setstate__((f, (), {}, {}))210 try:211 for proto in range(pickle.HIGHEST_PROTOCOL + 1):212 with self.assertRaises(RuntimeError):213 pickle.dumps(f, proto)214 finally:215 f.__setstate__((capture, (), {}, {}))216 f = self.partial(capture)217 f.__setstate__((capture, (f,), {}, {}))218 try:219 for proto in range(pickle.HIGHEST_PROTOCOL + 1):220 f_copy = pickle.loads(pickle.dumps(f, proto))221 try:222 self.assertIs(f_copy.args[0], f_copy)223 finally:224 f_copy.__setstate__((capture, (), {}, {}))225 finally:226 f.__setstate__((capture, (), {}, {}))227 f = self.partial(capture)228 f.__setstate__((capture, (), {'a': f}, {}))229 try:230 for proto in range(pickle.HIGHEST_PROTOCOL + 1):231 f_copy = pickle.loads(pickle.dumps(f, proto))232 try:233 self.assertIs(f_copy.keywords['a'], f_copy)234 finally:235 f_copy.__setstate__((capture, (), {}, {}))236 finally:237 f.__setstate__((capture, (), {}, {}))238 # Issue 6083: Reference counting bug239 def test_setstate_refcount(self):240 class BadSequence:241 def __len__(self):242 return 4243 def __getitem__(self, key):244 if key == 0:245 return max246 elif key == 1:247 return tuple(range(1000000))248 elif key in (2, 3):249 return {}250 raise IndexError251 f = self.partial(object)...

Full Screen

Full Screen

OtherXslElement.py

Source:OtherXslElement.py Github

copy

Full Screen

...24 self.baseUri)25 def __getstate__(self):26 base_state = XsltElement.__getstate__(self)27 return base_state28 def __setstate__(self, state):29 XsltElement.__setstate__(self, state)30 return31class IncludeElement(XsltElement):32 legalAttrs = ('href',)33 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='include', prefix='xsl', baseUri=''):34 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)35 def setup(self):36 self.__dict__['_href'] = self.getAttributeNS(EMPTY_NAMESPACE, 'href')37 return38 def __getinitargs__(self):39 return (None, self.namespaceURI, self.localName, self.prefix,40 self.baseUri)41 def __getstate__(self):42 base_state = XsltElement.__getstate__(self)43 new_state = (base_state, self._href, )44 return new_state45 def __setstate__(self, state):46 XsltElement.__setstate__(self, state[0])47 self._href = state[1]48 return49class FallbackElement(XsltElement):50 legalAttrs = ()51 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='fallback', prefix='xsl', baseUri=''):52 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)53 self.__dict__['_nss'] = xml.dom.ext.GetAllNs(self)54 return55 def setup(self):56 return57 def instantiate(self, context, processor):58 origState = context.copy()59 context.setNamespaces(self._nss)60 for child in self.childNodes:61 context = child.instantiate(context, processor)[0]62 context.set(origState)63 return (context,)64 def __getinitargs__(self):65 return (None, self.namespaceURI, self.localName, self.prefix,66 self.baseUri)67 def __getstate__(self):68 base_state = XsltElement.__getstate__(self)69 return base_state70 def __setstate__(self, state):71 XsltElement.__setstate__(self, state)72 return73class ImportElement(XsltElement):74 legalAttrs = ('href',)75 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='import', prefix='xsl', baseUri=''):76 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)77 self.stylesheet = None78 def setup(self):79 self.href = self.getAttributeNS(EMPTY_NAMESPACE, 'href')80 def __getinitargs__(self):81 return (None, self.namespaceURI, self.localName, self.prefix,82 self.baseUri)83 def __getstate__(self):84 base_state = XsltElement.__getstate__(self)85 return base_state86 def __setstate__(self, state):87 XsltElement.__setstate__(self, state)88 return89class KeyElement(XsltElement):90 legalAttrs = ('name', 'match', 'use')91 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='key', prefix='xsl', baseUri=''):92 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)93 def setup(self):94 pass95 def __getinitargs__(self):96 return (None, self.namespaceURI, self.localName, self.prefix,97 self.baseUri)98 def __getstate__(self):99 base_state = XsltElement.__getstate__(self)100 return base_state101 def __setstate__(self, state):102 XsltElement.__setstate__(self, state)103 return104class NamespaceAliasElement(XsltElement):105 legalAttrs = ('stylesheet-prefix', 'result-prefix')106 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='namespace-alias', prefix='xsl', baseUri=''):107 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)108 def setup(self):109 pass110 def __getinitargs__(self):111 return (None, self.namespaceURI, self.localName, self.prefix,112 self.baseUri)113 def __getstate__(self):114 base_state = XsltElement.__getstate__(self)115 return base_state116 def __setstate__(self, state):117 XsltElement.__setstate__(self, state)118 return119class OutputElement(XsltElement):120 legalAttrs = ('method', 'version', 'encoding', 'omit-xml-declaration', 'standalone', 'doctype-public', 'doctype-system', 'cdata-section-elements', 'indent', 'media-type')121 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='output', prefix='xsl', baseUri=''):122 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)123 def setup(self):124 pass125 def __getinitargs__(self):126 return (None, self.namespaceURI, self.localName, self.prefix,127 self.baseUri)128 def __getstate__(self):129 base_state = XsltElement.__getstate__(self)130 return base_state131 def __setstate__(self, state):132 XsltElement.__setstate__(self, state)133 return134class PreserveSpaceElement(XsltElement):135 legalAttrs = ('elements',)136 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='preserve-space', prefix='xsl', baseUri=''):137 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)138 def setup(self):139 pass140 def __getinitargs__(self):141 return (None, self.namespaceURI, self.localName, self.prefix,142 self.baseUri)143 def __getstate__(self):144 base_state = XsltElement.__getstate__(self)145 return base_state146 def __setstate__(self, state):147 XsltElement.__setstate__(self, state)148 return149 150class StripSpaceElement(XsltElement):151 legalAttrs = ('elements',)152 def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE, localName='strip-space', prefix='xsl', baseUri=''):153 XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)154 def setup(self):155 pass156 def __getinitargs__(self):157 return (None, self.namespaceURI, self.localName, self.prefix,158 self.baseUri)159 def __getstate__(self):160 base_state = XsltElement.__getstate__(self)161 return base_state162 def __setstate__(self, state):163 XsltElement.__setstate__(self, state)164 return165 166import urlparse167from xml.xslt import XsltException, Error...

Full Screen

Full Screen

versioned_unpickler.py

Source:versioned_unpickler.py Github

copy

Full Screen

1from pickle import *2#import sys3import new4from enthought.logging import logger56#from enthought.persistence.updater import __replacement_setstate__7 89class VersionedUnpickler(Unpickler):10 """ This class reads in a pickled file created at revision version 'n'11 and then applies the transforms specified in the updater class to 12 generate a new set of objects which are at revision version 'n+1'.13 14 I decided to keep the loading of the updater out of this generic class15 because we will want updaters to be generated for each plugin's type16 of project. eg ProAVA2 projects will need different updaters to the 17 ProAct project. 18 19 This ensures that the VersionedUnpickler can remain ignorant about the20 actual version numbers - all it needs to do is upgrade one release. 21 """2223 24 def __init__(self, file, updater=None):25 Unpickler.__init__(self, file)26 self.updater = updater27 return28 29 30 def find_class(self, module, name):31 """ Overridden method from Unpickler. 32 33 NB __setstate__ is not called until later.34 """3536 if self.updater:37 # check to see if this class needs to be mapped to a new class38 # or module name 39 original_module, original_name = module, name40 #logger.debug('omodule:%s oname:%s' % (original_module, original_name))41 module, name = self.updater.get_latest(module, name)42 #logger.debug('module:%s name:%s' % (module, name))43 44 # load the class...45 '''__import__(module)46 mod = sys.modules[module]47 klass = getattr(mod, name)'''48 klass = self.import_name(module, name)49 50 # add the updater.... TODO - why the old name? 51 self.add_updater(original_module, original_name, klass)52 53 else:54 # there is no updater so we will be reading in an up to date 55 # version of the file...56 try:57 klass = Unpickler.find_class(self, module, name)58 except:59 logger.error("Looking for [%s] [%s]" % (module, name))60 logger.exception('Problem using default unpickle functionality')61 62 # restore the original __setstate__ if necessary63 fn = getattr(klass, '__setstate_original__', False)64 if fn:65 m = new.instancemethod(fn, None, klass)66 setattr(klass, '__setstate__', m)67 68 return klass 69 7071 def add_updater(self, module, name, klass):72 """ If there is an updater defined for this class we will add it to the73 class as the __setstate__ method.74 """ 75 76 fn = self.updater.setstates.get((module, name), False) 77 78 if fn:79 # move the existing __setstate__ out of the way80 self.backup_setstate(module, klass)81 82 # add the updater into the class 83 m = new.instancemethod(fn, None, klass)84 setattr(klass, '__updater__', m)85 86 # hook up our __setstate__ which updates self.__dict__87 m = new.instancemethod(__replacement_setstate__, None, klass)88 setattr(klass, '__setstate__', m)8990 else:91 pass92 #print 'No updater fn to worry about'93 94 return95 9697 def backup_setstate(self, module, klass):98 """ If the class has a user defined __setstate__ we back it up.99 """100 if getattr(klass, '__setstate__', False):101 102 if getattr(klass, '__setstate_original__', False):103 # don't overwrite the original __setstate__104 name = '__setstate__%s' % self.updater.__class__105 else:106 # backup the original __setstate__ which we will restore107 # and run later when we have finished updating the class 108 name = '__setstate_original__'109 110 #logger.debug('renaming __setstate__ to %s' % name) 111 method = getattr(klass, '__setstate__')112 m = new.instancemethod(method, None, klass)113 setattr(klass, name, m)114 115 else:116 # the class has no __setstate__ method so do nothing117 pass118 119 return120121 122 def import_name(self, module, name):123 """ 124 If the class is needed for the latest version of the application then125 it should presumably exist. 126 127 If the class no longer exists then we should perhaps return 128 a proxy of the class. 129 130 If the persisted file is at v1 say and the application is at v3 then 131 objects that are required for v1 and v2 do not have to exist they only 132 need to be placeholders for the state during an upgrade. 133 """134 #print "importing %s %s" % (name, module)135 module = __import__(module, globals(), locals(), [name])136 return vars(module)[name]137 138### EOF #################################################################139 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run green automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful