Problem with StreamReaderWriter on 3.6.3?

2017-10-19 Thread Peter via Python-list
I came across this code in Google cpplint.py, a Python script for 
linting C++ code. I was getting funny results with Python 3.6.3, but it 
worked fine under 2.7.13


I've tracked the problem to an issue with StreamReaderWriter; the 
traceback and error never shows under 3. The _cause_ of the error is 
clear (xrange not in Py3), but I need the raised exception to show.


I'm running Python 3.6.3 32bit on Windows 10. I also get the same 
results on Python 3.5.2 on Ubuntu (under WSL)


I'm not super familiar with rebinding stderr with codecs, but I'm 
guessing they are doing it because of some Unicode issue they may have 
been having.


I have a workaround - drop the rebinding - but it seems like there might 
be an error in StreamReaderWriter.

Do other people see the same behaviour?
Is there something I'm not seeing or understanding?
Would I raise it on issue tracker?

Peter

--

import sys
import codecs

sys.stderr = codecs.StreamReaderWriter(
    sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'), 
'replace')


# This should work fine in Py 2, but raise an exception in Py3
# But instead Py3 "swallows" the exception and it is never seen
print(xrange(1, 10))

# Although this line doesn't show in Py 3 (as the script has silently 
crashed)

print("This line doesn't show in Py 3")

--





--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Problem with StreamReaderWriter on 3.6.3? SOLVED

2017-10-20 Thread Peter via Python-list

Thanks MRAB, your solution works a treat.

I'm replying to the list so others can know that this solution works. 
Note that sys.stderr.detach() is only available in >= 3.1, so one might 
need to do some version checking to get it to work properly in both 
versions. It also can mess up with the buffering and therefore the order 
of the output of stderr vs stdout.


Thanks again.

Peter


On 20/10/2017 10:19 AM, MRAB wrote:

On 2017-10-19 22:46, Peter via Python-list wrote:

I came across this code in Google cpplint.py, a Python script for
linting C++ code. I was getting funny results with Python 3.6.3, but it
worked fine under 2.7.13

I've tracked the problem to an issue with StreamReaderWriter; the
traceback and error never shows under 3. The _cause_ of the error is
clear (xrange not in Py3), but I need the raised exception to show.

I'm running Python 3.6.3 32bit on Windows 10. I also get the same
results on Python 3.5.2 on Ubuntu (under WSL)

I'm not super familiar with rebinding stderr with codecs, but I'm
guessing they are doing it because of some Unicode issue they may have
been having.

I have a workaround - drop the rebinding - but it seems like there might
be an error in StreamReaderWriter.
Do other people see the same behaviour?
Is there something I'm not seeing or understanding?
Would I raise it on issue tracker?

Peter

--

import sys
import codecs

sys.stderr = codecs.StreamReaderWriter(
      sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'),
'replace')

# This should work fine in Py 2, but raise an exception in Py3
# But instead Py3 "swallows" the exception and it is never seen
print(xrange(1, 10))

# Although this line doesn't show in Py 3 (as the script has silently
crashed)
print("This line doesn't show in Py 3")

--

StreamReaderWriter is being passed an encoder which returns bytes 
(UTF-8), but the output stream that is being passed, to which it will 
be writing those butes, i.e. the original sys.stderr, expects str.


I'd get the underlying byte stream of stderr using .detach():

sys.stderr = codecs.StreamReaderWriter(sys.stderr.detach(), 
codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace')





--
https://mail.python.org/mailman/listinfo/python-list


Unexpected behaviour with DeprecationWarning, Python 3.7 and escape codes

2018-08-23 Thread Peter via Python-list
I understand that Python 3.7 now issues DeprecationWarning for code 
entered in the interactive shell and also for single-module programs. I 
see this behaviour with:


C:\wrk> python
python 3.7.0 (v3.7.0:...

import imp

__main__:1: DeprecationWarning: the imp module is deprecated...

But if I use an unknown escape code, then the expected warning doesn't 
issue:



print('Hello \world')

Hello \world

But if I explicitly turn on default warnings, then I do get it:

C:\wrk> python -Wd

print('Hello \world')

:1: DeprecationWarning: invalid escape sequence \w
Hello \world


Shouldn't I see the "invalid escape sequence" deprecation warning under 
3.7 without having to turn on default warnings? They are both warnings 
of class DeprecationWarning. Am I not seeing something? Is this a bug?


I suspect it might have something to do with /when/ the warning is 
evaluated (parsing time vs runtime), but I'm not sure, and I'm not sure 
if that should be relevant to whether the warning is in fact raised. (I 
am aware of PEP 565)


Running CPython 3.7.0 on Windows 10, standard

Thanks for your insights.

Peter


--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: CURSES WINDOWS

2018-09-05 Thread Peter via Python-list

I get this:


C:\Users\Me> py
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 
bit (Inte

l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import curses
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Program Files (x86)\Python36-32\lib\curses\__init__.py", 
line 13, in


    from _curses import *
ModuleNotFoundError: No module named '_curses'
>>>


I get the same on py 2.7



On 5/09/2018 4:59 PM, Anssi Saari wrote:

shinobi@f153.n1.z21.fsxnet (shinobi) writes:


Hello All,

can anyone please let me know what's the path to port linux python curses
program to Windows?

Is there really anything that needs to be done? At least a simple hello
world python curses program runs on Windows and Linux with no changes.





--
https://mail.python.org/mailman/listinfo/python-list


Re: Debug script under pdb, how to avoid a bunch of errors caused by the exit()?

2018-09-06 Thread Peter via Python-list
I'm on 3.7.0 on Win 10, and get a different result. No traceback. 
Perhaps it's a bug in 3.4 that was fixed subsequently.


C:\test> py -m pdb bugInPDB.py
> c:\test\buginpdb.py(1)()
-> password = 'bad'
(Pdb) tbreak 3
Breakpoint 1 at c:\test\buginpdb.py:3
(Pdb) cont
Deleted breakpoint 1 at c:\test\buginpdb.py:3
> c:\test\buginpdb.py(3)()
-> print('bad password')
(Pdb) cont
bad password
The program exited via sys.exit(). Exit status: None
> c:\test\buginpdb.py(1)()
-> password = 'bad'
(Pdb) q

C:\test>

On 6/09/2018 7:38 PM, Jach Fong wrote:

Here the script file, test0.py:
--
password = 'bad'
if password == 'bad':
    print('bad password')
    exit()
else:
    print('good password')

print('something else to do')


    When running it under Python3.4 Windows Vista, no problem at all.

D:\Works\Python>py test0.py
bad password

    But if debug it under pdb:

D:\Works\Python>py -m pdb test0.py
> d:\works\python\test0.py(1)()
-> password = 'bad'
(Pdb) tbreak 3
Breakpoint 1 at d:\works\python\test0.py:3
(Pdb) cont
Deleted breakpoint 1 at d:\works\python\test0.py:3
> d:\works\python\test0.py(3)()
-> print('bad password')
(Pdb) cont
bad password
The program exited via sys.exit(). Exit status: None
> d:\works\python\test0.py(1)()
-> password = 'bad'
(Pdb) Traceback (most recent call last):
  File "C:\Python34\lib\pdb.py", line 1661, in main
    pdb._runscript(mainpyfile)
  File "C:\Python34\lib\pdb.py", line 1542, in _runscript
    self.run(statement)
  File "C:\Python34\lib\bdb.py", line 431, in run
    exec(cmd, globals, locals)
  File "", line 1, in 
  File "d:\works\python\test0.py", line 1, in 
    password = 'bad'
  File "d:\works\python\test0.py", line 1, in 
    password = 'bad'
  File "C:\Python34\lib\bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "C:\Python34\lib\bdb.py", line 66, in dispatch_line
    self.user_line(frame)
  File "C:\Python34\lib\pdb.py", line 259, in user_line
    self.interaction(frame, None)
  File "C:\Python34\lib\pdb.py", line 346, in interaction
    self._cmdloop()
  File "C:\Python34\lib\pdb.py", line 319, in _cmdloop
    self.cmdloop()
  File "C:\Python34\lib\cmd.py", line 126, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\python34\lib\cmd.py(126)cmdloop()
-> line = input(self.prompt)
(Pdb) Traceback (most recent call last):
  File "C:\Python34\lib\pdb.py", line 1661, in main
    pdb._runscript(mainpyfile)
  File "C:\Python34\lib\pdb.py", line 1542, in _runscript
    self.run(statement)
  File "C:\Python34\lib\bdb.py", line 431, in run
    exec(cmd, globals, locals)
  File "", line 1, in 
  File "d:\works\python\test0.py", line 1, in 
    password = 'bad'
  File "d:\works\python\test0.py", line 1, in 
    password = 'bad'
  File "C:\Python34\lib\bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "C:\Python34\lib\bdb.py", line 66, in dispatch_line
    self.user_line(frame)
  File "C:\Python34\lib\pdb.py", line 259, in user_line
    self.interaction(frame, None)
  File "C:\Python34\lib\pdb.py", line 346, in interaction
    self._cmdloop()
  File "C:\Python34\lib\pdb.py", line 319, in _cmdloop
    self.cmdloop()
  File "C:\Python34\lib\cmd.py", line 126, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python34\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python34\lib\pdb.py", line 1688, in 
    pdb.main()
  File "C:\Python34\lib\pdb.py", line 1680, in main
    pdb.interaction(None, t)
  File "C:\Python34\lib\pdb.py", line 346, in interaction
    self._cmdloop()
  File "C:\Python34\lib\pdb.py", line 319, in _cmdloop
    self.cmdloop()
  File "C:\Python34\lib\cmd.py", line 126, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

D:\Works\Python>

    How to get rid of these?

Best Regards,
Jach Fong


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus





--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Python indentation (3 spaces)

2018-10-07 Thread Peter via Python-list
It's also useful to be aware of the standard tabnanny module for 
"Detection of ambiguous indentation".


Very useful for highlighting problems with tabs and spaces.

Peter


On 8/10/2018 2:32 AM, Terry Reedy wrote:

On 10/5/2018 11:30 PM, Ryan Johnson wrote:

The point that OP is trying to make is that a fixed standard that is
distinguishable from the even-spacing Tab-length convention in code and
text editors will establish a level of trust between the end 
developer and
upstream developers or co-developers who may not have the same 
development

environment.


And my counter point is that a) we will not change the standard and b) 
we deliver an editor that by default enforces the standard, and c) to 
be fair, many other editors will do the same.



For example, the first Python library I ever tried to use was


What library?  From where?

poorly maintained and had spaces on one line with tabs on the next, 
and the
author mixed naming conventions and syntax from Python 2 and 3 in his 
code.

That type of experience doesn’t exactly instill trust in the coding
language’s standards, when a noob tries to use a library they found and
ends up having to debug weird errors with weirder error messages on the
first project they do.


I don't follow the logic.  If someone violates a law, does that make 
the law bad?  And if people follow a law, does that make it good?


People obviously should not distribute buggy messes, at least not 
without warning.  Were you using the library with an unsupported 
version?  Or inform the author or distributor?


Flexibility is great until the learning curve comes into play. That 
said,
there is an easy fix for tab misuse: in Visual Studio Code, you can 
replace

all Tabs with Spaces by highlighting the entire code block, hitting Tab
once and Shift-Tab after.


IDLE does that also.




--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: PEP 394

2018-10-20 Thread Peter via Python-list

I'd imagine the PEP would stay as active, but changed so that:

python2 -> always refers to python v2
python3 -> always refers to python v3
python -> which currently refers to python v2 will change to point to 
python v3


I don't know when the core team would be considering this. The PEP was 
last updated in April this year, so it's clearly still active. I would 
imagine this change would happen when python v2 ends updates at end of 
2019, although I personally would like to see it happen earlier.


Peter L



On 20/10/2018 6:47 PM, Anders Wegge Keller wrote:

På Sat, 20 Oct 2018 12:57:45 +1100
Ben Finney  skrev:

Anders Wegge Keller  writes:


Short and simple: Do you expect PEP 394 to change status

The status of PEP 394 today is “Active”. What change of status would you
expect in this Informational PEP?

  One possible change would be that it would become archived, ceased to be,
bereft of life, resting in peace or maybe even retired.

  I don't have any particular status change I want to see; I merely ask
what other experienced python developers expect to happen.




--
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Peter via Python-list



On 28/10/2018 12:17 AM, Musatov wrote:

I am wondering if Python could be used to write a program that allows:

1. Highlight some text
2. Ctl+HOTKEY1 stores the string of text somewhere as COPIEDTEXT1
3. Highlight another string of text
4. Ctl+HOTKEY1 stores another string of text somewhere as COPIEDTEXT2

THEN

5. Ctl+HOTKEY2 pastes COPIEDTEXT1
6. Ctl+HOTKEY2 pastes COPIEDTEXT2

I found "pyperclip" and "Tkinter" but I don't know where to start.

Thanks,

Musatov

You might find the GUI automation tool Skiuli or the newer SikuliX 
programs useful. They can be used to script a GUI and perform operations.


http://www.sikuli.org/

If you're on Windows then the COM interface can be useful (if the 
program you want to copy from supports it).


Peter


--
https://mail.python.org/mailman/listinfo/python-list


Re: AES Encryption/Decryption

2018-11-02 Thread Peter via Python-list

On 3/11/2018 1:42 AM, Jeff M wrote:

Python newbie here, looking for code samples for encrypting and decrypting 
functions, using AES.  See lots of stuff on the interwebs, but lots of comments 
back an forth about bugs, or implemented incorrect, etc...

I need to encrypt some strings that will be passed around in URL, and then also 
some PII data at rest.

Thanks.

Although not specifically what you asked about, it's also useful to be 
aware of the new secrets module in Python 3.6.

Peter



--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Generators, generator expressions, and loops

2018-11-16 Thread Peter via Python-list

Lovely, succinct answers.


On 17/11/2018 2:44 AM, Ian Kelly wrote:

On Fri, Nov 16, 2018 at 7:57 AM Steve Keller  wrote:

I have looked at generators, generator expressions, and iterators and
I try to get more familiar with these.

1. How would I loop over all (with no upper bound) integers or all
powers of two, for example?

In C it would be

for (int i = 0; ; i++) { ... }  or  for (int i = 1; ; i *= 2) { ... }

In Python, I could define a generator

 def powers():
 i = 1
 while True:
 yield(i)
 i *= 2

 for i in powers():
 ...

More elegant are generator expressions but I cannot think of a way
without giving an upper limit:

 for i in (2 ** i for i in range(100)):
 ...

which looks ugly.  Also, the double for-loop (and also the two loops
in the above exmaple, for + while in the generator) look unnatural,
somehow, i.e. loop over all elements which are created by a loop.

Is there a more beautyful way?

Some options:

from itertools import count

def powers():
 for i in count():
 yield 2 ** i


for i in (2 ** i for i in count()):
 ...


for i in map(lambda x: 2 ** x, count()):
 ...


from functools import partial
from operator import pow

for i in map(partial(pow, 2), count()):
 ...


Take your pick.




--
https://mail.python.org/mailman/listinfo/python-list


Re: Unable to find newly installed Python 3.7.1

2018-11-23 Thread Peter via Python-list

On 23/11/2018 10:51 PM, Edward Popko wrote:

Snake people:


I'm a Java person and thought to try Python for Windows.
I installed Python 3.7.1 (64-bit) hoping for an IDE, documentation and even
a sample or two.
The python-3.7.1-amd64.exe unpacks and installs fine.
Only problem is I cannot find it. There was no install option to add an
shortcut to the
desktop, there is no C:\ folder, no folder added to Program Files or Program
Files (x86).

I have Windows 10, 64 bit professional

Where did it go? Where is the IDE?


You can find out where the executable is with:

   >>> import sys
   >>> sys.executable
   'C:\\Program Files (x86)\\Python37-32\\python.exe'


(Your location may well be different)
Peter
--
https://mail.python.org/mailman/listinfo/python-list