Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Pythonwin/pywin/framework/interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
re.VERBOSE,
).match

tracebackHeader = "Traceback (".encode("ascii")
tracebackHeader = b"Traceback ("

sectionProfile = "Interactive Window"
valueFormatTitle = "FormatTitle"
Expand Down Expand Up @@ -201,7 +201,7 @@ def ColorizeInteractiveCode(self, cdoc, styleStart, stylePyStart):
# It is a PythonColorizer state - seek past the end of the line
# and ask the Python colorizer to color that.
end = startSeg
while end < lengthDoc and cdoc[end] not in "\r\n".encode("ascii"):
while end < lengthDoc and cdoc[end] not in b"\r\n":
end = end + 1
self.ColorizePythonCode(cdoc[:end], startSeg, state)
stylePyStart = self.GetStringStyle(end - 1)
Expand Down
6 changes: 3 additions & 3 deletions Pythonwin/pywin/framework/scriptutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"\n"
)

byte_cr = "\r".encode("ascii")
byte_lf = "\n".encode("ascii")
byte_crlf = "\r\n".encode("ascii")
byte_cr = b"\r"
byte_lf = b"\n"
byte_crlf = b"\r\n"


# A dialog box for the "Run Script" command.
Expand Down
3 changes: 1 addition & 2 deletions Pythonwin/pywin/scintilla/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
# Still not there - lets see if Windows can find it by searching?
dllid = win32api.LoadLibrary("Scintilla.DLL")

# null_byte is str in py2k, bytes on py3k
null_byte = "\0".encode("ascii")
null_byte = b"\0"

## These are from Richedit.h - need to add to win32con or commctrl
EM_GETTEXTRANGE = 1099
Expand Down
6 changes: 3 additions & 3 deletions Pythonwin/pywin/scintilla/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

from . import scintillacon

crlf_bytes = "\r\n".encode("ascii")
lf_bytes = "\n".encode("ascii")
crlf_bytes = b"\r\n"
lf_bytes = b"\n"

# re from pep263 - but we use it both on bytes and strings.
re_encoding_bytes = re.compile("coding[:=]\s*([-\w.]+)".encode("ascii"))
re_encoding_bytes = re.compile(b"coding[:=]\s*([-\w.]+)")
re_encoding_text = re.compile("coding[:=]\s*([-\w.]+)")

ParentScintillaDocument = docview.Document
Expand Down
6 changes: 1 addition & 5 deletions adodbapi/test/adodbapitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@
from adodbapi import ado_consts


def str2bytes(sval):
return sval.encode("latin1")


long = int


Expand Down Expand Up @@ -519,7 +515,7 @@ def testDataTypeDate(self):
)

def testDataTypeBinary(self):
binfld = str2bytes("\x07\x00\xE2\x40*")
binfld = b"\x07\x00\xE2\x40*"
arv = [binfld, adodbapi.Binary(binfld), bytes(binfld)]
if self.getEngine() == "PostgreSQL":
self.helpTestDataType(
Expand Down
8 changes: 2 additions & 6 deletions adodbapi/test/dbapi20.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ def _failUnless(self, expr, msg=None):
# nothing
# - Fix bugs in test_setoutputsize_basic and test_setinputsizes
#
def str2bytes(sval):
if sys.version_info < (3, 0) and isinstance(sval, str):
sval = sval.decode("latin1")
return sval.encode("latin1") # python 3 make unicode into bytes


class DatabaseAPI20Test(unittest.TestCase):
Expand Down Expand Up @@ -910,8 +906,8 @@ def test_Timestamp(self):
# self.assertEqual(str(t1),str(t2))

def test_Binary(self):
b = self.driver.Binary(str2bytes("Something"))
b = self.driver.Binary(str2bytes(""))
b = self.driver.Binary(b"Something")
b = self.driver.Binary(b"")

def test_STRING(self):
_failUnless(
Expand Down
3 changes: 1 addition & 2 deletions com/win32com/demos/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pythoncom
import win32com.server.connect
import win32com.server.util
from pywin32_testutil import str2bytes
from win32com.server.exception import Exception

# This is the IID of the Events interface both Client and Server support.
Expand Down Expand Up @@ -87,7 +86,7 @@ def test(verbose=0):
client = ConnectableClient()
connection.Connect(server, client, IID_IConnectDemoEvents)
CheckEvent(server, client, "Hello", verbose)
CheckEvent(server, client, str2bytes("Here is a null>\x00<"), verbose)
CheckEvent(server, client, b"Here is a null>\x00<", verbose)
CheckEvent(server, client, "Here is a null>\x00<", verbose)
val = "test-\xe0\xf2" # 2 extended characters.
CheckEvent(server, client, val, verbose)
Expand Down
7 changes: 3 additions & 4 deletions com/win32com/test/testPersist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import win32com.client.dynamic
import win32com.server.util
import win32ui
from pywin32_testutil import str2bytes
from pywintypes import Unicode
from win32com import storagecon
from win32com.axcontrol import axcontrol
Expand All @@ -34,8 +33,8 @@ class LockBytes:
]
_com_interfaces_ = [pythoncom.IID_ILockBytes]

def __init__(self, data=""):
self.data = str2bytes(data)
def __init__(self, data=b""):
self.data = data
self.ctime = now
self.mtime = now
self.atime = now
Expand Down Expand Up @@ -68,7 +67,7 @@ def Flush(self, whatsthis=0):
def SetSize(self, size):
print("Set Size" + str(size))
if size > len(self.data):
self.data = self.data + str2bytes("\000" * (size - len(self.data)))
self.data = self.data + b"\000" * (size - len(self.data))
else:
self.data = self.data[0:size]
return S_OK
Expand Down
27 changes: 12 additions & 15 deletions com/win32com/test/testShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import pywintypes
import win32com.test.util
import win32con
from pywin32_testutil import str2bytes
from win32com.shell import shell
from win32com.shell.shellcon import *
from win32com.storagecon import *
Expand Down Expand Up @@ -87,25 +86,23 @@ def _rtCIDA(self, parent, kids):
self.assertEqual(cida_str_rt, cida_str)

def testPIDL(self):
# A PIDL of "\1" is: cb pidl cb
expect = str2bytes("\03\00" "\1" "\0\0")
self.assertEqual(shell.PIDLAsString([str2bytes("\1")]), expect)
self._rtPIDL([str2bytes("\0")])
self._rtPIDL([str2bytes("\1"), str2bytes("\2"), str2bytes("\3")])
self._rtPIDL([str2bytes("\0") * 2048] * 2048)
# A PIDL of "\1" is: cb + pidl + cb
expect = b"\03\00" + b"\1" + b"\0\0"
self.assertEqual(shell.PIDLAsString([b"\1"]), expect)
self._rtPIDL([b"\0"])
self._rtPIDL([b"\1", b"\2", b"\3"])
self._rtPIDL([b"\0" * 2048] * 2048)
# PIDL must be a list
self.assertRaises(TypeError, shell.PIDLAsString, "foo")

def testCIDA(self):
self._rtCIDA([str2bytes("\0")], [[str2bytes("\0")]])
self._rtCIDA([str2bytes("\1")], [[str2bytes("\2")]])
self._rtCIDA(
[str2bytes("\0")], [[str2bytes("\0")], [str2bytes("\1")], [str2bytes("\2")]]
)
self._rtCIDA([b"\0"], [[b"\0"]])
self._rtCIDA([b"\1"], [[b"\2"]])
self._rtCIDA([b"\0"], [[b"\0"], [b"\1"], [b"\2"]])

def testBadShortPIDL(self):
# A too-short child element: cb pidl cb
pidl = str2bytes("\01\00" "\1")
# A too-short child element: cb + pidl + cb
pidl = b"\01\00" + b"\1"
self.assertRaises(ValueError, shell.StringAsPIDL, pidl)

# ack - tried to test too long PIDLs, but a len of 0xFFFF may not
Expand Down Expand Up @@ -224,7 +221,7 @@ def setUp(self):

self.src_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell")
self.dest_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell_dest")
self.test_data = str2bytes("Hello from\0Python")
self.test_data = b"Hello from\0Python"
f = open(self.src_name, "wb")
f.write(self.test_data)
f.close()
Expand Down
11 changes: 5 additions & 6 deletions com/win32com/test/testStreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pythoncom
import win32com.server.util
import win32com.test.util
from pywin32_testutil import str2bytes


class Persists:
Expand All @@ -18,7 +17,7 @@ class Persists:
_com_interfaces_ = [pythoncom.IID_IPersistStreamInit]

def __init__(self):
self.data = str2bytes("abcdefg")
self.data = b"abcdefg"
self.dirty = 1

def GetClassID(self):
Expand Down Expand Up @@ -82,7 +81,7 @@ class BadStream(Stream):
"""

def Read(self, amount):
return str2bytes("x") * (amount + 1)
return b"x" * (amount + 1)


class StreamTest(win32com.test.util.TestCase):
Expand All @@ -98,7 +97,7 @@ def _readWrite(self, data, write_stream, read_stream=None):
self.assertEqual(data[1:-1], got)

def testit(self):
mydata = str2bytes("abcdefghijklmnopqrstuvwxyz")
mydata = b"abcdefghijklmnopqrstuvwxyz"

# First test the objects just as Python objects...
s = Stream(mydata)
Expand All @@ -118,15 +117,15 @@ def testit(self):
self._readWrite(mydata, s2, s)
self._readWrite(mydata, s2, s2)

self._readWrite(str2bytes("string with\0a NULL"), s2, s2)
self._readWrite(b"string with\0a NULL", s2, s2)
# reset the stream
s.Write(mydata)
p2.Load(s2)
p2.Save(s2, 0)
self.assertEqual(s.data, mydata)

def testseek(self):
s = Stream(str2bytes("yo"))
s = Stream(b"yo")
s = win32com.server.util.wrap(s, pythoncom.IID_IStream)
# we used to die in py3k passing a value > 32bits
s.Seek(0x100000000, pythoncom.STREAM_SEEK_SET)
Expand Down
2 changes: 1 addition & 1 deletion com/win32comext/mapi/mapiutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def GetAllProperties(obj, make_tag_names=True):
_MapiTypeMap = {
type(0.0): mapitags.PT_DOUBLE,
type(0): mapitags.PT_I4,
type("".encode("ascii")): mapitags.PT_STRING8, # bytes
type(b""): mapitags.PT_STRING8, # bytes
type(""): mapitags.PT_UNICODE, # str
type(None): mapitags.PT_UNSPECIFIED,
# In Python 2.2.2, bool isn't a distinct type (type(1==1) is type(0)).
Expand Down
4 changes: 2 additions & 2 deletions win32/Demos/BackupRead_BackupWrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import win32con
import win32file
import win32security
from pywin32_testutil import ob2memory, str2bytes
from pywin32_testutil import ob2memory
from win32com import storagecon

all_sd_info = (
Expand Down Expand Up @@ -103,7 +103,7 @@
)
print("Written:", bytes_written, "Context:", outctxt)
win32file.BackupRead(h, 0, buf, True, True, ctxt)
win32file.BackupWrite(outh, 0, str2bytes(""), True, True, outctxt)
win32file.BackupWrite(outh, 0, b"", True, True, outctxt)
win32file.CloseHandle(h)
win32file.CloseHandle(outh)

Expand Down
5 changes: 2 additions & 3 deletions win32/Demos/CreateFileTransacted_MiniVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import win32transaction
import winerror
import winioctlcon
from pywin32_testutil import str2bytes # py3k-friendly helper


def demo():
Expand Down Expand Up @@ -49,14 +48,14 @@ def demo():
Transaction=trans,
)

win32file.WriteFile(hfile, str2bytes("This is first miniversion.\n"))
win32file.WriteFile(hfile, b"This is first miniversion.\n")
buf = win32file.DeviceIoControl(
hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION, None, buf_size, None
)
struct_ver, struct_len, base_ver, ver_1 = struct.unpack(buf_fmt, buf)

win32file.SetFilePointer(hfile, 0, win32con.FILE_BEGIN)
win32file.WriteFile(hfile, str2bytes("This is second miniversion!\n"))
win32file.WriteFile(hfile, b"This is second miniversion!\n")
buf = win32file.DeviceIoControl(
hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION, None, buf_size, None
)
Expand Down
6 changes: 3 additions & 3 deletions win32/Demos/eventLogDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,23 @@ def test():
logType,
2,
strings=["The message text for event 2", "Another insert"],
data="Raw\0Data".encode("ascii"),
data=b"Raw\0Data",
sid=my_sid,
)
win32evtlogutil.ReportEvent(
logType,
1,
eventType=win32evtlog.EVENTLOG_WARNING_TYPE,
strings=["A warning", "An even more dire warning"],
data="Raw\0Data".encode("ascii"),
data=b"Raw\0Data",
sid=my_sid,
)
win32evtlogutil.ReportEvent(
logType,
1,
eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["An info", "Too much info"],
data="Raw\0Data".encode("ascii"),
data=b"Raw\0Data",
sid=my_sid,
)
print("Successfully wrote 3 records to the log")
Expand Down
17 changes: 8 additions & 9 deletions win32/Demos/mmapfile_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import mmapfile
import win32api
import winerror
from pywin32_testutil import str2bytes

system_info = win32api.GetSystemInfo()
page_size = system_info[1]
Expand All @@ -17,34 +16,34 @@

m1 = mmapfile.mmapfile(File=fname, Name=mapping_name, MaximumSize=fsize)
m1.seek(100)
m1.write_byte(str2bytes("?"))
m1.write_byte(b"?")
m1.seek(-1, 1)
assert m1.read_byte() == str2bytes("?")
assert m1.read_byte() == b"?"

## A reopened named mapping should have exact same size as original mapping
m2 = mmapfile.mmapfile(Name=mapping_name, File=None, MaximumSize=fsize * 2)
assert m2.size() == m1.size()
m1.seek(0, 0)
m1.write(fsize * str2bytes("s"))
assert m2.read(fsize) == fsize * str2bytes("s")
m1.write(fsize * b"s")
assert m2.read(fsize) == fsize * b"s"

move_src = 100
move_dest = 500
move_size = 150

m2.seek(move_src, 0)
assert m2.tell() == move_src
m2.write(str2bytes("m") * move_size)
m2.write(b"m" * move_size)
m2.move(move_dest, move_src, move_size)
m2.seek(move_dest, 0)
assert m2.read(move_size) == str2bytes("m") * move_size
assert m2.read(move_size) == b"m" * move_size
## m2.write('x'* (fsize+1))

m2.close()
m1.resize(fsize * 2)
assert m1.size() == fsize * 2
m1.seek(fsize)
m1.write(str2bytes("w") * fsize)
m1.write(b"w" * fsize)
m1.flush()
m1.close()
os.remove(fname)
Expand All @@ -54,7 +53,7 @@
## need 10 GB free on drive where your temp folder lives
fname_large = tempfile.mktemp()
mapping_name = "Pywin32_large_mmap"
offsetdata = str2bytes("This is start of offset")
offsetdata = b"This is start of offset"

## Deliberately use odd numbers to test rounding logic
fsize = (1024 * 1024 * 1024 * 10) + 333
Expand Down
2 changes: 1 addition & 1 deletion win32/Demos/service/pipeTestService.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def DoProcessClient(self, pipeHandle, tid):
try:
# Create a loop, reading large data. If we knew the data stream was
# was small, a simple ReadFile would do.
d = "".encode("ascii") # ensure bytes on py2k and py3k...
d = b""
hr = winerror.ERROR_MORE_DATA
while hr == winerror.ERROR_MORE_DATA:
hr, thisd = ReadFile(pipeHandle, 256)
Expand Down
Loading