1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

python: Make top-level samba modules Python 3 compatible

New file compat.py will help with porting to Python 3. For now, it
contains only PY3 variable based on six.PY3 which simplifies
condition mentioned below.

The added `if not PY3` conditions enable us to bootstrap running
tests with Python 3 even if most modules are not ported yet.
The plan is to move modules outside this condition as they are ported.
The `PY3` condition is currently used only in tests and for
the samba._ldb module which is not ported yet and has a lot of
dependencies.

The other changes are related to differences between Python 2 and 3.
Python 2.6 introduced the `0o` prefix for octal literals as an
alternative to plain `0`. In Python 3, support for plain `0` is
dropped and octal literals have to start with `0o` prefix.
Python 2.6 introduced a clearer `except` syntax:
`except ExceptionType as target:` instead of
`except ExceptionType, target:`. In Python 3, the old syntax
is no longer allowed.

Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
This commit is contained in:
Lumir Balhar 2016-12-13 11:26:53 +01:00 committed by Andrew Bartlett
parent 211df4a1e4
commit 6fa125e121
3 changed files with 57 additions and 23 deletions

View File

@ -25,7 +25,15 @@ __docformat__ = "restructuredText"
import os
import sys
import time
import ldb
from samba.compat import PY3
import samba.param
from samba import _glue
if not PY3:
from samba._ldb import Ldb as _Ldb
else:
# samba._ldb is not yet ported to Python 3
_Ldb = object
def source_tree_topdir():
@ -47,10 +55,6 @@ def in_source_tree():
return True
import ldb
from samba._ldb import Ldb as _Ldb
class Ldb(_Ldb):
"""Simple Samba-specific LDB subclass that takes care
of setting up the modules dir, credentials pointers, etc.
@ -98,7 +102,7 @@ class Ldb(_Ldb):
# TODO set debug
def msg(l, text):
print text
print(text)
#self.set_debug(msg)
self.set_utf8_casefold()
@ -109,7 +113,7 @@ class Ldb(_Ldb):
if nosync_p is not None and nosync_p:
flags |= ldb.FLG_NOSYNC
self.set_create_perms(0600)
self.set_create_perms(0o600)
if url is not None:
self.connect(url, flags, options)
@ -141,7 +145,8 @@ class Ldb(_Ldb):
try:
res = self.search(base=dn, scope=ldb.SCOPE_SUBTREE, attrs=[],
expression="(|(objectclass=user)(objectclass=computer))")
except ldb.LdbError, (errno, _):
except ldb.LdbError as error:
(errno, estr) = error.args
if errno == ldb.ERR_NO_SUCH_OBJECT:
# Ignore no such object errors
return
@ -151,7 +156,8 @@ class Ldb(_Ldb):
try:
for msg in res:
self.delete(msg.dn, ["relax:0"])
except ldb.LdbError, (errno, _):
except ldb.LdbError as error:
(errno, estr) = error.args
if errno != ldb.ERR_NO_SUCH_OBJECT:
# Ignore no such object errors
raise
@ -175,7 +181,8 @@ class Ldb(_Ldb):
[], controls=["show_deleted:0", "show_recycled:0"]):
try:
self.delete(msg.dn, ["relax:0"])
except ldb.LdbError, (errno, _):
except ldb.LdbError as error:
(errno, estr) = error.args
if errno != ldb.ERR_NO_SUCH_OBJECT:
# Ignore no such object errors
raise
@ -190,7 +197,8 @@ class Ldb(_Ldb):
"@OPTIONS", "@PARTITION", "@KLUDGEACL"]:
try:
self.delete(attr, ["relax:0"])
except ldb.LdbError, (errno, _):
except ldb.LdbError as error:
(errno, estr) = error.args
if errno != ldb.ERR_NO_SUCH_OBJECT:
# Ignore missing dn errors
raise
@ -203,7 +211,8 @@ class Ldb(_Ldb):
for attr in ["@INDEXLIST", "@ATTRIBUTES"]:
try:
self.delete(attr, ["relax:0"])
except ldb.LdbError, (errno, _):
except ldb.LdbError as error:
(errno, estr) = error.args
if errno != ldb.ERR_NO_SUCH_OBJECT:
# Ignore missing dn errors
raise
@ -386,7 +395,6 @@ def arcfour_encrypt(key, data):
raise Exception("arcfour_encrypt() requires " +
"python*-crypto or python*-m2crypto or m2crypto")
import _glue
version = _glue.version
interface_ips = _glue.interface_ips
set_debug_level = _glue.set_debug_level

22
python/samba/compat.py Normal file
View File

@ -0,0 +1,22 @@
# module which helps with porting to Python 3
#
# Copyright (C) Lumir Balhar <lbalhar@redhat.com> 2017
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""module which helps with porting to Python 3"""
import sys
PY3 = sys.version_info[0] == 3

View File

@ -21,22 +21,25 @@
import os
import ldb
import samba
import samba.auth
from samba import param
from samba.samdb import SamDB
from samba import credentials
import samba.ndr
import samba.dcerpc.dcerpc
import samba.dcerpc.base
import samba.dcerpc.epmapper
from samba.credentials import Credentials
from samba import gensec
import socket
import struct
import subprocess
import sys
import tempfile
import unittest
from samba.compat import PY3
if not PY3:
# Py2 only
import samba.auth
from samba.samdb import SamDB
import samba.ndr
import samba.dcerpc.dcerpc
import samba.dcerpc.base
import samba.dcerpc.epmapper
from samba import gensec
try:
from unittest import SkipTest
@ -136,7 +139,7 @@ class TestCase(unittest.TestCase):
try:
try:
self.setUp()
except SkipTest, e:
except SkipTest as e:
self._addSkip(result, str(e))
return
except KeyboardInterrupt:
@ -149,7 +152,7 @@ class TestCase(unittest.TestCase):
try:
testMethod()
ok = True
except SkipTest, e:
except SkipTest as e:
self._addSkip(result, str(e))
return
except self.failureException:
@ -161,7 +164,7 @@ class TestCase(unittest.TestCase):
try:
self.tearDown()
except SkipTest, e:
except SkipTest as e:
self._addSkip(result, str(e))
except KeyboardInterrupt:
raise
@ -380,5 +383,6 @@ def connect_samdb_env(env_url, env_username, env_password, lp=None):
def delete_force(samdb, dn):
try:
samdb.delete(dn)
except ldb.LdbError, (num, errstr):
except ldb.LdbError as error:
(num, errstr) = error.args
assert num == ldb.ERR_NO_SUCH_OBJECT, "ldb.delete() failed: %s" % errstr