diff --git a/python/samba/__init__.py b/python/samba/__init__.py index 19d5e38e896..a5db0f0353e 100644 --- a/python/samba/__init__.py +++ b/python/samba/__init__.py @@ -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 diff --git a/python/samba/compat.py b/python/samba/compat.py new file mode 100644 index 00000000000..c8215f3f6af --- /dev/null +++ b/python/samba/compat.py @@ -0,0 +1,22 @@ +# module which helps with porting to Python 3 +# +# Copyright (C) Lumir Balhar 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 . + +"""module which helps with porting to Python 3""" + +import sys + +PY3 = sys.version_info[0] == 3 diff --git a/python/samba/tests/__init__.py b/python/samba/tests/__init__.py index e54b09853ff..ba215ea9e84 100644 --- a/python/samba/tests/__init__.py +++ b/python/samba/tests/__init__.py @@ -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