Extension Module for Python 3.6 +

2019-03-01 Thread Anthony Flury via Python-list
In my brave and noble quest to get to grips with the CAPI - I am trying 
to write a C extension module which provides a new class


The exact details are not important, but what is important is that 
instances of my new class are imutable, and therefore from time to time, 
my extension module needs to build a new instance of my extension class.


The documentation is pretty sparse here, and suggests using PyObject_New 
& PyObject_Init but gives no examples.


An internet search suggests using PyObject_CallObject in some way - but 
the examples here are call backs to Python functions.


What is the canonical way to create New Instances of the Extension Type 
from within the Extension Module - even if someone can point me to a 
working example that would be great.


Another option would be to call the extension class new and init 
functions directly - is there a reason that is not allowed ?


PS - I think I also need to use Py_BuildValue to 'encapsulate' the 
initial arguments for the new instance - or could I just build a 'blank' 
instance and then directly set the fields as neccessary ?


--
Anthony Flury
*Email* : anthony.fl...@btinternet.com 
*Twitter* : @TonyFlury 
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to setup for localhost:8000

2019-03-01 Thread tommy yama
Here you go
A port can be configurable.

https://www.tutorialspoint.com/python/python_http_server.htm


On Mon, Feb 25, 2019 at 12:48 PM Cameron Simpson  wrote:

> On 24Feb2019 19:00, 0x906  wrote:
> >>> I am working on window 7 and Python 3.5 to setup a localhost:8000
> >>> but it did not get through as shown below:
>  python -m http.server
> >>> Serving HTTP on 0.0.0.0 port 8000 ...
> >>> But it did not show the results.
> >>> Can someone help me how to setup the localhost?
> >
> >There is a chance that I missed something with the 0.0.0.0. but I am
> pretty sure that the localhost IP is 127.0.0.1.
>
> Yeah. 0.0.0.0 is the wildcard address: the server will be listening on
> all the available addresses (127.0.0.1 and also any LAN address).
>
> Wen-Ruey may simply be missing that it is just running a web server. To
> actually see anything she/he needs to hit it with a web browser, for
> example with a URL like:
>
>   http://127.0.0.1:8000/
>
> Cheers,
> Cameron Simpson 
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: revisiting the "What am I running on?" question

2019-03-01 Thread songbird
Terry Reedy wrote:
> On 2/22/2019 7:55 AM, songbird wrote:
>> eryk sun wrote:
>> ...
>>> The win-amd64 ABI is significantly different, but at the API level
>>> there isn't a drastic difference between 32-bit and 64-bit Windows, so
>>> there's no cognitive burden with perpetuating the Win32 name. The
>>> official API name was actually changed to "Windows API" or WINAPI (or
>>> WinAPI). But it would require a massive effort to change the culture.
>>> There's no pressing need to expend that much time and energy over a
>>> name.
>> 
>>just adding a comment to the documentation that
>> win32 also covers win64 would be a help IMO.
>
> Could you open an issue on the tracker suggesting a specific edit at a 
> specific place?

  i don't want to create yet another account to file
a bug/issue, but the specific place in the docs is:

  
https://docs.python.org/3/library/sys.html?highlight=sys%20platform#sys.platform


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


Version numbers in Standard Library

2019-03-01 Thread Thompson, Matt (GSFC-610.1)[SCIENCE SYSTEMS AND APPLICATIONS INC] via Python-list

Dear Python List,

A question. I help maintain a Python stack for users in my division here 
at NASA and one user asked about updating the re module to 2.4. I 
believe because he read the docs:


https://docs.python.org/2.7/library/re.html

where you see lines like "New in version 2.4" and he also did:

$ python2 -c 'import re; print (re.__version__)'
2.2.1

And, well, one can think "oh, a newer version is needed". I searched on 
conda, etc. and can't find it and finally realized that 2.4 meant Python 
2.4, not re 2.4. (The 3.7 docs have lines like "Changed in version 3.7".)


My question to the pros here is what purpose do the __version__/version 
variables serve in the Python Standard Library?  I can understand in 
external packages, but once in the Standard Library...?


For example, in re.py, that line was last changed 18 years ago according 
to git blame. In tarfile.py, the version string was last changed 12 
years ago. But in both, the modules were edited in 2018 so they haven't 
been static for a decade.


Are those strings there just for historic purposes?

Not a big deal, I was just wondering.

Thanks,
Matt

--
Matt Thompson, SSAI, Sr Scientific Programmer/Analyst
NASA GSFC,Global Modeling and Assimilation Office
Code 610.1,  8800 Greenbelt Rd,  Greenbelt,  MD 20771
Phone: 301-614-6712 Fax: 301-614-6246
http://science.gsfc.nasa.gov/sed/bio/matthew.thompson
--
https://mail.python.org/mailman/listinfo/python-list


Re: Extension Module for Python 3.6 +

2019-03-01 Thread MRAB

On 2019-03-01 01:19, Anthony Flury via Python-list wrote:

In my brave and noble quest to get to grips with the CAPI - I am trying
to write a C extension module which provides a new class

The exact details are not important, but what is important is that
instances of my new class are imutable, and therefore from time to time,
my extension module needs to build a new instance of my extension class.

The documentation is pretty sparse here, and suggests using PyObject_New
& PyObject_Init but gives no examples.

An internet search suggests using PyObject_CallObject in some way - but
the examples here are call backs to Python functions.

What is the canonical way to create New Instances of the Extension Type
from within the Extension Module - even if someone can point me to a
working example that would be great.

Another option would be to call the extension class new and init
functions directly - is there a reason that is not allowed ?

PS - I think I also need to use Py_BuildValue to 'encapsulate' the
initial arguments for the new instance - or could I just build a 'blank'
instance and then directly set the fields as neccessary ?

Here's an example that exports a 'make' factory function to create a new 
instance. Hope it helps:


8< class_example.c 8<

#include "Python.h"
#include "structmember.h" /* offsetof */

/* The MyClassObject. */
typedef struct MyClassObject {
PyObject_HEAD
PyObject* weakreflist; /* List of weak references. */
PyObject* value;
} MyClassObject;

/* The MyClass_Type. */
static PyTypeObject MyClass_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"class_example.MyClass",
sizeof(MyClassObject)
};

/* Makes an instance with the given value. */
Py_LOCAL_INLINE(PyObject*) myclass_make(PyObject* self_, PyObject* args,
  PyObject* kwargs) {
PyObject* value;
MyClassObject* instance;

static char* kwlist[] = { "value", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &value))
return NULL;

   instance = PyObject_New(MyClassObject, &MyClass_Type);
   if (!instance)
return NULL;

   instance->value = value;
   Py_INCREF(instance->value);

   return (PyObject*)instance;
}

/* Deallocates a MyClassObject. */
static void myclass_dealloc(PyObject* self_) {
MyClassObject* self;

self = (MyClassObject*)self_;

Py_DECREF(self->value);

PyObject_DEL(self);
}

/* The documentation of a MyClassObject. */
PyDoc_STRVAR(myclass_doc, "MyClass object");

/* The methods of a MyClassObject. */
static PyMethodDef myclass_methods[] = {
/* The instance methods here. */
{NULL, NULL}
};

/* The members of a MyClassObject. */
static PyMemberDef myclass_members[] = {
{"value", T_OBJECT, offsetof(MyClassObject, value), READONLY,
  "The stored value."},
{NULL} /* Sentinel */
};

PyDoc_STRVAR(myclass_make_doc,
"make(value) --> MyClassObject.\n"
"Makes a MyClass object.");

/* The table of the module's functions. */
static PyMethodDef functions[] = {
{ "make", (PyCFunction)myclass_make, METH_VARARGS | METH_KEYWORDS,
  myclass_make_doc },
{ NULL, NULL }
};

/* The module definition. */
static struct PyModuleDef class_example_module = {
PyModuleDef_HEAD_INIT,
"class_example",
NULL,
-1,
functions,
NULL,
NULL,
NULL,
NULL
};

/* Initialises the module. */
PyMODINIT_FUNC PyInit_class_example(void) {
PyObject* this_module;

/* Initialise the MyClass_Type. */
MyClass_Type.tp_dealloc = myclass_dealloc;
MyClass_Type.tp_flags = Py_TPFLAGS_DEFAULT;
MyClass_Type.tp_doc = myclass_doc;
MyClass_Type.tp_weaklistoffset = offsetof(MyClassObject, weakreflist);
MyClass_Type.tp_methods = myclass_methods;
MyClass_Type.tp_members = myclass_members;
if (PyType_Ready(&MyClass_Type) < 0)
return NULL;

/* Create the module. */
this_module = PyModule_Create(&class_example_module);
if (!this_module)
return NULL;

/* This makes MyClass visible, but won't let you create an 
instance. Useful

 * with Python's isinstance function.
 */
if (PyModule_AddObject(this_module, "MyClass", 
(PyObject*)&MyClass_Type) != 0)

return NULL;

return this_module;
}

8< test_example.py 8<

#!python3.7
# -*- coding: utf-8 -*-
import class_example

from class_example import make
instance = make(0)
print(instance)
print(instance.value)

# Cannot change the attribute:
#instance.value = 1
--
https://mail.python.org/mailman/listinfo/python-list


Re: Version numbers in Standard Library

2019-03-01 Thread Skip Montanaro
The point of the "Changed in version ..." or "New in version ..." bits
in the documentation is to alert readers who maintain software which
needs to remain backward compatible with older versions of Python. If
you maintain a package which you support for Python 3.4, 3.5, 3.6, and
3.7, you'll probably shy away from bits which weren't around for 3.4,
and be careful about APIs which have changed since 3.4.

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


Re: Lifetime of a local reference

2019-03-01 Thread Alan Bawden
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Alan Bawden  writes:
> >The Java compiler has no way to know whether a variable references an
> >object with a finalize() method that has side effects
> 
>   java.lang.Object#finalize() is deprecated since Java 9. 

And we are advised to use a "Cleaner" or a "PhantomReference" instead.  So
there are still non-deprecated mechanisms in Java 11 you can use to run
cleanup code when an object becomes unreachable.  And the language I quoted
from the Java 8 spec is still there in the Java 11 spec.  So the situation
is unchanged with respect to the point I was making.

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


Re: Lifetime of a local reference

2019-03-01 Thread Gregory Ewing

Alan Bawden wrote:

The Java compiler has no way to know whether a variable references an
object with a finalize() method that has side effects


It should be able to tell in some situations, e.g.

String a = "hello";
String b = a.replace('e', 'u');

There's no way that b can reference anything other than a
plain String instance at this point.

Maybe Java implementations are living dangerously and making
assumptions beyond this. My point is that I would expect a
Python implementation to be careful enough not to get this
wrong.

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