[pypy-commit] pypy cffi_dlopen_unicode: add failing test

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: cffi_dlopen_unicode
Changeset: r95181:ec14b51b9207
Date: 2018-10-07 13:03 +0300
http://bitbucket.org/pypy/pypy/changeset/ec14b51b9207/

Log:add failing test

diff --git a/pypy/module/_cffi_backend/test/test_re_python.py 
b/pypy/module/_cffi_backend/test/test_re_python.py
--- a/pypy/module/_cffi_backend/test/test_re_python.py
+++ b/pypy/module/_cffi_backend/test/test_re_python.py
@@ -1,4 +1,5 @@
 import py
+import shutil
 from rpython.tool.udir import udir
 from pypy.interpreter.gateway import interp2app
 from pypy.module._cffi_backend.newtype import _clean_cache
@@ -40,6 +41,9 @@
 'globalconst42', 'globalconsthello'])
 outputfilename = ffiplatform.compile(str(tmpdir), ext)
 cls.w_extmod = space.wrap(outputfilename)
+outputfileUname = unicode(udir.join(u'load\u03betest.dll'))
+shutil.copyfile(outputfilename, outputfileUname)
+cls.w_extmodU = space.wrap(outputfileUname)
 #mod.tmpdir = tmpdir
 #
 ffi = FFI()
@@ -108,6 +112,13 @@
 assert lib.add42(-10) == 32
 assert type(lib.add42) is _cffi_backend.FFI.CData
 
+def test_dlopen_unicode(self):
+import _cffi_backend
+self.fix_path()
+from re_python_pysrc import ffi
+lib = ffi.dlopen(self.extmodU)
+assert lib.add42(-10) == 32
+
 def test_dlclose(self):
 import _cffi_backend
 self.fix_path()
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: add, test LoadLibraryW, LoadLibraryEx{A, W}

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r95180:80ba133be397
Date: 2018-10-07 12:10 +0300
http://bitbucket.org/pypy/pypy/changeset/80ba133be397/

Log:add, test LoadLibraryW, LoadLibraryEx{A, W}

diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -113,6 +113,7 @@
MB_ERR_INVALID_CHARS ERROR_NO_UNICODE_TRANSLATION
WC_NO_BEST_FIT_CHARS STD_INPUT_HANDLE STD_OUTPUT_HANDLE
STD_ERROR_HANDLE HANDLE_FLAG_INHERIT FILE_TYPE_CHAR
+   LOAD_WITH_ALTERED_SEARCH_PATH
 """
 from rpython.translator.platform import host_factory
 static_platform = host_factory()
@@ -195,6 +196,22 @@
 GetModuleHandle = winexternal('GetModuleHandleA', [rffi.CCHARP], HMODULE)
 LoadLibrary = winexternal('LoadLibraryA', [rffi.CCHARP], HMODULE,
   save_err=rffi.RFFI_SAVE_LASTERROR)
+def wrap_loadlibraryex(func):
+def loadlibrary(name, handle=None, 
flags=LOAD_WITH_ALTERED_SEARCH_PATH):
+# Requires a full path name with '/' -> '\\'
+return func(name, handle, flags)
+return loadlibrary
+
+_LoadLibraryExA = winexternal('LoadLibraryExA',
+[rffi.CCHARP, HANDLE, DWORD], HMODULE,
+save_err=rffi.RFFI_SAVE_LASTERROR)
+LoadLibraryExA = wrap_loadlibraryex(_LoadLibraryExA)
+LoadLibraryW = winexternal('LoadLibraryW', [rffi.CWCHARP], HMODULE,
+  save_err=rffi.RFFI_SAVE_LASTERROR)
+_LoadLibraryExW = winexternal('LoadLibraryExW',
+[rffi.CWCHARP, HANDLE, DWORD], HMODULE,
+save_err=rffi.RFFI_SAVE_LASTERROR)
+LoadLibraryExW = wrap_loadlibraryex(_LoadLibraryExW)
 GetProcAddress = winexternal('GetProcAddress',
  [HMODULE, rffi.CCHARP],
  rffi.VOIDP)
diff --git a/rpython/rlib/test/loadtest/loadtest0.dll 
b/rpython/rlib/test/loadtest/loadtest0.dll
new file mode 100644
index 
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9bdcc33a1902f8e989d349c49c2cc08e633aa32b
GIT binary patch

[cut]
diff --git a/rpython/rlib/test/loadtest/loadtest1.dll 
b/rpython/rlib/test/loadtest/loadtest1.dll
new file mode 100644
index 
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..cb83854875c876717371bdf90488eed9c6571f03
GIT binary patch

[cut]

diff --git a/rpython/rlib/test/test_rwin32.py b/rpython/rlib/test/test_rwin32.py
--- a/rpython/rlib/test/test_rwin32.py
+++ b/rpython/rlib/test/test_rwin32.py
@@ -6,6 +6,44 @@
 from rpython.rlib import rwin32
 from rpython.tool.udir import udir
 
+loadtest_dir = os.path.dirname(__file__) + '/loadtest'
+test1 = os.path.abspath(loadtest_dir + '/loadtest1.dll')
+test0 = os.path.abspath(loadtest_dir + '/loadtest0.dll')
+
+if not os.path.exists(test1) or not os.path.exists(test0):
+# This is how the files, which are checked into the repo, were created
+from rpython.translator.tool.cbuild import ExternalCompilationInfo
+from rpython.translator.platform import platform
+from rpython.translator import cdir
+if not os.path.exists(loadtest_dir):
+os.mkdir(loadtest_dir)
+c_file = udir.ensure("test_rwin32", dir=1).join("test0.c")
+c_file.write(py.code.Source('''
+#include "src/precommondefs.h"
+RPY_EXPORTED
+int internal_sum(int a, int b) {
+return a + b;
+}
+'''))
+eci = ExternalCompilationInfo(include_dirs=[cdir])
+lib_name = str(platform.compile([c_file], eci, test0[:-4],
+   standalone=False))
+assert os.path.abspath(lib_name) == os.path.abspath(test0)
+
+c_file = udir.ensure("test_rwin32", dir=1).join("test1.c")
+c_file.write(py.code.Source('''
+#include "src/precommondefs.h"
+int internal_sum(int a, int b);
+RPY_EXPORTED
+int sum(int a, int b) {
+return internal_sum(a, b);
+}
+'''))
+eci = ExternalCompilationInfo(include_dirs=[cdir], 
+libraries=[loadtest_dir + '/loadtest0'])
+lib_name = str(platform.compile([c_file], eci, test1[:-4],
+   standalone=False, ))
+assert os.path.abspath(lib_name) == os.path.abspath(test1)
 
 def test_get_osfhandle():
 fid = open(str(udir.join('validate_test.txt')), 'w')
@@ -28,13 +66,13 @@
  "import time;"
  "time.sleep(10)",
  ],
-) 
+)
 print proc.pid
 handle = rwin32.OpenProcess(rwin32.PROCESS_ALL_ACCESS, False, proc.pid)
 assert rwin32.TerminateProcess(handle, signal.SIGTERM) == 1
 rwin32.CloseHandle(handle)
 assert proc.wait() == signal.SIGTERM
- 
+
 @py.test.mark.dont_track_allocations('putenv intentionally keeps strings 
alive')
 def test_wenviron():
 name, value = u'PYPY_TEST_日本', u'foobar日本'

[pypy-commit] pypy default: Use LoadLibraryEx in loading extension modules, like CPython's _PyImport_FindSharedFuncptr

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: 
Changeset: r95182:8894e5f1d5c0
Date: 2018-10-07 14:20 +0300
http://bitbucket.org/pypy/pypy/changeset/8894e5f1d5c0/

Log:Use LoadLibraryEx in loading extension modules, like CPython's
_PyImport_FindSharedFuncptr

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -92,8 +92,11 @@
 
 if sys.platform == 'win32':
 dash = '_'
+WIN32 = True
 else:
 dash = ''
+WIN32 = False
+
 
 def fclose(fp):
 try:
@@ -1656,7 +1659,11 @@
 try:
 ll_libname = rffi.str2charp(path)
 try:
-dll = rdynload.dlopen(ll_libname, space.sys.dlopenflags)
+if WIN32:
+# Allow other DLLs in the same directory with "path"
+dll = rdynload.dlopenex(ll_libname)
+else:
+dll = rdynload.dlopen(ll_libname, space.sys.dlopenflags)
 finally:
 lltype.free(ll_libname, flavor='raw')
 except rdynload.DLOpenError as e:
diff --git a/rpython/rlib/rdynload.py b/rpython/rlib/rdynload.py
--- a/rpython/rlib/rdynload.py
+++ b/rpython/rlib/rdynload.py
@@ -233,6 +233,15 @@
 raise DLOpenError(ustr.encode('utf-8'))
 return res
 
+def dlopenex(name):
+res = rwin32.LoadLibraryExA(name)
+if not res:
+err = rwin32.GetLastError_saved()
+ustr = rwin32.FormatErrorW(err)
+# DLOpenError unicode msg breaks translation of cpyext 
create_extension_module
+raise DLOpenError(ustr.encode('utf-8'))
+return res
+
 def dlclose(handle):
 res = rwin32.FreeLibrary(handle)
 if res:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy cffi_dlopen_unicode: allow unicode filename in W_DlOpenLibObject

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: cffi_dlopen_unicode
Changeset: r95183:9093671404b4
Date: 2018-10-07 16:30 +0300
http://bitbucket.org/pypy/pypy/changeset/9093671404b4/

Log:allow unicode filename in W_DlOpenLibObject

diff --git a/pypy/module/_cffi_backend/cdlopen.py 
b/pypy/module/_cffi_backend/cdlopen.py
--- a/pypy/module/_cffi_backend/cdlopen.py
+++ b/pypy/module/_cffi_backend/cdlopen.py
@@ -1,3 +1,4 @@
+import sys
 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
 from rpython.rlib.objectmodel import specialize, we_are_translated
 from rpython.rlib.rdynload import DLLHANDLE, dlopen, dlsym, dlclose, 
DLOpenError
@@ -12,20 +13,36 @@
 from pypy.module._cffi_backend.lib_obj import W_LibObject
 from pypy.module._cffi_backend import cffi_opcode, cffi1_module
 
+if sys.platform == 'win32':
+from rpython.rlib.rdynload import dlopenU
+WIN32 = True
+else:
+WIN32 = False
 
 class W_DlOpenLibObject(W_LibObject):
 
-def __init__(self, ffi, filename, flags):
-with rffi.scoped_str2charp(filename) as ll_libname:
-if filename is None:
-filename = ""
+def __init__(self, ffi, w_filename, flags):
+space = ffi.space
+_scoped = rffi.scoped_str2charp
+_dlopen = dlopen
+if WIN32 and space.isinstance_w(w_filename, space.w_unicode):
+_scoped = rffi.scoped_unicode2wcharp
+_dlopen = dlopenU
+fname = space.unicode_w(w_filename)
+elif space.is_none(w_filename):
+fname = None
+else:
+fname = space.text_w(w_filename)
+with _scoped(fname) as ll_libname:
+if fname is None:
+fname = ""
 try:
-handle = dlopen(ll_libname, flags)
+handle = _dlopen(ll_libname, flags)
 except DLOpenError as e:
-raise wrap_dlopenerror(ffi.space, e, filename)
-W_LibObject.__init__(self, ffi, filename)
+raise wrap_dlopenerror(space, e, fname)
+W_LibObject.__init__(self, ffi, fname)
 self.libhandle = handle
-self.register_finalizer(ffi.space)
+self.register_finalizer(space)
 
 def _finalize_(self):
 h = self.libhandle
diff --git a/pypy/module/_cffi_backend/ffi_obj.py 
b/pypy/module/_cffi_backend/ffi_obj.py
--- a/pypy/module/_cffi_backend/ffi_obj.py
+++ b/pypy/module/_cffi_backend/ffi_obj.py
@@ -572,8 +572,8 @@
 return self.ffi_type(w_arg, ACCEPT_STRING | ACCEPT_CDATA)
 
 
-@unwrap_spec(filename="fsencode_or_none", flags=int)
-def descr_dlopen(self, filename, flags=0):
+@unwrap_spec(flags=int)
+def descr_dlopen(self, w_filename, flags=0):
 """\
 Load and return a dynamic library identified by 'name'.  The standard
 C library can be loaded by passing None.
@@ -584,7 +584,7 @@
 first access."""
 #
 from pypy.module._cffi_backend import cdlopen
-return cdlopen.W_DlOpenLibObject(self, filename, flags)
+return cdlopen.W_DlOpenLibObject(self, w_filename, flags)
 
 
 def descr_dlclose(self, w_lib):
diff --git a/rpython/rlib/rdynload.py b/rpython/rlib/rdynload.py
--- a/rpython/rlib/rdynload.py
+++ b/rpython/rlib/rdynload.py
@@ -233,6 +233,16 @@
 raise DLOpenError(ustr.encode('utf-8'))
 return res
 
+def dlopenU(name, mode=-1):
+# mode is unused on windows, but a consistant signature
+res = rwin32.LoadLibraryW(name)
+if not res:
+err = rwin32.GetLastError_saved()
+ustr = rwin32.FormatErrorW(err)
+# DLOpenError unicode msg breaks translation of cpyext 
create_extension_module
+raise DLOpenError(ustr.encode('utf-8'))
+return res
+
 def dlclose(handle):
 res = rwin32.FreeLibrary(handle)
 if res:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy py3.5: #2900 fix in the _ssl module with poll()

2018-10-07 Thread arigo
Author: Armin Rigo 
Branch: py3.5
Changeset: r95184:000e19254270
Date: 2018-10-07 16:39 +0200
http://bitbucket.org/pypy/pypy/changeset/000e19254270/

Log:#2900 fix in the _ssl module with poll()

diff --git a/lib_pypy/_cffi_ssl/_stdssl/__init__.py 
b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
--- a/lib_pypy/_cffi_ssl/_stdssl/__init__.py
+++ b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
@@ -186,7 +186,7 @@
 # Prefer poll, if available, since you can poll() any fd
 # which can't be done with select().
 if HAVE_POLL:
-p.register(sock.fileno(), POLLOUT | POLLIN)
+p.register(sock.fileno(), POLLOUT if writing else POLLIN)
 
 rc = len(p.poll(timeout * 1000.0))
 else:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy cffi_dlopen_unicode: rework to fix win32 translation

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: cffi_dlopen_unicode
Changeset: r95185:7013b39caaa8
Date: 2018-10-07 20:33 +0300
http://bitbucket.org/pypy/pypy/changeset/7013b39caaa8/

Log:rework to fix win32 translation

diff --git a/pypy/module/_cffi_backend/cdlopen.py 
b/pypy/module/_cffi_backend/cdlopen.py
--- a/pypy/module/_cffi_backend/cdlopen.py
+++ b/pypy/module/_cffi_backend/cdlopen.py
@@ -23,23 +23,26 @@
 
 def __init__(self, ffi, w_filename, flags):
 space = ffi.space
-_scoped = rffi.scoped_str2charp
-_dlopen = dlopen
 if WIN32 and space.isinstance_w(w_filename, space.w_unicode):
-_scoped = rffi.scoped_unicode2wcharp
-_dlopen = dlopenU
 fname = space.unicode_w(w_filename)
-elif space.is_none(w_filename):
-fname = None
+with rffi.scoped_unicode2wcharp(fname) as ll_libname:
+fname = fname.encode('utf-8')
+try:
+handle = dlopenU(ll_libname, flags)
+except DLOpenError as e:
+raise wrap_dlopenerror(space, e, fname)
 else:
-fname = space.text_w(w_filename)
-with _scoped(fname) as ll_libname:
-if fname is None:
-fname = ""
-try:
-handle = _dlopen(ll_libname, flags)
-except DLOpenError as e:
-raise wrap_dlopenerror(space, e, fname)
+if space.is_none(w_filename):
+fname = None
+else:
+fname = space.text_w(w_filename)
+with rffi.scoped_str2charp(fname) as ll_libname:
+if fname is None:
+fname = ""
+try:
+handle = dlopen(ll_libname, flags)
+except DLOpenError as e:
+raise wrap_dlopenerror(space, e, fname)
 W_LibObject.__init__(self, ffi, fname)
 self.libhandle = handle
 self.register_finalizer(space)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy cffi_dlopen_unicode: fix for non-win32

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: cffi_dlopen_unicode
Changeset: r95186:da2aa8584393
Date: 2018-10-07 20:56 +0300
http://bitbucket.org/pypy/pypy/changeset/da2aa8584393/

Log:fix for non-win32

diff --git a/pypy/module/_cffi_backend/cdlopen.py 
b/pypy/module/_cffi_backend/cdlopen.py
--- a/pypy/module/_cffi_backend/cdlopen.py
+++ b/pypy/module/_cffi_backend/cdlopen.py
@@ -34,6 +34,8 @@
 else:
 if space.is_none(w_filename):
 fname = None
+elif space.isinstance_w(w_filename, space.w_unicode):
+fname = space.unicode_w(w_filename).encode('utf-8')
 else:
 fname = space.text_w(w_filename)
 with rffi.scoped_str2charp(fname) as ll_libname:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy cffi_dlopen_unicode: copy unicode handling to _cffi_backend.libraryobj.W_Library

2018-10-07 Thread mattip
Author: Matti Picus 
Branch: cffi_dlopen_unicode
Changeset: r95187:295154c77400
Date: 2018-10-08 09:36 +0300
http://bitbucket.org/pypy/pypy/changeset/295154c77400/

Log:copy unicode handling to _cffi_backend.libraryobj.W_Library

diff --git a/pypy/module/_cffi_backend/libraryobj.py 
b/pypy/module/_cffi_backend/libraryobj.py
--- a/pypy/module/_cffi_backend/libraryobj.py
+++ b/pypy/module/_cffi_backend/libraryobj.py
@@ -1,4 +1,5 @@
 from __future__ import with_statement
+import sys
 
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.error import oefmt
@@ -12,20 +13,42 @@
 from pypy.module._cffi_backend.cdataobj import W_CData
 from pypy.module._cffi_backend.ctypeobj import W_CType
 
+if sys.platform == 'win32':
+from rpython.rlib.rdynload import dlopenU
+WIN32 = True
+else:
+WIN32 = False
+
 
 class W_Library(W_Root):
 _immutable_ = True
 
-def __init__(self, space, filename, flags):
+def __init__(self, space, w_filename, flags):
 self.space = space
-with rffi.scoped_str2charp(filename) as ll_libname:
-if filename is None:
-filename = ""
-try:
-self.handle = dlopen(ll_libname, flags)
-except DLOpenError as e:
-raise wrap_dlopenerror(space, e, filename)
-self.name = filename
+if WIN32 and space.isinstance_w(w_filename, space.w_unicode):
+fname = space.unicode_w(w_filename)
+with rffi.scoped_unicode2wcharp(fname) as ll_libname:
+fname = fname.encode('utf-8')
+try:
+handle = dlopenU(ll_libname, flags)
+except DLOpenError as e:
+raise wrap_dlopenerror(space, e, fname)
+else:
+if space.is_none(w_filename):
+fname = None
+elif space.isinstance_w(w_filename, space.w_unicode):
+fname = space.unicode_w(w_filename).encode('utf-8')
+else:
+fname = space.text_w(w_filename)
+with rffi.scoped_str2charp(fname) as ll_libname:
+if fname is None:
+fname = ""
+try:
+handle = dlopen(ll_libname, flags)
+except DLOpenError as e:
+raise wrap_dlopenerror(space, e, fname)
+self.handle = handle
+self.name = fname
 self.register_finalizer(space)
 
 def _finalize_(self):
@@ -104,7 +127,7 @@
 W_Library.typedef.acceptable_as_base_class = False
 
 
-@unwrap_spec(filename="fsencode_or_none", flags=int)
-def load_library(space, filename, flags=0):
-lib = W_Library(space, filename, flags)
+@unwrap_spec(flags=int)
+def load_library(space, w_filename, flags=0):
+lib = W_Library(space, w_filename, flags)
 return lib
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit