summarylogtreecommitdiffstats
path: root/python-milksnake-0.1.5-distutils.patch
blob: 38a13295c72f4417306d54c78ede364411bbc9fc (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
From f7f80752e7b2c0a343a69abb01a3c5be11fd6162 Mon Sep 17 00:00:00 2001
From: messense <messense@icloud.com>
Date: Tue, 22 Jun 2021 13:39:45 +0800
Subject: [PATCH 1/5] Patch distutils build command instead

This removes the empty C extension trick.
---
 .gitignore                  |  1 +
 milksnake/setuptools_ext.py | 77 ++++++++++++-------------------------
 setup.py                    |  3 --
 tests/conftest.py           |  8 +++-
 tests/test_basic.py         |  4 +-
 5 files changed, 34 insertions(+), 59 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5e6e4be..b846d5f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@ build
 __pycache__
 .eggs
 .pytest_cache
+tests/res/
diff --git a/milksnake/setuptools_ext.py b/milksnake/setuptools_ext.py
index 88cc549..bec6e80 100644
--- a/milksnake/setuptools_ext.py
+++ b/milksnake/setuptools_ext.py
@@ -1,19 +1,14 @@
 import os
 import sys
-import uuid
 import shutil
-import tempfile
 import subprocess
 
 from distutils import log
-from distutils.core import Extension
-from distutils.ccompiler import new_compiler
 from distutils.command.build_py import build_py
 from distutils.command.build_ext import build_ext
 
 from cffi import FFI
 from cffi import recompiler as cffi_recompiler
-from cffi import setuptools_ext as cffi_ste
 
 try:
     from wheel.bdist_wheel import bdist_wheel
@@ -21,16 +16,6 @@
     bdist_wheel = None
 
 here = os.path.abspath(os.path.dirname(__file__))
-EMPTY_C = u'''
-void init%(mod)s(void) {}
-void PyInit_%(mod)s(void) {}
-'''
-
-BUILD_PY = u'''
-import cffi
-from milksnake.ffi import make_ffi
-ffi = make_ffi(**%(kwargs)r)
-'''
 
 MODULE_PY = u'''# auto-generated file
 __all__ = ['lib', 'ffi']
@@ -88,24 +73,6 @@ def run(self):
                     func(base_path=base_path, inplace=False)
 
         class MilksnakeBuildExt(base_build_ext):
-            def get_ext_fullpath(self, ext_name):
-                milksnake_dummy_ext = None
-                for ext in spec.dist.ext_modules:
-                    if ext.name == ext_name:
-                        milksnake_dummy_ext = getattr(
-                            ext, 'milksnake_dummy_ext', None)
-                        break
-
-                if milksnake_dummy_ext is None:
-                    return base_build_ext.get_ext_fullpath(self, ext_name)
-
-                fullname = self.get_ext_fullname(ext_name)
-                modpath = fullname.split('.')
-                package = '.'.join(modpath[0:-1])
-                build_py = self.get_finalized_command('build_py')
-                package_dir = os.path.abspath(build_py.get_package_dir(package))
-                return os.path.join(package_dir, milksnake_dummy_ext)
-
             def run(self):
                 base_build_ext.run(self)
                 if self.inplace:
@@ -216,7 +183,6 @@ def __init__(self, spec, module_path, dylib=None, header_filename=None,
 
         genbase = '%s._%s' % (parts[0], parts[1].lstrip('_'))
         self.cffi_module_path = '%s__ffi' % genbase
-        self.fake_module_path = '%s__lib' % genbase
 
         from distutils.sysconfig import get_config_var
         self.lib_filename = '%s__lib%s' % (
@@ -237,27 +203,9 @@ def get_header_source(self):
 
     def prepare_build(self):
         dist = self.spec.dist
-
-        # Because distutils was never intended to support other languages and
-        # this was never cleaned up, we need to generate a fake C module which
-        # we later override with our rust module.  This means we just compile
-        # an empty .c file into a Python module.  This will trick wheel and
-        # other systems into assuming our library has binary extensions.
         if dist.ext_modules is None:
             dist.ext_modules = []
 
-        build = dist.get_command_obj('build')
-        build.ensure_finalized()
-        empty_c_path = os.path.join(build.build_temp, 'empty.c')
-        if not os.path.isdir(build.build_temp):
-            os.makedirs(build.build_temp)
-        with open(empty_c_path, 'w') as f:
-            f.write(EMPTY_C % {'mod': self.fake_module_path.split('.')[-1]})
-
-        ext = Extension(self.fake_module_path, sources=[empty_c_path])
-        ext.milksnake_dummy_ext = self.lib_filename
-        dist.ext_modules.append(ext)
-
         def make_ffi():
             from milksnake.ffi import make_ffi
             return make_ffi(self.module_path,
@@ -319,8 +267,33 @@ def get_tag(self):
     dist.cmdclass['bdist_wheel'] = MilksnakeBdistWheel
 
 
+def patch_distutils_build(dist):
+    """Trick wheel and other systems into assuming
+    our library has binary extensions
+
+    See also https://github.com/pypa/distutils/pull/43
+    """
+    from distutils.command import build as _build
+
+    class build(_build.build, object):
+        def finalize_options(self):
+            super(build, self).finalize_options()
+            if self.distribution.has_ext_modules():
+                self.build_lib = self.build_platlib
+            else:
+                self.build_lib = self.build_purelib
+
+    _build.build = build
+
+    orig_has_ext_modules = dist.has_ext_modules
+    dist.has_ext_modules = lambda: (
+        orig_has_ext_modules() or bool(dist.milksnake_tasks)
+    )
+
+
 def milksnake_tasks(dist, attr, value):
     """Registers task callbacks."""
+    patch_distutils_build(dist)
     patch_universal_wheel(dist)
 
     spec = Spec(dist)
diff --git a/setup.py b/setup.py
index 39b3d79..8a73b5b 100644
--- a/setup.py
+++ b/setup.py
@@ -11,9 +11,6 @@
     author_email='armin.ronacher@active-4.com',
     license='Apache License 2.0',
     packages=['milksnake'],
-    package_data={
-        'milksnake': ['empty.c'],
-    },
     description='A python library that extends setuptools for binary extensions.',
     long_description=readme,
     long_description_content_type='text/markdown',
diff --git a/tests/conftest.py b/tests/conftest.py
index 57e4411..005c53b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,4 +1,5 @@
 import os
+import sys
 import uuid
 import json
 import atexit
@@ -28,7 +29,10 @@ def __init__(self, path):
         self.path = path
 
     def spawn(self, executable, args=None, **kwargs):
-        return subprocess.Popen([os.path.join(self.path, 'bin', executable)] +
+        bin_dir = 'bin'
+        if sys.platform == 'win32' and not executable.endswith('.exe'):
+            bin_dir = 'Scripts'
+        return subprocess.Popen([os.path.join(self.path, bin_dir, executable)] +
                                 list(args or ()), **kwargs)
 
     def run(self, executable, args=None):
@@ -62,7 +66,7 @@ def _remove():
     subprocess.Popen(['virtualenv', path]).wait()
     try:
         venv = VirtualEnv(path)
-        venv.run('pip', ['install', '--editable', root])
+        venv.run('python', ['-m', 'pip', 'install', '--editable', root])
         yield venv
     finally:
         _remove()
diff --git a/tests/test_basic.py b/tests/test_basic.py
index 5ebf511..406d8e0 100644
--- a/tests/test_basic.py
+++ b/tests/test_basic.py
@@ -4,7 +4,7 @@
 def test_example_dev_run(virtualenv):
     pkg = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                        'res', 'minimal'))
-    virtualenv.run('pip', ['install', '-v', '--editable', pkg])
+    virtualenv.run('python', ['-m', 'pip', 'install', '-v', '--editable', pkg])
     virtualenv.eval('''if 1:
         from example import test
         def execute():
@@ -15,7 +15,7 @@ def execute():
 def test_example_nested_dev_run(virtualenv):
     pkg = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                        'res', 'nested'))
-    virtualenv.run('pip', ['install', '-v', '--editable', pkg])
+    virtualenv.run('python', ['-m', 'pip', 'install', '-v', '--editable', pkg])
     virtualenv.eval('''if 1:
         from example.nested import test
         def execute():

From 63a96270e1cbebc3340367b15f4ecbfd312aa212 Mon Sep 17 00:00:00 2001
From: messense <messense@icloud.com>
Date: Tue, 22 Jun 2021 15:25:07 +0800
Subject: [PATCH 2/5] Use cbindgen 0.19 in example

---
 README.md               | 2 +-
 example/rust/Cargo.toml | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 0bc2936..97aa666 100644
--- a/README.md
+++ b/README.md
@@ -107,7 +107,7 @@ name = "example"
 crate-type = ["cdylib"]
 
 [build-dependencies]
-cbindgen = "0.4"
+cbindgen = "0.19"
 ```
 
 And finally the build.rs file:
diff --git a/example/rust/Cargo.toml b/example/rust/Cargo.toml
index 0504526..5cda692 100644
--- a/example/rust/Cargo.toml
+++ b/example/rust/Cargo.toml
@@ -9,4 +9,4 @@ name = "example"
 crate-type = ["cdylib"]
 
 [build-dependencies]
-cbindgen = "0.4"
+cbindgen = "0.19"

From cedd71b2d5541f01ced71880497b143498a2d035 Mon Sep 17 00:00:00 2001
From: messense <messense@icloud.com>
Date: Tue, 22 Jun 2021 15:28:20 +0800
Subject: [PATCH 3/5] Replace \r\n with \n to fix Windows error

---
 milksnake/ffi.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/milksnake/ffi.py b/milksnake/ffi.py
index de09ef6..0cb6c02 100644
--- a/milksnake/ffi.py
+++ b/milksnake/ffi.py
@@ -14,6 +14,7 @@ def make_ffi(module_path, header, strip_directives=False):
         header = header.decode('utf-8')
     if strip_directives:
         header = _directive_re.sub('', header)
+    header = header.replace('\r\n', '\n')
 
     ffi = cffi.FFI()
     ffi.cdef(header)

From 980e70c6e2b7feff8add4d0ca334667e1b36f757 Mon Sep 17 00:00:00 2001
From: messense <messense@icloud.com>
Date: Tue, 22 Jun 2021 13:45:56 +0800
Subject: [PATCH 4/5] Add GitHub Actions configuration

---
 .github/workflows/CI.yml | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 .github/workflows/CI.yml

diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml
new file mode 100644
index 0000000..9b06aaf
--- /dev/null
+++ b/.github/workflows/CI.yml
@@ -0,0 +1,27 @@
+name: CI
+on: [push, pull_request]
+
+jobs:
+  test:
+    name: Test
+    runs-on: ${{ matrix.os }}
+    strategy:
+      matrix:
+        os: [ubuntu-latest, windows-latest, macos-latest]
+        python-version: ['2.7', '3.6', 'pypy-2.7', 'pypy-3.7']
+    steps:
+      - uses: actions/checkout@v2
+      - name: Set up Python
+        uses: actions/setup-python@v2
+        with:
+          python-version: ${{ matrix.python-version }}
+      - name: Install Rust toolchain
+        uses: actions-rs/toolchain@v1
+        with:
+          toolchain: stable
+          profile: minimal
+          override: true
+      - name: Install test dependencies
+        run: pip install pytest virtualenv
+      - name: pytest
+        run: pytest tests

From bb1a9e387bef18ef7baef1743944251762725a0f Mon Sep 17 00:00:00 2001
From: messense <messense@icloud.com>
Date: Tue, 22 Jun 2021 15:39:47 +0800
Subject: [PATCH 5/5] Fix module import error on Windows PyPy

```
Traceback (most recent call last):

  File "<string>", line 7, in <module>

  File "<stdin>", line 2, in <module>

  File "d:\a\milksnake\milksnake\tests\res\nested\example\nested\__init__.py", line 1, in <module>

    from . import _native

  File "d:\a\milksnake\milksnake\tests\res\nested\example\nested\_native.py", line 7, in <module>

    lib = ffi.dlopen(os.path.join(os.path.dirname(__file__), '_native__libNone'), 0)

OSError: Cannot load library 'd:\\a\\milksnake\\milksnake\\tests\\res\\nested\\example\\nested\\_native__libNone': The specified module could not be found
```
---
 milksnake/setuptools_ext.py | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/milksnake/setuptools_ext.py b/milksnake/setuptools_ext.py
index bec6e80..ce35c3a 100644
--- a/milksnake/setuptools_ext.py
+++ b/milksnake/setuptools_ext.py
@@ -185,10 +185,18 @@ def __init__(self, spec, module_path, dylib=None, header_filename=None,
         self.cffi_module_path = '%s__ffi' % genbase
 
         from distutils.sysconfig import get_config_var
-        self.lib_filename = '%s__lib%s' % (
-            genbase.split('.')[-1],
-            get_config_var('SHLIB_SUFFIX') or get_config_var('SO')
-        )
+
+        if sys.platform == 'darwin':
+            plat_ext = ".dylib"
+        elif sys.platform == 'win32':
+            plat_ext = ".dll"
+        else:
+            plat_ext = ".so"
+        ext = get_config_var('EXT_SUFFIX') or \
+            get_config_var('SHLIB_SUFFIX') or \
+            get_config_var('SO') or plat_ext
+
+        self.lib_filename = '%s__lib%s' % (genbase.split('.')[-1], ext)
 
     def get_header_source(self):
         if self.header_source is not None: