summaryrefslogtreecommitdiff
path: root/pipermail/pycrypto/2010q4/000338.html
blob: 4fffeeebf04737ed5f2be6c6efa14b0b535a6d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
 <HEAD>
   <TITLE> [pycrypto] Once again: Python3 with PyCrypto
   </TITLE>
   <LINK REL="Index" HREF="index.html" >
   <LINK REL="made" HREF="mailto:pycrypto%40lists.dlitz.net?Subject=%5Bpycrypto%5D%20Once%20again%3A%20Python3%20with%20PyCrypto&In-Reply-To=4D18ED63.2030909%40gmx.li">
   <META NAME="robots" CONTENT="index,nofollow">
   <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
   <LINK REL="Previous"  HREF="000329.html">
   <LINK REL="Next"  HREF="000343.html">
 </HEAD>
 <BODY BGCOLOR="#ffffff">
   <H1>[pycrypto] Once again: Python3 with PyCrypto</H1>
    <B>Thorsten Behrens</B> 
    <A HREF="mailto:pycrypto%40lists.dlitz.net?Subject=%5Bpycrypto%5D%20Once%20again%3A%20Python3%20with%20PyCrypto&In-Reply-To=4D18ED63.2030909%40gmx.li"
       TITLE="[pycrypto] Once again: Python3 with PyCrypto">sbehrens at gmx.li
       </A><BR>
    <I>Tue Dec 28 15:22:17 CST 2010</I>
    <P><UL>
        <LI>Previous message: <A HREF="000329.html">[pycrypto] Once again: Python3 with PyCrypto
</A></li>
        <LI>Next message: <A HREF="000343.html">[pycrypto] Once again: Python3 with PyCrypto
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#338">[ date ]</a>
              <a href="thread.html#338">[ thread ]</a>
              <a href="subject.html#338">[ subject ]</a>
              <a href="author.html#338">[ author ]</a>
         </LI>
       </UL>
    <HR>  
<!--beginarticle-->
<PRE>How about them apples.

D:\Users\Soenke\Documents\Source\pycrypto&gt;python --version
Python 3.1.3

D:\Users\Soenke\Documents\Source\pycrypto&gt;python setup.py test
running test
..................................................................................................................................
..................................................................................................................................
..................................................................................................................................
..................................................................................................................................
..................................................................................................................................
..................................................................................................................................
.............................................................................................................SelfTest: 
You can ign
ore the RandomPool_DeprecationWarning that follows.
build\lib.win-amd64-3.1\Crypto\Util\randpool.py:40: 
RandomPool_DeprecationWarning: This application uses RandomPool, which 
is BROK
EN in older releases.  See <A HREF="http://www.pycrypto.org/randpool-broken">http://www.pycrypto.org/randpool-broken</A>
   RandomPool_DeprecationWarning)
.....................
----------------------------------------------------------------------
Ran 910 tests in 28.723s

OK

Alright, allow me to bask in this for a second. Okay, I'm good.

This also still builds and tests correctly on Python 2.7, 2.2 and2.1.

I'll commit this to the py3k branch. It's not ready to be merged yet. 
Here's the TODO as I have it:

TODO:
- Check for type of string in functions and throw an error when it's not 
correct. While at it, ensure that functions observe the guidelines below 
re type.
   This is friendlier than just relying on Python's errors.
- Document the expected types for functions. The cipher, the key and the 
input texts are byte-strings. Plaintext decodes are byte-strings 
currently, this needs review.
   In keeping with how Python 3.x's hash functions work, the input MODE 
is a text string. hexdigest() returns a text string, and digest() 
returns a byte-string.
- Compile and test _fastmath.c
- Go through test cases and see which modules are not covered

Volunteers for any of those?

Here is a writeup of what was changed:

Py code:

setup.py invokes 2to3 automatically. This handles int/long and print 
issues, among others.
setup.py will touch nt.py on win32 after build and build again. This is 
necessary so 2to3 can do its magic on that file.

There are still a lot of places in the code that need manual attention 
even with 2to3. They mostly have to do with string (2.x) vs. 
byte/unicode (3.x) representation

Use &quot;if sys.version_info[0] is 2:&quot; where needed. Ideally, most of the 
conditional code can be in py3compat.

Replace str(x) with bstr(x) if bytes were intended. Becomes str(x) in 
2.x and bytes(x) in 3.x through py3compat module.
Replace chr(x) with bchr(x) if bytes were intended. Becomes chr(x) in 
2.x and bytes([x]) in 3.x through py3compat module.
Replace ord(x) with bord(x) if bytes were intended. Becomes ord(x) in 
2.x and x in 3.x through py3compat module.

Comparing a string index to a string literal needs to be changed in 3.x, 
as b'string'[0] returns an integer, not b's'.
The comparison can be fixed by indexing the right side, too: &quot;if 
s[0]==b('\x30')[0]:&quot; or &quot;if self.typeTag!=self.typeTags['SEQUENCE'][0]:&quot;

String literals need to be bytes if bytes were intended.
Replace &quot;x&quot; with b(&quot;x&quot;) if bytes were intended. Becomes &quot;x&quot; in 2.x, and 
s.encode(&quot;x&quot;,&quot;latin-1&quot;) in 3.x through py3compat module.
For example, '&quot;&quot;.join' is replaced by 'b(&quot;&quot;).join', and 's = &quot;&quot;' becomes 
's = b(&quot;&quot;)'.
Search for \x to find literals that may have been intended as byte strings
!! However, where a human-readable ASCII text string output was 
intended, such as in AllOrNothing.undigest(), leave as a string literal !!

Only load python_compat.py &quot;if sys.version_info[0] is 2 and 
sys.version_info[1] is 1:&quot; .
The assignment to True, False generates syntax errors in 3.x, and &gt;= 2.2 
don't need the compatibility code.

Where print is used with &gt;&gt; to redirect, use a separate function 
instead. See setup.py for an example

The string module has been changed in 3.x. It lost join and split, 
maketrans now expects bytes, and so on.
Replace string.join(a,b) with b.join(a).
Replace string.split(a) with a.split().
Replace body of white-space-stripping functions with 'return 
&quot;&quot;.join(s.split())'

Integer division via the &quot;/&quot; operator can return a float in 3.x. This 
causes issues in Util.number.getStrongPrime. As 2.1 does not support
the &quot;//&quot; operator, a helper function &quot;floordiv()&quot; is brought in via 'if 
sys.version_info' from floordiv.py or py21floordiv.py.


C code:

Extended &quot;pycrypto_compat.h&quot;. It handles #define's for Python 3.x 
forward compatibility

#include &quot;pycrypto_compat.h&quot;
// All other local includes after this, so they can benefit from the 
definitions in pycrypto_compat.h

The compat header #defines IS_PY3K if compiling on 3.x
The compat header #defines PyBytes_*, PyUnicode_*, PyBytesObject to 
resolve to their PyString* counterparts if compiling on 2.x.
PyLong_* can be dangerous depending on code construct (think an if that 
runs PyInt_* with else PyLong_*),
therefore it is #defined in each individual module if needed and safe to 
do so.

PyString_* has been replaced with PyBytes_* or PyUnicode_* depending on 
intent
PyStringObject has been replaced with PyBytesObject or PyUnicodeObject 
depending on intent.
PyInt_* has been replaced with PyLong_*, where safe to do (in all cases 
so far)

The C code uses &quot;#ifdef IS_PY3K&quot; liberally.
Code duplication has been avoided.

Module initialization and module structure differ significantly in 3.x. 
Conditionals take care of it.

myModuleType.ob_type assignment conditionally becomes PyTypeReady for 3.x.

getattr cannot be used to check against custom attributes in 3.x. For 
3.x, conditional code uses getattro
and PyUnicode_CompareWithASCIIString instead of strcmp

hexdigest() needed to be changed to return a Unicode object with 3.x

</PRE>




<!--endarticle-->
    <HR>
    <P><UL>
        <!--threads-->
	<LI>Previous message: <A HREF="000329.html">[pycrypto] Once again: Python3 with PyCrypto
</A></li>
	<LI>Next message: <A HREF="000343.html">[pycrypto] Once again: Python3 with PyCrypto
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#338">[ date ]</a>
              <a href="thread.html#338">[ thread ]</a>
              <a href="subject.html#338">[ subject ]</a>
              <a href="author.html#338">[ author ]</a>
         </LI>
       </UL>

<hr>
<a href="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">More information about the pycrypto
mailing list</a><br>
</body></html>