Skip to content

Commit 2b44aa1

Browse files
committed
Implementing compat function codecs_open
1 parent 137687e commit 2b44aa1

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

data/txt/sha256sums.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ c4bfb493a03caf84dd362aec7c248097841de804b7413d0e1ecb8a90c8550bc0 lib/core/readl
188188
d1bd70c1a55858495c727fbec91e30af267459c8f64d50fabf9e4ee2c007e920 lib/core/replication.py
189189
1d0f80b0193ac5204527bfab4bde1a7aee0f693fd008e86b4b29f606d1ef94f3 lib/core/revision.py
190190
d2eb8e4b05ac93551272b3d4abfaf5b9f2d3ac92499a7704c16ed0b4f200db38 lib/core/session.py
191-
8c5ad7a690a3c6843ad22c8d88c7facd33307c200a435820e7dcb1d7d7ccce4a lib/core/settings.py
191+
07334f49a15c569a20523fcf2c7a8706d9e253e6b44c9cdb897796f91898f22a lib/core/settings.py
192192
1c5eab9494eb969bc9ce118a2ea6954690c6851cbe54c18373c723b99734bf09 lib/core/shell.py
193193
4eea6dcf023e41e3c64b210cb5c2efc7ca893b727f5e49d9c924f076bb224053 lib/core/subprocessng.py
194194
cdd352e1331c6b535e780f6edea79465cb55af53aa2114dcea0e8bf382e56d1a lib/core/target.py

lib/core/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from extra.cloak.cloak import decloak
4848
from lib.core.bigarray import BigArray
4949
from lib.core.compat import cmp
50+
from lib.core.compat import codecs_open
5051
from lib.core.compat import LooseVersion
5152
from lib.core.compat import round
5253
from lib.core.compat import xrange
@@ -3819,7 +3820,7 @@ def openFile(filename, mode='r', encoding=UNICODE_ENCODING, errors="reversible",
38193820
return contextlib.closing(io.StringIO(readCachedFileContent(filename)))
38203821
else:
38213822
try:
3822-
return codecs.open(filename, mode, encoding, errors, buffering)
3823+
return codecs_open(filename, mode, encoding, errors, buffering)
38233824
except IOError:
38243825
errMsg = "there has been a file opening error for filename '%s'. " % filename
38253826
errMsg += "Please check %s permissions on a file " % ("write" if mode and ('w' in mode or 'a' in mode or '+' in mode) else "read")

lib/core/compat.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
from __future__ import division
99

10+
import codecs
1011
import binascii
1112
import functools
13+
import io
1214
import math
1315
import os
1416
import random
@@ -312,3 +314,116 @@ def LooseVersion(version):
312314
result = float("NaN")
313315

314316
return result
317+
318+
# NOTE: codecs.open re-implementation (deprecated in Python 3.14)
319+
320+
try:
321+
# Py2
322+
_text_type = unicode
323+
_bytes_types = (str, bytearray)
324+
except NameError:
325+
# Py3
326+
_text_type = str
327+
_bytes_types = (bytes, bytearray, memoryview)
328+
329+
_WRITE_CHARS = ("w", "a", "x", "+")
330+
331+
def _is_write_mode(mode):
332+
return any(ch in mode for ch in _WRITE_CHARS)
333+
334+
class MixedWriteTextIO(object):
335+
"""
336+
Text-ish stream wrapper that accepts both text and bytes in write().
337+
Bytes are decoded using the file's (encoding, errors) before writing.
338+
339+
Optionally approximates line-buffering by flushing when a newline is written.
340+
"""
341+
def __init__(self, fh, encoding, errors, line_buffered=False):
342+
self._fh = fh
343+
self._encoding = encoding
344+
self._errors = errors
345+
self._line_buffered = line_buffered
346+
347+
def write(self, data):
348+
# bytes-like but not text -> decode
349+
if isinstance(data, _bytes_types) and not isinstance(data, _text_type):
350+
data = bytes(data).decode(self._encoding, self._errors)
351+
elif not isinstance(data, _text_type):
352+
data = _text_type(data)
353+
354+
n = self._fh.write(data)
355+
356+
# Approximate "line buffering" behavior if requested
357+
if self._line_buffered and u"\n" in data:
358+
try:
359+
self._fh.flush()
360+
except Exception:
361+
pass
362+
363+
return n
364+
365+
def writelines(self, lines):
366+
for x in lines:
367+
self.write(x)
368+
369+
def __iter__(self):
370+
return iter(self._fh)
371+
372+
def __next__(self):
373+
return next(self._fh)
374+
375+
def next(self): # Py2
376+
return self.__next__()
377+
378+
def __getattr__(self, name):
379+
return getattr(self._fh, name)
380+
381+
def __enter__(self):
382+
self._fh.__enter__()
383+
return self
384+
385+
def __exit__(self, exc_type, exc, tb):
386+
return self._fh.__exit__(exc_type, exc, tb)
387+
388+
389+
def _codecs_open(filename, mode="r", encoding=None, errors="strict", buffering=-1):
390+
"""
391+
Replacement for deprecated codecs.open() entry point with sqlmap-friendly behavior.
392+
393+
- If encoding is None: return io.open(...) as-is.
394+
- If encoding is set: force underlying binary mode and wrap via StreamReaderWriter
395+
(like codecs.open()).
396+
- For write-ish modes: return a wrapper that also accepts bytes on .write().
397+
- Handles buffering=1 in binary mode by downgrading underlying buffering to -1,
398+
while optionally preserving "flush on newline" behavior in the wrapper.
399+
"""
400+
if encoding is None:
401+
return io.open(filename, mode, buffering=buffering)
402+
403+
bmode = mode
404+
if "b" not in bmode:
405+
bmode += "b"
406+
407+
# Avoid line-buffering warnings/errors on binary streams
408+
line_buffered = (buffering == 1)
409+
if line_buffered:
410+
buffering = -1
411+
412+
f = io.open(filename, bmode, buffering=buffering)
413+
414+
try:
415+
info = codecs.lookup(encoding)
416+
srw = codecs.StreamReaderWriter(f, info.streamreader, info.streamwriter, errors)
417+
srw.encoding = encoding
418+
419+
if _is_write_mode(mode):
420+
return MixedWriteTextIO(srw, encoding, errors, line_buffered=line_buffered)
421+
422+
return srw
423+
except Exception:
424+
try:
425+
f.close()
426+
finally:
427+
raise
428+
429+
codecs_open = _codecs_open if sys.version_info >= (3, 14) else codecs.open

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from thirdparty import six
2020

2121
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
22-
VERSION = "1.9.12.16"
22+
VERSION = "1.9.12.17"
2323
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2424
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2525
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

0 commit comments

Comments
 (0)