URL: https://github.com/freeipa/freeipa/pull/458
Author: tiran
 Title: #458: Bytes deprecation warnings
Action: opened

PR body:
"""
* Enable bytes and deprecation warnings
* Fix a couple of bytes and deprecation warnings

https://fedorahosted.org/freeipa/ticket/6631
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/458/head:pr458
git checkout pr458
From 5b17ca0069867b620e1ba5bfc58830a4e26b858d Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Fri, 10 Feb 2017 14:19:07 +0100
Subject: [PATCH 1/4] Enable additional warnings (BytesWarning,
 DeprecationWarning)

Closes: https://fedorahosted.org/freeipa/ticket/6631
Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipalib/__init__.py     | 46 +++++++++++++++++++++++++++++++++++++++-------
 ipatests/ipa-run-tests |  1 +
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/ipalib/__init__.py b/ipalib/__init__.py
index 4f09090..7949086 100644
--- a/ipalib/__init__.py
+++ b/ipalib/__init__.py
@@ -880,8 +880,46 @@ class my_command(Command):
     http://freeipa.org/page/Contribute
 
 '''
+version_info = (2, 0, 0, 'alpha', 0)
+if version_info[3] == 'final':
+    __version__ = '%d.%d.%d' % version_info[:3]
+else:
+    __version__ = '%d.%d.%d.%s.%d' % version_info
+
 
-import os
+def _enable_warnings(error=False):
+    """Enable additional warnings during development
+    """
+    import ctypes
+    import warnings
+
+    # get reference to Py_BytesWarningFlag from Python CAPI
+    byteswarnings = ctypes.c_int.in_dll(  # pylint: disable=no-member
+        ctypes.pythonapi, 'Py_BytesWarningFlag')
+
+    if byteswarnings.value >= 2:
+        # bytes warnings flag already set to error
+        return
+
+    # default warning mode for all modules: warn once per location
+    warnings.simplefilter('default', BytesWarning)
+    if error:
+        byteswarnings.value = 2
+        action = 'error'
+    else:
+        byteswarnings.value = 1
+        action = 'default'
+
+    module = '(ipa.*|__main__)'
+    warnings.filterwarnings(action, category=BytesWarning, module=module)
+    warnings.filterwarnings(action, category=DeprecationWarning,
+                            module=module)
+
+# call this as early as possible
+if version_info[3] != 'final':
+    _enable_warnings(False)
+
+# noqa: E402
 from ipalib import plugable
 from ipalib.backend import Backend
 from ipalib.frontend import Command, LocalOrRemote, Updater
@@ -893,12 +931,6 @@ class my_command(Command):
 from ipalib.errors import SkipPluginModule
 from ipalib.text import _, ngettext, GettextFactory, NGettextFactory
 
-version_info = (2, 0, 0, 'alpha', 0)
-if version_info[3] == 'final':
-    __version__ = '%d.%d.%d' % version_info[:3]
-else:
-    __version__ = '%d.%d.%d.%s.%d' % version_info
-
 Registry = plugable.Registry
 
 
diff --git a/ipatests/ipa-run-tests b/ipatests/ipa-run-tests
index cafd993..6b2070b 100755
--- a/ipatests/ipa-run-tests
+++ b/ipatests/ipa-run-tests
@@ -32,6 +32,7 @@ import sys
 
 import pytest
 
+import ipalib
 import ipatests
 
 # This is set to store --with-xunit report in an accessible place:

From 64704c317d55729e9a90be55c9fc0422fae0fe08 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Fri, 10 Feb 2017 14:20:22 +0100
Subject: [PATCH 2/4] cryptography has deprecated serial in favor of
 serial_number

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipalib/install/certstore.py          | 2 +-
 ipaserver/install/cainstance.py      | 4 ++--
 ipaserver/plugins/cert.py            | 4 ++--
 ipaserver/plugins/service.py         | 4 ++--
 ipatests/test_ipalib/test_x509.py    | 2 +-
 ipatests/test_ipaserver/test_ldap.py | 8 ++++----
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/ipalib/install/certstore.py b/ipalib/install/certstore.py
index 70ae942..310e08e 100644
--- a/ipalib/install/certstore.py
+++ b/ipalib/install/certstore.py
@@ -33,7 +33,7 @@ def _parse_cert(dercert):
         cert = x509.load_certificate(dercert, x509.DER)
         subject = DN(cert.subject)
         issuer = DN(cert.issuer)
-        serial_number = cert.serial
+        serial_number = cert.serial_number
         public_key_info = x509.get_der_public_key_info(dercert, x509.DER)
     except (ValueError, PyAsn1Error) as e:
         raise ValueError("failed to decode certificate: %s" % e)
diff --git a/ipaserver/install/cainstance.py b/ipaserver/install/cainstance.py
index a73a9c4..d869641 100644
--- a/ipaserver/install/cainstance.py
+++ b/ipaserver/install/cainstance.py
@@ -702,7 +702,7 @@ def __create_ca_agent(self):
             userstate=["1"],
             userCertificate=[cert_data],
             description=['2;%s;%s;%s' % (
-                cert.serial,
+                cert.serial_number,
                 DN(self.ca_subject),
                 DN(('CN', 'IPA RA'), self.subject_base))])
         conn.add_entry(entry)
@@ -1437,7 +1437,7 @@ def make_filter(dercert):
 
     def make_entry(dercert, entry):
         cert = x509.load_certificate(dercert, datatype=x509.DER)
-        entry['authoritySerial'] = cert.serial
+        entry['authoritySerial'] = cert.serial_number
         return entry
 
     return __update_entry_from_cert(make_filter, make_entry, dercert)
diff --git a/ipaserver/plugins/cert.py b/ipaserver/plugins/cert.py
index 6bf5c03..0852197 100644
--- a/ipaserver/plugins/cert.py
+++ b/ipaserver/plugins/cert.py
@@ -387,7 +387,7 @@ def _parse(self, obj, full=True):
             cert = x509.load_certificate(obj['certificate'])
             obj['subject'] = DN(cert.subject)
             obj['issuer'] = DN(cert.issuer)
-            obj['serial_number'] = cert.serial
+            obj['serial_number'] = cert.serial_number
             obj['valid_not_before'] = x509.format_datetime(
                     cert.not_valid_before)
             obj['valid_not_after'] = x509.format_datetime(
@@ -1257,7 +1257,7 @@ def _get_cert_key(self, cert):
 
             raise
 
-        return (DN(cert_obj.issuer), cert_obj.serial)
+        return (DN(cert_obj.issuer), cert_obj.serial_number)
 
     def _get_cert_obj(self, cert, all, raw, pkey_only):
         obj = {'certificate': base64.b64encode(cert).decode('ascii')}
diff --git a/ipaserver/plugins/service.py b/ipaserver/plugins/service.py
index ddae37f..0c49808 100644
--- a/ipaserver/plugins/service.py
+++ b/ipaserver/plugins/service.py
@@ -268,8 +268,8 @@ def set_certificate_attrs(entry_attrs):
     cert = x509.normalize_certificate(cert)
     cert = x509.load_certificate(cert, datatype=x509.DER)
     entry_attrs['subject'] = unicode(DN(cert.subject))
-    entry_attrs['serial_number'] = unicode(cert.serial)
-    entry_attrs['serial_number_hex'] = u'0x%X' % cert.serial
+    entry_attrs['serial_number'] = unicode(cert.serial_number)
+    entry_attrs['serial_number_hex'] = u'0x%X' % cert.serial_number
     entry_attrs['issuer'] = unicode(DN(cert.issuer))
     entry_attrs['valid_not_before'] = x509.format_datetime(
             cert.not_valid_before)
diff --git a/ipatests/test_ipalib/test_x509.py b/ipatests/test_ipalib/test_x509.py
index a3e6cda..1ccf99d 100644
--- a/ipatests/test_ipalib/test_x509.py
+++ b/ipatests/test_ipalib/test_x509.py
@@ -116,6 +116,6 @@ def test_3_cert_contents(self):
 
         assert DN(cert.subject) == DN(('CN', 'ipa.example.com'), ('O', 'IPA'))
         assert DN(cert.issuer) == DN(('CN', 'IPA Test Certificate Authority'))
-        assert cert.serial == 1093
+        assert cert.serial_number == 1093
         assert cert.not_valid_before == not_before
         assert cert.not_valid_after == not_after
diff --git a/ipatests/test_ipaserver/test_ldap.py b/ipatests/test_ipaserver/test_ldap.py
index 1ea9959..13604db 100644
--- a/ipatests/test_ipaserver/test_ldap.py
+++ b/ipatests/test_ipaserver/test_ldap.py
@@ -80,7 +80,7 @@ def test_GSSAPI(self):
         entry_attrs = self.conn.get_entry(self.dn, ['usercertificate'])
         cert = entry_attrs.get('usercertificate')
         cert = cert[0]
-        serial = x509.load_certificate(cert, x509.DER).serial
+        serial = x509.load_certificate(cert, x509.DER).serial_number
         assert serial is not None
 
     def test_simple(self):
@@ -99,7 +99,7 @@ def test_simple(self):
         entry_attrs = self.conn.get_entry(self.dn, ['usercertificate'])
         cert = entry_attrs.get('usercertificate')
         cert = cert[0]
-        serial = x509.load_certificate(cert, x509.DER).serial
+        serial = x509.load_certificate(cert, x509.DER).serial_number
         assert serial is not None
 
     def test_Backend(self):
@@ -127,7 +127,7 @@ def test_Backend(self):
         entry_attrs = result['result']
         cert = entry_attrs.get('usercertificate')
         cert = cert[0]
-        serial = x509.load_certificate(cert, x509.DER).serial
+        serial = x509.load_certificate(cert, x509.DER).serial_number
         assert serial is not None
 
     def test_autobind(self):
@@ -143,7 +143,7 @@ def test_autobind(self):
         entry_attrs = self.conn.get_entry(self.dn, ['usercertificate'])
         cert = entry_attrs.get('usercertificate')
         cert = cert[0]
-        serial = x509.load_certificate(cert, x509.DER).serial
+        serial = x509.load_certificate(cert, x509.DER).serial_number
         assert serial is not None
 
 

From dc42e96edcf46f8311396cc4f8d55026666ece07 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Fri, 10 Feb 2017 14:31:10 +0100
Subject: [PATCH 3/4] Stable _is_null check

Avoid comparison of bytes with int in _is_null() check. b'' == 0
triggers a BytesWarning.

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipalib/parameters.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index 073e138..7fbe63e 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -123,7 +123,13 @@
 
 
 def _is_null(value):
-    return not value and value != 0 # NOTE: False == 0
+    if value:
+        return False
+    elif isinstance(value, six.integer_types + (float, decimal.Decimal)):
+        # 0 is not NULL
+        return False
+    else:
+        return True
 
 if six.PY3:
     unicode = str

From a071eb499451540ee3e5feb3eb9da74bdefaff5c Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Fri, 10 Feb 2017 14:32:17 +0100
Subject: [PATCH 4/4] test_StrEnum: use int as bad type

bytes triggers a BytesWarning error.

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipatests/test_ipalib/test_parameters.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ipatests/test_ipalib/test_parameters.py b/ipatests/test_ipalib/test_parameters.py
index d0bab40..0a69f91 100644
--- a/ipatests/test_ipalib/test_parameters.py
+++ b/ipatests/test_ipalib/test_parameters.py
@@ -1154,8 +1154,8 @@ class test_StrEnum(EnumChecker):
     _name = 'my_strenum'
     _datatype = unicode
     _test_values = u'Hello', u'naughty', u'nurse!'
-    _bad_type_values = u'Hello', b'naughty', u'nurse!'
-    _bad_type = bytes
+    _bad_type_values = u'Hello', 1, u'nurse!'
+    _bad_type = int
     _translation = u"values='Hello', 'naughty', 'nurse!'"
     _bad_values = u'Howdy', u'quiet', u'library!'
     _single_value_translation = u"value='Hello'"
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to