Re: tokenize.untokenize adding line continuation characters
On Tuesday, January 17, 2017 at 2:47:03 AM UTC, Steven D'Aprano wrote: > On Tuesday 17 January 2017 09:42, Rotwang wrote: > > > Here's something odd I've found with the tokenize module: > [...] > > Copypasted from iPython: > > It's not impossible that iPython is doing something funny with the tokenize > module. It happens outside iPython: Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import io, tokenize >>> tokenize.untokenize(tokenize.tokenize(io.BytesIO('if x:\n >>> y'.encode()).readline)).decode() 'if x:\ny\\\n' and also in Python 2: Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import io, tokenize >>> tokenize.untokenize(tokenize.generate_tokens(io.BytesIO('if x:\n >>> y').readline)) 'if x:\ny\\\n' > Before reporting it as a bug, I recommend that you confirm that it also > occurs in the standard Python interpreter and isn't iPython specific. Is this behaviour actually a bug, as opposed to a feature I don't understand? -- https://mail.python.org/mailman/listinfo/python-list
Re: Getting globals of the caller, not the defining module
(Sorry for posting through GG, I'm at work.) On Monday, November 11, 2013 11:25:42 AM UTC, Steven D'Aprano wrote: Suppose I have a function that needs access to globals: # module A.py def spam(): g = globals() # this gets globals from A introspect(g) As written, spam() only sees its own globals, i.e. those of the module in which spam is defined. But I want spam to see the globals of the caller. # module B import A A.spam() # I want spam to see globals from B I can have the caller explicitly pass the globals itself: def spam(globs=None): if globs is None: globs = globals() introspect(globs) But since spam is supposed to introspect as much information as possible, I don't really want to do that. What (if anything) are my other options? How about this? # module A.py import inspect def spam(): return inspect.stack()[1][0].f_globals # module B.py import A print(A.spam() is globals()) # prints True def f(): return A.spam() # module C.py import B print(B.f() is vars(B)) # prints True I don't really know what I'm doing but I guess it won't work in alternative implementations of Python. -- https://mail.python.org/mailman/listinfo/python-list