is compatibility builtins broken?

閲覧: 49 回
最初の未読メッセージにスキップ

Chris Smith

未読、
2019/05/08 3:23:452019/05/08
To: sympy
Am I doing something wrong here when running Python 2?

>>> from sympy.core.compatibility import builtins
>>> from builtins import type
Traceback (most recent call last):
 
File "<stdin>", line 1, in <module>
ImportError: No module named builtins


/c

SHUBHAM JHA

未読、
2019/05/08 4:13:122019/05/08
To: sympy
Hi Chris,

I am using version 1.5-dev
In [2]: from sympy.core.compatibility import builtins
In [3]: from builtins import type
In [4]:

Also,
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/10966434-39a0-4da5-b239-0782175abeb7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oscar Benjamin

未読、
2019/05/08 8:34:272019/05/08
To: sympy
There are two possible wrong things:

1) On Python 2 the module is called __builtin__:

>>> import __builtin__
>>> __builtin__.map
<built-in function map>

2) You've imported the name builtins so that it is available as a name
in the current global namespace. That does not mean that a subsequent
import statement will import from the resulting module object. An
import statement never looks at the names in the current namespace to
find anything. You can however do

import sympy.core.compatbility.builtins as builtins
builtins.type

Or

from sympy.core.compatibility.builtins import type

Not sure what you're aiming for but there is a hacky way to do it:

sys.modules['builtins'] = builtins

--
Oscar

Chris Smith

未読、
2019/05/08 17:38:302019/05/08
To: sympy
None of them work

>>> import sympy.core.compatibility.builtins as builtins
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named builtins

>>> from sympy.core.compatibility.builtins import type
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named builtins

>>> sys.modules['builtins']=__builtin__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__builtin__' is not defined

but

>>> from __builtin__ import type
>>> 'builtins' in dir()
True
>>> builtins
<module '__builtin__' (built-in)>
>>> from builtins import type
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named builtins

Vishesh Mangla

未読、
2019/05/08 17:42:582019/05/08
To: sy...@googlegroups.com

What do these builtins ought to be doing?

 

Sent from Mail for Windows 10

--

You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Isuru Fernando

未読、
2019/05/08 17:47:282019/05/08
To: sy...@googlegroups.com
You can do,

>>> from sympy.core.compatibility import builtins
>>> type = builtins.type

Oscar Benjamin

未読、
2019/05/08 17:48:412019/05/08
To: sympy
On Wed, 8 May 2019 at 22:38, Chris Smith <smi...@gmail.com> wrote:
>
> None of them work

That's what I get for not trying them out!

> >>> import sympy.core.compatibility.builtins as builtins
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ImportError: No module named builtins
>
> >>> from sympy.core.compatibility.builtins import type
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ImportError: No module named builtins

Yes that's because there is no builtins module in the compatibility
package. In fact compatibility is a module not a package.

> >>> sys.modules['builtins']=__builtin__
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name '__builtin__' is not defined

This will work if you have imported __builtin__ first:

>>> import __builtin__
>>> import sys
>>> sys.modules['builtins'] = __builtin__
>>> import builtins

--
Oscar

Aaron Meurer

未読、
2019/05/08 18:02:052019/05/08
To: sympy
What is this for? We shouldn't be monkey-patching sys.modules in library code.

Aaron Meurer

>
> --
> Oscar
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQ59yzRzns5MHiqNmZz09Tx82B4aF_CnvidTwMGPA9E6Q%40mail.gmail.com.

Chris Smith

未読、
2019/05/09 4:02:552019/05/09
To: sympy
builtins is in core.compatibility and it doesn't seem to work for me.

The issue will go away when we drop Py 2.7...or sooner if someone can figure out what's wrong.
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.

Aaron Meurer

未読、
2019/05/09 15:36:212019/05/09
To: sympy
This works for me:

Python 2.7.14 | packaged by conda-forge | (default, Dec 9 2017, 16:31:12)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy.core.compatibility import builtins
>>> builtins.type
<type 'type'>

Aaron Meurer
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/bb44f6d2-8d32-410b-97e7-5bd4e2409a2a%40googlegroups.com.

Chris Smith

未読、
2019/05/09 16:56:342019/05/09
To: sympy
Hmm - yes, that works for me, too. But in Py3 you can do 'from builtins import type' -- is the only way to do that in 2 is to use monkey patching?

/c

Aaron Meurer

未読、
2019/05/09 18:19:362019/05/09
To: sympy
In Python 2 builtins was called called __builtin__. That's why it's in
the compatibility module (not to be confused with __builtins__, which
is a different thing, hence the rename). You could just as well write

try:
# Python 3
from builtins import type
except ImportError:
# Python 2
from __builtin__ import type

but the whole point of the SymPy compatibility module is to isolate
such 2/3 compatibility code into one place. So it's better to do

from sympy.core.compatibility import builtins
builtins.type

There's also another possible level of confusion here which is that if
you have the 'future' package installed in your Python 2 environment,
it automatically adds a builtins module to the standard library. So in
that case, 'from builtins import type' will work. But it shouldn't be
relied on for SymPy, because we do not have 'future' as a dependency
(and do not want to have it as one).

Aaron Meurer
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/13957bcc-ec40-4f21-a2c0-05576db5db53%40googlegroups.com.

Chris Smith

未読、
2019/05/10 1:35:412019/05/10
To: sympy
Thanks for clarifying. I generally think of compatibility making it so whatever you do in Py3 you can do in Py2. In this case the common idiom is to reference a builtin from its module (builtins.foo) rather than to try import foo from builtins (which works with Py3 but not with Py2 *with the same name*).

/c
全員に返信
投稿者に返信
転送
新着メール 0 件