[issue28905] re.sub appears not to check count optional argument for integerness

2016-12-08 Thread Danny Yoo

Danny Yoo added the comment:

Alternatively, change the representation of flag values from integers to some 
class extension that supports the common bitwise operators.

As a very rough sketch:

>>> class FlagInt(int):
... def __or__(self, other):
... return FlagInt(int(self) | int(other))
... 
>>> f1 = FlagInt(1)
>>> f2 = FlagInt(2)
>>> f1 | f2
3
>>> isinstance(3, FlagInt)
False
>>> isinstance(f1 | f2, FlagInt)
True


That way, flag arguments can be determined at runtime to have derived from the 
proper flag values.

This kind of approach may have some backwards-incompatibility, unfortunately, 
since other folks have been hardcoding integers rather than use the flag 
constants.  Other concerns might include serialization, in case someone tries 
to save a FlagInt somewhere and pull it out at some other time.

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28905>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28905] re.sub appears not to check count optional argument for integerness

2016-12-08 Thread Danny Yoo

Danny Yoo added the comment:

Ugh.  I suddenly realize that this is complicated by the fact that flag values 
are themselves represented as integers, and Python's type system isn't rich 
enough to label flag values as a distinct type for the purposes.

It may be worthwhile to add a warning in the documentation about this, as it is 
an easy mistake to make.

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28905>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28905] re.sub appears not to check count optional argument for integerness

2016-12-08 Thread Danny Yoo

New submission from Danny Yoo:

This comes from diagnosing a beginner's question on Python-tutor.

https://mail.python.org/pipermail/tutor/2016-December/110066.html

It appears that re.sub is not checking whether the count argument is integer or 
not, and silently accepts a nonsensical argument.  For example:


>>> import re
>>> s = "AAAcBBB\nAAAdBBB"
>>> print(re.sub(r'^AAA', "aaa", s, re.MULTILINE))
aaacBBB
AAAdBBB


Of course, the user intended to pass re.MULTILINE to flags, not to count, but 
the fact that this isn't raising a TypeError is error-prone.

--
components: Library (Lib)
messages: 282719
nosy: Danny Yoo
priority: normal
severity: normal
status: open
title: re.sub appears not to check count optional argument for integerness
type: behavior
versions: Python 3.4

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28905>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23306] zlib.crc32 raises OverflowError at argument-parsing time on large strings

2015-01-23 Thread Danny Yoo

Danny Yoo added the comment:

Unfortunately, fixing just zlib.crc32 isn't quite enough for our purposes.  We 
still will see OverflowErrow in zipfile if compression is selected.


Demonstration code:


import zipfile

## Possible workaround: monkey-patch crc32 from binascii?!
import binascii
zipfile.crc32 = binascii.crc32

content = 'a'*(131)
filename = '/tmp/zip_test.zip'

zf = zipfile.ZipFile(filename, w,
 compression=zipfile.ZIP_DEFLATED,
 allowZip64=True)
zf.writestr('big', content)
zf.close()

zf = zipfile.ZipFile(filename, r, allowZip64=True)
print zf.open('big').read() == content
#


This will raise the following error under Python 2.7.6:

#
$ python zip_test.py
Traceback (most recent call last):
  File zip_test.py, line 13, in module
zf.writestr('big', content)
  File /usr/lib/python2.7/zipfile.py, line 1228, in writestr
bytes = co.compress(bytes) + co.flush()
OverflowError: size does not fit in an int
#



If we use compression=zipfile.ZIP_STORED, we don't see this error, but it kind 
of misses a major point of using zipfile.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23306
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23306] zlib.crc32 raises OverflowError at argument-parsing time on large strings

2015-01-23 Thread Danny Yoo

New submission from Danny Yoo:

Reproduction steps:

---
$ python2.7 -c import zlib;zlib.crc32('a'*(131))
Traceback (most recent call last):
  File string, line 1, in module
OverflowError: size does not fit in an int
---

We ran into this bug in zlib.crc32 when using zipfile.writestr() with a very 
large string; as soon as zipfile tried to write the crc checksum, it raised 
this error.


Python 3 does not appear to suffer from this bug.

--
components: Library (Lib)
messages: 234587
nosy: Danny.Yoo, gregory.p.smith
priority: normal
severity: normal
status: open
title: zlib.crc32 raises OverflowError at argument-parsing time on large strings
type: behavior
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23306
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23306] Within zipfile, use of zlib.crc32 raises OverflowError at argument-parsing time on large strings

2015-01-23 Thread Danny Yoo

Changes by Danny Yoo danny...@google.com:


--
title: zlib.crc32 raises OverflowError at argument-parsing time on large 
strings - Within zipfile, use of zlib.crc32 raises OverflowError at 
argument-parsing time on large strings

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23306
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com