Custom importer and errors

2024-04-15 Thread Fabiano Sidler via Python-list

Hi folks!

I'd like to split my package tree into several IDE projects and build a 
custom

importer to import
    'top.child1.child2'
from the directory
    /top.child1.child2/__init__.py
so basically replacing the dots with slashes and having the package content
lying directly in the project folder. I have come up with this:

=== usercustomize.py ===
 1 import sys
 2 from importlib.machinery import ModuleSpec
 3 from pathlib import Path
 4
 5 Loader = type(__spec__.loader)
 6
 7 class IdeHelper:
 8 @classmethod
 9 def find_spec(cls, name, path, target=None):
10 for dirname in sys.path:
11 dirobj = Path(dirname)
12 if dirobj.name == name:
13 break
14 else:
15 return None
16 origin = str(dirobj.joinpath('__init__.py').absolute())
17 ret = ModuleSpec(name, Loader(name, origin), origin=origin)
18 return ret
19
20 sys.meta_path.append(IdeHelper)

which I'm on the right direction with. Unfortunately, I'm getting errors 
while

importing a subpackage. With 'import top.child1' the error is
    ModuleNotFoundError: No module named 'top.child1'; 'top' is not a 
package

whereas with 'from top import child1' the error changes to
    ImportError: cannot import name 'child1' from 'top' (unknown location)

How can I make this work?

Best wishes,
Fabiano

--
https://mail.python.org/mailman/listinfo/python-list


Re: TLSServer: certificate one request behind...

2018-03-18 Thread Fabiano Sidler
Hello? Rfd, anyone?

Thus wrote Fabiano Sidler:
> Thus wrote Fabiano Sidler:
> > What's the reason for this? Please find attached my TLSServer.
> 
> Oh, sorry...! Apparently, the attachment has been stripped. Here inline:
> 
> === tlsserver.py ===
> from socketserver import ThreadingTCPServer,StreamRequestHandler
> import ssl
> 
> class TLSServer(ThreadingTCPServer):
>   def __init__(self, *args, **kwargs):
>   super(TLSServer, self).__init__(*args, **kwargs)
>   ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
>   ctx.set_servername_callback(self.servername_callback)
>   ctx.check_hostname = False
>   self._ctx = ctx
>   def get_request(self):
>   s,a = super(TLSServer, self).get_request()
>   s = self._ctx.wrap_socket(s, server_side=True)
>   return s,a
>   def servername_callback(self, sock, req_hostname, cb_context):
>   return ssl.ALERT_DESCRIPTION_INTERNAL_ERROR
> 
> 
> from OpenSSL import crypto as x509
> from tempfile import NamedTemporaryFile
> 
> class SelfSigningServer(TLSServer):
>   def servername_callback(self, sock, req_hostname, cb_context):
>   key = x509.PKey()
>   key.generate_key(x509.TYPE_RSA, 2048)
>   cert = x509.X509()
>   subj = cert.get_subject()
>   subj.C  = 'CH'
>   subj.ST = 'ZH'
>   subj.L  = 'Zurich'
>   subj.O  = 'ACME Inc.'
>   subj.OU = 'IT dept.'
>   subj.CN = req_hostname
>   cert.set_version(0x02)
>   cert.set_serial_number(1000)
>   cert.gmtime_adj_notBefore(0)
>   cert.gmtime_adj_notAfter(10*365*24*60*60)
>   cert.set_issuer(subj)
>   cert.set_pubkey(key)
>   cert.sign(key, 'sha256')
>   certfile = NamedTemporaryFile()
>   keyfile = NamedTemporaryFile()
>   certfile.write(x509.dump_certificate(x509.FILETYPE_PEM, cert))
>   keyfile.write(x509.dump_privatekey(x509.FILETYPE_PEM, key))
>   certfile.seek(0)
>   keyfile.seek(0)
>   cb_context.load_cert_chain(certfile=certfile.name, 
> keyfile=keyfile.name)
>   cb_context.set_servername_callback(self.servername_callback)
>   sock.context = cb_context
>   certfile.close()
>   keyfile.close()
> 
> class SelfSigningHandler(StreamRequestHandler):
>   def handle(self):
>   self.wfile.write(b'Hello World!\r\n')
> 
> server = SelfSigningServer(('localhost',1234), SelfSigningHandler)
> server.serve_forever()
> === tlsserver.py ===
> 
> Thanks again!
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TLSServer: certificate one request behind...

2018-03-14 Thread Fabiano Sidler
Thus wrote Fabiano Sidler:
> What's the reason for this? Please find attached my TLSServer.

Oh, sorry...! Apparently, the attachment has been stripped. Here inline:

=== tlsserver.py ===
from socketserver import ThreadingTCPServer,StreamRequestHandler
import ssl

class TLSServer(ThreadingTCPServer):
def __init__(self, *args, **kwargs):
super(TLSServer, self).__init__(*args, **kwargs)
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.set_servername_callback(self.servername_callback)
ctx.check_hostname = False
self._ctx = ctx
def get_request(self):
s,a = super(TLSServer, self).get_request()
s = self._ctx.wrap_socket(s, server_side=True)
return s,a
def servername_callback(self, sock, req_hostname, cb_context):
return ssl.ALERT_DESCRIPTION_INTERNAL_ERROR


from OpenSSL import crypto as x509
from tempfile import NamedTemporaryFile

class SelfSigningServer(TLSServer):
def servername_callback(self, sock, req_hostname, cb_context):
key = x509.PKey()
key.generate_key(x509.TYPE_RSA, 2048)
cert = x509.X509()
subj = cert.get_subject()
subj.C  = 'CH'
subj.ST = 'ZH'
subj.L  = 'Zurich'
subj.O  = 'ACME Inc.'
subj.OU = 'IT dept.'
subj.CN = req_hostname
cert.set_version(0x02)
cert.set_serial_number(1000)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(10*365*24*60*60)
cert.set_issuer(subj)
cert.set_pubkey(key)
cert.sign(key, 'sha256')
certfile = NamedTemporaryFile()
keyfile = NamedTemporaryFile()
certfile.write(x509.dump_certificate(x509.FILETYPE_PEM, cert))
keyfile.write(x509.dump_privatekey(x509.FILETYPE_PEM, key))
certfile.seek(0)
keyfile.seek(0)
cb_context.load_cert_chain(certfile=certfile.name, 
keyfile=keyfile.name)
cb_context.set_servername_callback(self.servername_callback)
sock.context = cb_context
certfile.close()
keyfile.close()

class SelfSigningHandler(StreamRequestHandler):
def handle(self):
self.wfile.write(b'Hello World!\r\n')

server = SelfSigningServer(('localhost',1234), SelfSigningHandler)
server.serve_forever()
=== tlsserver.py ===

Thanks again!
-- 
https://mail.python.org/mailman/listinfo/python-list


TLSServer: certificate one request behind...

2018-03-14 Thread Fabiano Sidler
Hi folks!

I have written a TLSServer for testing purposes that generates
self-signed certificates upon request. This works pretty well
except that the certificates are always supplied one request too
late:

# gets no cert, but a handshake failure instead
$ openssl s_client -connect localhost:1234 -servername server1

# gets the cert for server1
$ openssl s_client -connect localhost:1234 -servername server2

# gets the cert for server2
$ openssl s_client -connect localhost:1234 -servername server3

What's the reason for this? Please find attached my TLSServer.

Best wishes,
Fabiano
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do I say Is this a function?

2008-05-05 Thread Fabiano Sidler

John Henry schrieb:

  exec fct



You don't want this. You want to store the function in a list instead:

l = [ f1, f3, others ]
for i in [0,1]: l[i]()

Greetings,
Fabiano
--
http://mail.python.org/mailman/listinfo/python-list


getattrfunc vs. tp_methods

2006-11-03 Thread Fabiano Sidler
Hello list!

Writing a C extension module, which way is better to implement attribute
retrieval, by a getattr function in the PyTypeObject struct or by an entry
tp_methods? I don't need any black magic being executed on attribute access,
so I would tend towards the tp_methods entry. Any argument against it?

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing out the objects stack

2006-10-31 Thread Fabiano Sidler
On Sunday 29 October 2006 17:48, I wrote:
 Now the following things are not clear to me:
 -Why does the VM crash? Did I use the wrong stack boundaries?
 -Why are no locales printed?
 -Why is the function stack not right before or after foo
  on the stack? When I disassemble the code of f with dis.dis,
  it reveals that sys.stack and foo are pushed onto the stack
  successively.

No hints?

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Printing out the objects stack

2006-10-29 Thread Fabiano Sidler
Hi folks!

For getting a plan how a stack-based VM like Python works, I added a
function that prints out the current object stack. Unfortunately, it
crashes the VM. Could someone here take a look at it? What's wrong with
it?:

--- snip ---
static PyObject *
sys_stack(PyObject *self)
{
PyFrameObject *f = PyThreadState_GET()-frame;
PyObject
**i,
**begin = f-f_localsplus,
**end = f-f_valuestack;

end += f-f_code-co_stacksize;
flog(   co_name: %s\n
co_stacksize: %d\n
localsplus: %d\n
valuestack: %d\n,
PyString_AsString(f-f_code-co_name), f-f_code-co_stacksize,
f-f_localsplus, f-f_valuestack);
flog(locals:\n);
{
PyObject *list = f-f_code-co_names;
int len,i;

len = PyList_Size(list);
for (i=0; ilen; i++) {
PyObject *obi;
char *strval;
obi = PyList_GetItem(list, i);
if ((obi=PyObject_Str(obi))!=NULL) {
if ((strval=PyString_AsString(obi))!=NULL) {
flog([%d] %s\n, i, strval);
}
}
Py_DECREF(obi);
}
Py_DECREF(list);
}
flog(end of locals\n);
for (i=begin; i=end; i++) {
int o = ((int)f-f_valuestack - (int)i)/4;
PyObject *obi;
char *strval;

if (*i == NULL) {
flog(NULL\n);
break; }
if ((obi=PyObject_Str(*i)) != NULL) {
if ((strval=PyString_AsString(obi)) != NULL) {
flog([%3d] %s\n, o, strval);
}
}
}
finished:
Py_INCREF(Py_None);
return Py_None;
}
--- snap ---

flog(fmt, ...) is my function to log to a file, sys_stack I've made available
to Python as sys.stack and PyFrame_New I modified so that it nulls the memory
allocated for the objects stack. Now the following Python code crashes...

--- snip ---
def f(foo,bar,boo,far):
foobar='foobar'
print foobar
sys.stack()

f('foo','bar','boo','far') # CRASH
--- snap ---

...and in my logfile I have...

--- snip ---
co_name: f
co_stacksize: 1
localsplus: 136139316
valuestack: 136139336
locals:
end of locals
[  5] foo
[  4] bar
[  3] boo
[  2] far
[  1] foobar
[  0] built-in function stack
--- snap ---

Now the following things are not clear to me:
-Why does the VM crash? Did I use the wrong stack boundaries?
-Why are no locales printed?
-Why is the function stack not right before or after foo
 on the stack? When I disassemble the code of f with dis.dis,
 it reveals that sys.stack and foo are pushed onto the stack
 successively.

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Current stackdepth outside PyEval_EvalFrameEx

2006-10-25 Thread Fabiano Sidler
On Tuesday 24 October 2006 17:05, Neil Cerutti wrote:
 Perhaps the inspect module will help?  See 3.11.4 The Interpreter
 Stack.

No, sorry if I didn't eplain it well enough. I meant the object stack
of the current frame, not the frame stack. In my function, I wanted to
return the list of objects on the object stack (f_valuestack) in the caller's 
frame. For this, I must have or compute the number of objects on this stack,
but don't have an idea how to do it.

Thanks in advance!
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Current stackdepth outside PyEval_EvalFrameEx

2006-10-24 Thread Fabiano Sidler
On Monday 23 October 2006 02:20, I wrote:
 I'm trying to implement a python function that returns the current stack
 depth of its frame. Unfortunately, I don't see any possibility to get this
 value outside of PyEval_EvalFrameEx. Inside of it, I'd use the STACK_LEVEL
 macro. How do I do it?

No hints?

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Current stackdepth outside PyEval_EvalFrameEx

2006-10-22 Thread Fabiano Sidler
Hi folks!

I'm trying to implement a python function that returns the current stack
depth of its frame. Unfortunately, I don't see any possibility to get this
value outside of PyEval_EvalFrameEx. Inside of it, I'd use the STACK_LEVEL
macro. How do I do it?

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are the CALL_FUNCTION_* opcodes ever used?

2006-09-22 Thread Fabiano Sidler
On Thursday 21 September 2006 22:36, Peter Otten wrote:
  def test():

 ... func(*args)
 ... func(**kw)
 ... func(*args, **kw)

Oh, I didn't know the possibility of using the *args and **kwargs semantics
at function call. Thank you for revealing them to me! :)
Now it is also obvious how the CALL_FUNCTION_* opcodes are used.

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Are the CALL_FUNCTION_* opcodes ever used?

2006-09-21 Thread Fabiano Sidler
Hi folks!

Studying python byte code I encountered an interesting issue: there is no
matter, which one of the following function calls I compile:

1: func('foo','bar',foo='bar')
2: func('foobar')
3: func(foo='bar')

The compiler always uses the simple CALL_FUNCTION for all of the source
examples above. While this is fine for me (since the labels in Python/ceval.c
for the other 3 opcodes lead to the same code anyway), I'm curious to know
if there is a case where the compiler really uses the CALL_FUNCTION_* opcodes
or if we could silently remove these opcodes without breaking anything?

Greetings,
Fips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unable to resize mmap object

2006-05-02 Thread Fabiano Sidler
On Sunday 30 April 2006 21:06, Serge Orlov wrote:
 Fabiano Sidler wrote:
 Now, when I try to resize mm to 10 byte
 --- snip ---
 mm.resize(10)
 --- snap ---
 I get an EnvironmentError:[Errno 22] Invalid argument.

 Just a guess: try a new size argument that is multiple of page size.

No, doesn't work neitzer. :(

Thank you anyway for the idea!
-- 
http://mail.python.org/mailman/listinfo/python-list


unable to resize mmap object

2006-04-30 Thread Fabiano Sidler
Hi folks!

I created an mmap object like so:
--- snip ---
from mmap import mmap,MAP_ANONYMOUS,MAP_PRIVATE
fl = file('/dev/zero','rw')
mm = mmap(fl.fileno(), 1, MAP_PRIVATE|MAP_ANONYMOUS)
--- snap ---

Now, when I try to resize mm to 10 byte
--- snip ---
mm.resize(10)
--- snap ---
I get an EnvironmentError:[Errno 22] Invalid argument.

How can I implement a resizeable anonymous memory mapping?
Thanks for your reply!

F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


inheriting type or object?

2006-04-27 Thread Fabiano Sidler
Hi folks!

As stated in subject, how do I decide wether to inherit type 'type' or
type 'object'? Whenever I want to intantiate my derived type, I taked
type 'type' here, but inheriting from type 'object' consequently would
be reasonable in cases of pure static objects (i.e. objects/types using
staticmethods exclusively), for whose I would prefer toplevel code outside
a class definition in python, since python does not oblige programmers to
use classes (like JAVA et. al.).

So, finally, my question is: Is there a design pattern, that makes 
type 'object' a reasonable (direct) base type for new objects/types?
Right now, I can't see any.

Thank you for answering!
Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Inexplicable behaviour of type 'function'

2006-04-23 Thread Fabiano Sidler
Have a look to the following lines of code:
--- snip ---
class Foo: pass
def bar(): pass
Foo.bar = bar
--- snap ---

Why does 'bar.__get__(Foo) is Foo.bar' evaluate to False here? Did I
misunderstand the descriptor protocol?

Thank you for answering,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why are so many built-in types inheritable?

2006-03-28 Thread Fabiano Sidler
I really wanted to learn the reason for this, nothing else! ;)

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why are so many built-in types inheritable?

2006-03-25 Thread Fabiano Sidler
Kent Johnson [EMAIL PROTECTED] wrote:
 You could do this with a simple decorator:
 http://wiki.python.org/moin/PythonDecoratorLibrary#head-d4ce77c6d6e75aad25baf982f6fec0ff4b3653f4

 or I think your class PrintingFunction would work as
 class PrintingFunction(object):
def __init__(self, func):
  self.func = func
def __call__(self, *args, **kwargs):
  print args, kwargs
  return self.func(*args, **kwargs)

The problem with this is that the func_code attribute would contain
the code of PrintingFunction instead of func. What I wanted to do, is
to keep the original behaviour, i.e. set the variable __metaclass__ to
DebugMeta and so get debug output, without changing a function and
getting the original function's code object by the func_code
attribute, not PrintigFunction's one. That's why I *must* inherit from
type 'function'.

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why are so many built-in types inheritable?

2006-03-25 Thread Fabiano Sidler
25 Mar 2006 13:58:17 -0800, Ziga Seilnacht [EMAIL PROTECTED]:
 No, you don't have to:

Okay, but I'd prefer! ;)

 [a lot of python code]

That's what I wanted to avoid. Additionally, the possibility to do it
this way doesn't make it reasonable that type 'function' is
inheritable. Are there any reasons for that?

Greetings,
F.Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why are so many built-in types inheritable?

2006-03-24 Thread Fabiano Sidler
Hello? Or, is there any obvious reason for this behaviour I don't see?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: anonymous memory mapping

2006-03-20 Thread Fabiano Sidler
2006/3/14, Fabiano Sidler [EMAIL PROTECTED]:
 2006/3/14, Peter Hansen [EMAIL PROTECTED]:
  (As for me, I have no idea what the question is about, so this is the
  most help I can give.)

 Ok, sorry! I wanted to do this:

 --- snip ---
 from mmap import mmap, MAP_ANONYMOUS
 mm = mmap(-1, 1024, MAP_ANONYMOUS)
 --- snap ---

 But I got an EnvironmentError, [Errno 22] Invalid argument (on
 Linux2.6, btw.). The reason why I want to use anonymous mapping is
 that it only allocates the memory it actually uses.

Hello? Nobody out there who can answer this question?

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: anonymous memory mapping

2006-03-14 Thread Fabiano Sidler
2006/3/14, Peter Hansen [EMAIL PROTECTED]:
 (As for me, I have no idea what the question is about, so this is the
 most help I can give.)

Ok, sorry! I wanted to do this:

--- snip ---
from mmap import mmap, MAP_ANONYMOUS
mm = mmap(-1, 1024, MAP_ANONYMOUS)
--- snap ---

But I got an EnvironmentError, [Errno 22] Invalid argument (on
Linux2.6, btw.). The reason why I want to use anonymous mapping is
that it only allocates the memory it actually uses.

Greetings,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: anonymous memory mapping

2006-03-13 Thread Fabiano Sidler
2006/3/12, Fabiano Sidler [EMAIL PROTECTED]:
 Is there any way to use anonymous memory mapping in python, versions
 earlier than 2.5?

No idea or no way to do it?

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


anonymous memory mapping

2006-03-11 Thread Fabiano Sidler
Hi folks!

Is there any way to use anonymous memory mapping in python, versions
earlier than 2.5?

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Class use

2006-02-07 Thread Fabiano Sidler
Huh? You definitely must import that module. Then, is your homedir
listed in sys.path?

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using bytecode, not code objects

2006-02-06 Thread Fabiano Sidler
2006/1/29, Fabiano Sidler [EMAIL PROTECTED]:
 28 Jan 2006 22:02:45 -0800, Raymond Hettinger [EMAIL PROTECTED]:
  But if you want to make your life unnecessarily hard, you can hack the
  compiler module just upstream from the creation of the code object --
  alter the newCodeObject() method in pyassem.py.

 Thanks! I think this will help me, because it demonstrates how a code
 object is to be created (with new.code), although in a very
 complicated way.

Are you familiar with this module? I don't get the essence of it, even
with pdb (which I'm surely not using as neatly as it could be). Or is
there any documentation on it I couldn't find?

Greetings,
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list


Using bytecode, not code objects

2006-01-28 Thread Fabiano Sidler
Hi folks!

I'm looking for a way to compile python source to bytecode instead of
code-objects. Is there a possibility to do that? The reason is: I want
to store pure bytecode with no additional data.

The second question is, therefore: How can I get the correct values
for a given bytecode, such as the stacksize and flags attributes of
the correspondent code object?

No, I don't want to extract all of these things out of a code object.

Best wishes and thanks for answering!
F. Sidler
-- 
http://mail.python.org/mailman/listinfo/python-list